Skip to content

Mailer wrapper over SwiftMailer and View component for Phalcon.

Notifications You must be signed in to change notification settings

alvlapo/phalcon-mailer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Mailer wrapper over SwiftMailer and View component for Phalcon.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published