Repository navigation
Cake Email
CakeEmail is a new class from the cake library to send emails. With this class you can send email from any place of your application, ie. Controllers, Models, Console.
This class replace the EmailComponent and give more flexibility to you send your emails. For example, you can create your custom methods to send email instead use the hard-coded smtp and mail.
First of all, you should call the App::uses() to import the class: App::uses('CakeEmail', 'Network');
The use of the class is similar of EmailComponent, but instead use attributes you must use methods. Example:
$email = new CakeEmail();
$email->from('me@example.com');
$email->to('you@example.com');
$email->subject('About');
$email->send('My message');To simplify, all set methods return the instance of class. You can re-write the same code as:
$email = new CakeEmail();
$email->from('me@example.com')->to('you@example.com')->subject('About')->send('My message');Transports are classes designed to send the e-mail over some protocol or method. CakePHP support the Mail (default) and Smtp transports.
To configure your method, you must use the CakeEmail::transport() method.
You are able to create your custom transports to integrate with others emails systems (like SwiftMailer). To it, you must to create the file APP/Network/Email/YourTransport.php (where Your is the name of your transport).
This file must contain the class YourTransport extending AbstractTransport (do not forget to use App::uses('AbstractTransport', 'Network/Email'); before).
You must implement the method send(CakeEmail $email) with your custom logic. Optionally, you can implement the method config($config) that is called before the send to pass the user configurations. By default, this method put the configuration in protected attribute $_config.
If you need to call some method from this transport before send, you can call the method transportClass() from CakeEmail to get an instance of transport. Example:
$yourInstance = $email->transport('your')->transportClass();
$yourInstance->myCustomMethod();
$email->send();Similar of database configuration, email also have a class to put the configurations!
You should create the file APP/config/email.php with the class EmailConfig. The APP/config/email.php.default have an example of this file.
The CakeEmail create an instance of this file before use the config. If you have a variable data to put in the configs, you can use the constructor to do that.
- ssl? gmail?
Are you bored with too many configurations to do every time? You send every time the same email? Ok, now it is more easy...
You can create a configuration in EmailConfig or an array with all options that you need and use the static method CakeEmail::fastSend(). Example:
CakeEmail::fastSend('you@example.com', 'Subject', 'Message', array('from' => 'me@example.com'));
This method will send an email to you@example.com, from me@example.com with subject Subject and content Message.
The return is a CakeEmail instance with all configurations setted. If you do not want send the email and configure something more before send, you can pass the 5th parameter as false.
The 3th parameter is the content of message or an array with variables (when using rendered content). The 4th parameter can be an array with the configurations or a string with the name of configuration in EmailConfig.
If you want, you can pass the to, subject and message as null and do all configurations in the 4th parameter (as array or using EmailConfig). The follow configurations are used:
- 'from': Email or array of sender. See CakeEmail::from().
- 'to': Email or array of destination. See CakeEmail::to().
- 'cc': Email or array of carbon copy. See CakeEmail::cc().
- 'bcc': Email or array of blind carbon copy. See CakeEmail::bcc().
- 'replyTo': Email or array to reply the e-mail. See CakeEmail::replyTo().
- 'readReceipt': Email or array to receive the receipt of read. See CakeEmail::readReceipt().
- 'returnPath': Email or array to return if have some error. See CakeEmail::returnPath().
- 'messageId': Message ID of e-mail. See CakeEmail::messageId().
- 'subject': Subject of the message. See CakeEmail::subject().
- 'message': Content of message. Do not set this field if you are using rendered content.
- 'headers': Headers to be included. See CakeEmail::setHeaders().
- 'viewRender': If you are using rendered content, set the view classname. See CakeEmail::viewRender().
- 'template': If you are using rendered content, set the template name. See CakeEmail::template().
- 'layout': If you are using rendered content, set the layout to render. If you want to render a template without layout, set this field to null. See CakeEmail::template().
- 'viewVars': If you are using rendered content, set the array with variables to be used in the view. See CakeEmail::viewVars().
- 'attachments': List of files to attach. See CakeEmail::attachments().
- 'emailFormat': Format of email (html, text or both). See CakeEmail::emailFormat().
- 'transport': Transport name. See CakeEmail::transport().
All these configurations are optional, except 'from'. If you put more configurations in this array, these configurations will be used in the CakeEmail::config() method. For example, if you are using smtp transport, you should pass the host, port and others configurations.
EmailComponent is now deprecated, but keep working. Internally this class is using CakeEmail to send emails.
We recommend to you upgrade your code to use CakeEmail class instead of the component. Below some tips about the migration.
- Configuration attributes are now methods. For example
$emailComponent->to = 'user@example.com';is now$cakeEmail->to('user@example.com');. The same is applied tofrom,cc,bccand all the others addresses, subject, messageId, etc. - The headers are not changed to be X-... What you set is what we use. So, remember to put X- in your custom headers.
- The
send()method receive only the message content. The template and layout should be set usingCakeEmail::template()method. - The list of attachements should be and array with filename (that will appear in email) as key and value the full path to real file.
- At any error, CakeEmail will thrown an exception instead of return false. We recommend to you use try/catch to check your variable configs and sends and avoid that some code break because that.