swiftmailer Swift Mailer

  1. Documentation
  2. Change Log

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.
3 2
14 followers
6 516 downloads
Yii Version: 1.1
License: GPL-3.0
Category: Mail
Tags:
Developed by: sAe
Created on: May 19, 2010
Last updated: 14 years ago

Downloads

show all