Coder Social home page Coder Social logo

phalcon-mailer's Introduction

phalcon-mailer

Mailer wrapper over SwiftMailer for Phalcon framework.

Usage

Configuration

There are several types of Transport in Swift Mailer, you create a Transport, use it to create the Manager, then you use the Manager to send the mail.

$transport = new \Swift_SendmailTransport();
$view = \Phalcon\Di::getDefault()->getShared('view');

$mailer = new \Rotoscoping\Phalcon\Manager($transport, $view);

// Add mailer to container (optional)
\Phalcon\Di::getDefault()->setShared('mailer', $mailer);

Sending the Email

// Get mailer from DI
$mailer = \Phalcon\Di::getDefault()->get('mailer');

// Compose mail
$mailer
  ->to('[email protected]')
  ->subject('Simple subject')
  ->text('Simple text part message')
  ->send();
  
// Fin

Configuring The Sender

There are two ways to configure the sender.

First, you may use the from method within your Mail class' build method:

// You must initialize and add Manager instance to DI as 'mailer' service (see above)
// or extend Mail class and owerride method getMailerInstance()

$mail = new Mail();
$mail
  ->from('[email protected]', 'Support team')
  ->to('[email protected]')
  ->subject('Simple subject')
  ->text('Simple text part message');
  
$mail->send();

Instead, you may specify a global "from" address throw draft mail in your Manager instance. This address will be used by default for all you mails:

$defaultMail = new Mail();
$defaultMail
  ->from('[email protected]', 'Support team');

// Get mailer from DI (see initializatio section)
$mailer = \Phalcon\Di::getDefault()->get('mailer');

$mailer->setDraftMail($defaultMail);

$mail = new Mail();
$mail
  ->to('[email protected]')
  ->subject('Simple subject')
  ->text('Simple text part message');
  
$mail->send();

Example

Sending a message from the template

// Initialize manager with mail and view services from DI
$mailer = new \Rotoscoping\Phalcon\Manager('mail', 'view');

// template email_confirmation.phtml in your viewDir
$mailer->send(
  // view path
  'email_confirmation',
  // Swift params
  [
    'subject' => 'Example email confirmation',
    'from' => '[email protected]',
    'to' => '[email protected]'
  ],
  // View params
  [ 
    'username' => 'User Name',
    'token' => 'aq1sw2de3'
  ]
);

Sending a message from the template via a magic method call

// word of the send from the beginning and the template filename in camelCase notation
$mailer->sendEmailConfirmation(
  [
    'subject' => 'Example email confirmation',
    'from' => '[email protected]',
    'to' => '[email protected]'
  ],
  [
    'username' => 'User Name',
    'token' => 'aq1sw2de3'
  ]
);

Sending with a Mailable

Using mailable classes are a lot more elegant than the basic usage example above. Building up the mail in a mailable class cleans up controllers and routes, making things look a more tidy and less cluttered as well as making things so much more manageable.

All of a mailable class' configuration is done in the compose method. Within this method, you may call various methods such as from, subject, view, and attach to configure the email's presentation and delivery.

Mailable classes are required to extend the base Rotoscoping\Phalcon\Mailer\Mailable class;

Inspired by

phalcon-mailer's People

Contributors

alvlapo avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.