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.
swiftMailer folder to protected/extensionsAdd the component to your config file within the components section:
'swiftMailer' => array( 'class' => 'ext.swiftMailer.SwiftMailer', ),
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);
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); }
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(); }
Total 11 comments
Error 500 fsockopen(): unable to connect to STARTTLS://mail.myserver.com:25 (Unable to find the socket transport "STARTTLS" - did you forget to enable it when you configured PHP?) Any idea guyz how to resolve this error
Needed to let the user upload files then have them sent as attachments to site Admins using the CMultiFileUpload Widget (very handy).
Upload View
Controller
Changes to the SwiftMailer.php wrapper
Enjoy!
SUGGESTION:
add the third parameter 'security = null' to constructor of smtpTransport, like in class SmptTransport::__construct
Extensione is 100% perfect. Is it a way to upgrade it at latest version of Swift? SwiftMailer looks like it now can survive at Yii autoloading.
I want to sent email more than 100 at a time. The sever time out when i try to send email server time out. So I want to use antiflood plugin availabe in the swiftmailer libray. If there is any solution out regarding this problem . I would be very happy.
Thanks in Advance
Regards Sundar
Hey guys, I needed swiftmailer support, so I took this ext and had it redone: http://www.yiiframework.com/extension/wkd-swiftmailer/
I kind of get the feeling the wrapper is half baked. Looking through the documentation I was able to add this to his wrapper:
Usage:
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
I'm trying various methods of attaching a file and having no luck. Simply appending ->attachment( variables ) is not working for me. Any thoughts?
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."
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
Some of the links above, are broken (in the extension description).
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 login to leave your comment.