swiftmailer

Swift Mailer
7 followers

This is a wrapper class to Swift Mailer with some helper methods to speed up sending mail. Current version of Swift Mailer is 4.0.4 due to 4.0.6 (latest as of speaking) having troubles with autoloading classes. Feel free to upgrade if there's anything specific in the new version you might need.

Documentation

Requirements

  • Should work with all Yii versions

Installation

  • Extract and move the swiftMailer folder to protected/extensions
  • Add the swiftmailer extension to config file (optional)

Refer to the authors documentation for more detailed instructions on how the library works

Usage

Add the component to your config file within the components section:

'swiftMailer' => array(
    'class' => 'ext.swiftMailer.SwiftMailer',
),

Alternatively

Import the class where you intend to use it

// Import class
Yii::import('ext.swiftMailer.SwiftMailer');
// Create a new Transport object
$Transport = SwiftMailer::smtpTransport($host, $port);

Example usage

Using SMTP server to send a HTML mail (using a view) and an alternative in plain text

public function actionViewTest() {
 
    // Render view and get content
    // Notice the last argument being `true` on render()
    $content = $this->render('viewTest', array(
        'Test' => 'TestText 123',
    ), true);
 
    // Plain text content
    $plainTextContent = "This is my Plain Text Content for those with cheap emailclients ;-)\nThis is my second row of text";
 
    // Get mailer
    $SM = Yii::app()->swiftMailer;
 
    // Get config
    $mailHost = 'mail.example.com';
    $mailPort = 25; // Optional
 
    // New transport
    $Transport = $SM->smtpTransport($mailHost, $mailPort);
 
    // Mailer
    $Mailer = $SM->mailer($Transport);
 
    // New message
    $Message = $SM
        ->newMessage('My subject')
        ->setFrom(array('from@example.com' => 'Example Name'))
        ->setTo(array('recipient@example.com' => 'Recipient Name'))
        ->addPart($content, 'text/html')
        ->setBody($plainTextContent);
 
    // Send mail
    $result = $Mailer->send($Message);
}

Available helpers in SwiftMailer class

public function preferences() {
    return  Swift_Preferences;
}
 
public function attachment() {
    return Swift_Attachment;
}
 
public function newMessage($subject) {
    return Swift_Message::newInstance($subject);
}
 
public function mailer($transport=null) {
    return Swift_Mailer::newInstance($transport);
}
 
public function image() {
    return Swift_Image;
}
 
public function smtpTransport($host=null, $port=null) {
    return Swift_SmtpTransport::newInstance($host, $port);
}
 
public function sendmailTransport($command=null) {
    return Swift_SendmailTransport::newInstance($command);
}
 
public function mailTransport() {
    return Swift_MailTransport::newInstance();
}

Change Log

May 19, 2010

  • Initial release.

Total 7 comments

#6145 report it
b3atb0x at 2011/12/15 11:56am
Update

Hey guys, I needed swiftmailer support, so I took this ext and had it redone: http://www.yiiframework.com/extension/wkd-swiftmailer/

#5608 report it
notsoluckycharm at 2011/10/24 06:10pm
Attaching a File

I kind of get the feeling the wrapper is half baked. Looking through the documentation I was able to add this to his wrapper:

public function fromPath( $path ){
       return Swift_Attachment::fromPath($path);
    }

Usage:

$Message = $SM
        ->newMessage('Booking Confirmation for '.$this->lastOrigin->state->code. ', '.$this->lastOrigin->country->ccode. ' to '. $this->lastDestination->city. ', '.$this->lastDestination->country->country.' Order '.$this->id)
        ->setFrom(array('emails@morganshipping.com' => 'Morgan Shipping'))
        ->setTo(array('hevean@gmail.com' => 'Brad Gunn'))
        ->addPart($content, 'text/html')
        ->setBody("")->attach($SM->fromPath(Yii::app()->params['webRoot'].$fileUrlBase = Yii::app()->params['ucSignFileBase'].$this->ucsignfile), "foo.pdf", "application/pdf");
          // Send mail
          $result = $Mailer->send($Message);

Hopefully that makes sense. I used the attach which is a method of Swift_Message. His attachment() is not useful for static calls to ::fromPath

#5607 report it
notsoluckycharm at 2011/10/24 05:49pm
Documentation on Attachment Would be Nice

I'm trying various methods of attaching a file and having no luck. Simply appending ->attachment( variables ) is not working for me. Any thoughts?

#4495 report it
danko at 2011/07/13 10:32am
doesnt want to work

iwas do all like in man but it's doest want to work: error like "Alias "ext.SwiftMailer.SwiftMailer" is invalid. Make sure it points to an existing PHP file."

#3103 report it
stinkytofu at 2011/03/16 10:35am
Bug fix for "failed to open stream" error

There is an inherent bug in this extension. If you are receiving an error as follows when trying to use this extension:

YiiBase::include(Swift_SmtpTransport.php) [yiibase.include]: failed to open stream: No such file or directory

Just edit protected/extensions/SwiftMailer.php and changed the following code on line 38 from:

return Swift_SmtpTransport::newInstance($host, $port);

to:

return SmtpTransport::newInstance($host, $port);

That will fix the problem

#2338 report it
intel352 at 2010/12/14 01:21pm
Links broken in description

Some of the links above, are broken (in the extension description).

#343 report it
k0432 at 2010/07/05 12:46am
无法支持认证smpt

public function smtpTransport($host=null, $port=null,$username=null,$password=null) { return Swift_SmtpTransport::newInstance($host, $port)->setUsername($username)->setPassword($password); }

Leave a comment

Please to leave your comment.

Create extension