Skip to content

Commit 4d31ed9

Browse files
committed
Merge pull request cakephp#26 from ceeram/master
updating email docs
2 parents c36dbe2 + a96ae33 commit 4d31ed9

File tree

1 file changed

+88
-46
lines changed

1 file changed

+88
-46
lines changed

en/core-utility-libraries/email.rst

Lines changed: 88 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ using attributes you must use methods. Example::
3030
$email->subject('About');
3131
$email->send('My message');
3232

33-
To simplify things, all of the setter methods return the instance of class. You can re-write the
34-
above code as::
33+
To simplify things, all of the setter methods return the instance of class.
34+
You can re-write the above code as::
3535

3636
<?php
3737
$email = new CakeEmail();
@@ -57,14 +57,13 @@ original sender using the Sender header. You can do so using ``sender()``::
5757
deliverability.
5858

5959
Configuration
60-
-------------
60+
=============
6161

62-
Similar of database configuration, emails also have a class to centralize all the
62+
Similar of database configuration, emails can have a class to centralize all the
6363
configuration.
6464

65-
You should create the file ``app/Config/email.php`` with the class
66-
``EmailConfig``. The ``app/Config/email.php.default`` has an example of this
67-
file.
65+
Create the file ``app/Config/email.php`` with the class ``EmailConfig``.
66+
The ``app/Config/email.php.default`` has an example of this file.
6867

6968
``CakeEmail`` will create an instance of the ``EmailConfig`` class to access the
7069
config. If you have dynamic data to put in the configs, you can use the
@@ -77,6 +76,30 @@ constructor to do that::
7776
}
7877
}
7978

79+
It is not required to create ``app/Config/email.php``, ``CakeEmail`` can be used
80+
without it and use respective methods to set all configurations separately or
81+
load an array of configs.
82+
83+
To load a config from ``EmailConfig`` you can use the ``config()`` method or pass it
84+
to the constructor of ``CakeEmail``::
85+
86+
<?php
87+
$email = new CakeEmail();
88+
$email->config('default');
89+
90+
//or in constructor::
91+
$email = new CakeEmail('default');
92+
93+
Instead of passing a string which matches the configuration name in ``EmailConfig``
94+
you can also just load an array of configs::
95+
96+
<?php
97+
$email = new CakeEmail();
98+
$email->config(array('from' => 'me@example.org', 'transport' => 'MyCustom'));
99+
100+
//or in constructor::
101+
$email = new CakeEmail(array('from' => 'me@example.org', 'transport' => 'MyCustom'));
102+
80103
You can configure SSL SMTP servers, like GMail. To do so, put the ``'ssl://'``
81104
at prefix in the host and configure the port value accordingly. Example::
82105

@@ -95,6 +118,40 @@ at prefix in the host and configure the port value accordingly. Example::
95118
To use this feature, you will need to have the SSL configured in your PHP
96119
install.
97120

121+
.. _email-configurations:
122+
123+
Configurations
124+
--------------
125+
126+
The following configuration keys are used:
127+
128+
- ``'from'``: Email or array of sender. See ``CakeEmail::from()``.
129+
- ``'sender'``: Email or array of real sender. See ``CakeEmail::sender()``.
130+
- ``'to'``: Email or array of destination. See ``CakeEmail::to()``.
131+
- ``'cc'``: Email or array of carbon copy. See ``CakeEmail::cc()``.
132+
- ``'bcc'``: Email or array of blind carbon copy. See ``CakeEmail::bcc()``.
133+
- ``'replyTo'``: Email or array to reply the e-mail. See ``CakeEmail::replyTo()``.
134+
- ``'readReceipt'``: Email or array to receive the receipt of read. See ``CakeEmail::readReceipt()``.
135+
- ``'returnPath'``: Email or array to return if have some error. See ``CakeEmail::returnPath()``.
136+
- ``'messageId'``: Message ID of e-mail. See ``CakeEmail::messageId()``.
137+
- ``'subject'``: Subject of the message. See ``CakeEmail::subject()``.
138+
- ``'message'``: Content of message. Do not set this field if you are using rendered content.
139+
- ``'headers'``: Headers to be included. See ``CakeEmail::setHeaders()``.
140+
- ``'viewRender'``: If you are using rendered content, set the view classname. See ``CakeEmail::viewRender()``.
141+
- ``'template'``: If you are using rendered content, set the template name. See ``CakeEmail::template()``.
142+
- ``'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()``.
143+
- ``'viewVars'``: If you are using rendered content, set the array with variables to be used in the view. See ``CakeEmail::viewVars()``.
144+
- ``'attachments'``: List of files to attach. See ``CakeEmail::attachments()``.
145+
- ``'emailFormat'``: Format of email (html, text or both). See ``CakeEmail::emailFormat()``.
146+
- ``'transport'``: Transport name. See ``CakeEmail::transport()``.
147+
- ``'log'``: Log level to log the email headers and message. ``true`` will use LOG_DEBUG. See also ``CakeLog::write()``
148+
149+
All these configurations are optional, except ``'from'``. If you put more
150+
configuration in this array, the configurations will be used in the
151+
:php:meth:`CakeEmail::config()` method and passed to the transport class ``config()``.
152+
For example, if you are using smtp transport, you should pass the host, port and
153+
other configurations.
154+
98155
Setting headers
99156
---------------
100157

@@ -142,6 +199,16 @@ This would use the following view files:
142199
When sending templated emails you have the option of sending either
143200
``text``, ``html`` or ``both``.
144201

202+
You can set view variables with ``CakeEmail::viewVars()``::
203+
204+
<?php
205+
$email = new CakeEmail('templated');
206+
$email->viewVars(array('value' => 12345));
207+
208+
And in your email templates you can use these with::
209+
210+
<p>Here is your value: <b><?php echo $value; ?></b></p>
211+
145212
Sending attachments
146213
-------------------
147214

@@ -158,15 +225,15 @@ you want the filenames to appear in the recipient's mail client:
158225
attach some_hash.png with the name photo.png. The recipient will see
159226
photo.png, not some_hash.png.
160227
4. Nested arrays::
161-
162-
<?php
163-
$email->attachments(array(
164-
'photo.png' => array(
165-
'file' => '/full/some_hash.png',
166-
'mimetype' => 'image/png',
167-
'contentId' => 'my-unique-id'
168-
)
169-
));
228+
229+
<?php
230+
$email->attachments(array(
231+
'photo.png' => array(
232+
'file' => '/full/some_hash.png',
233+
'mimetype' => 'image/png',
234+
'contentId' => 'my-unique-id'
235+
)
236+
));
170237

171238
The above will attach the file with different mimetype and with custom
172239
Content ID (when set the content ID the attachment is transformed to inline).
@@ -182,7 +249,7 @@ Transports are classes designed to send the e-mail over some protocol or method.
182249
CakePHP support the Mail (default), Debug and Smtp transports.
183250

184251
To configure your method, you must use the :php:meth:`CakeEmail::transport()`
185-
method.
252+
method or have the transport in your configuration
186253

187254
Creating custom Transports
188255
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -222,8 +289,8 @@ Sending messages quickly
222289
========================
223290

224291
Sometimes you need a quick way to fire off an email, and you don't necessarily
225-
want do setup a bunch of configuration ahead of time.
226-
:php:meth:`~CakeEmail::deliver()` is intended for that purpose.
292+
want do setup a bunch of configuration ahead of time.
293+
:php:meth:`CakeEmail::deliver()` is intended for that purpose.
227294

228295
You can create your configuration in ``EmailConfig``, or use an array with all
229296
options that you need and use the static method ``CakeEmail::deliver()``.
@@ -247,30 +314,5 @@ The 4th parameter can be an array with the configurations or a string with the
247314
name of configuration in ``EmailConfig``.
248315

249316
If you want, you can pass the to, subject and message as null and do all
250-
configurations in the 4th parameter (as array or using ``EmailConfig``). The
251-
follow configurations are used:
252-
253-
- ``'from'``: Email or array of sender. See ``CakeEmail::from()``.
254-
- ``'sender'``: Email or array of real sender. See ``CakeEmail::sender()``.
255-
- ``'to'``: Email or array of destination. See ``CakeEmail::to()``.
256-
- ``'cc'``: Email or array of carbon copy. See ``CakeEmail::cc()``.
257-
- ``'bcc'``: Email or array of blind carbon copy. See ``CakeEmail::bcc()``.
258-
- ``'replyTo'``: Email or array to reply the e-mail. See ``CakeEmail::replyTo()``.
259-
- ``'readReceipt'``: Email or array to receive the receipt of read. See ``CakeEmail::readReceipt()``.
260-
- ``'returnPath'``: Email or array to return if have some error. See ``CakeEmail::returnPath()``.
261-
- ``'messageId'``: Message ID of e-mail. See ``CakeEmail::messageId()``.
262-
- ``'subject'``: Subject of the message. See ``CakeEmail::subject()``.
263-
- ``'message'``: Content of message. Do not set this field if you are using rendered content.
264-
- ``'headers'``: Headers to be included. See ``CakeEmail::setHeaders()``.
265-
- ``'viewRender'``: If you are using rendered content, set the view classname. See ``CakeEmail::viewRender()``.
266-
- ``'template'``: If you are using rendered content, set the template name. See ``CakeEmail::template()``.
267-
- ``'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()``.
268-
- ``'viewVars'``: If you are using rendered content, set the array with variables to be used in the view. See ``CakeEmail::viewVars()``.
269-
- ``'attachments'``: List of files to attach. See ``CakeEmail::attachments()``.
270-
- ``'emailFormat'``: Format of email (html, text or both). See ``CakeEmail::emailFormat()``.
271-
- ``'transport'``: Transport name. See ``CakeEmail::transport()``.
272-
273-
All these configurations are optional, except ``'from'``. If you put more
274-
configuration in this array, the configurations will be used in the
275-
:php:meth:`CakeEmail::config()` method. For example, if you are using smtp transport,
276-
you should pass the host, port and others configurations.
317+
configurations in the 4th parameter (as array or using ``EmailConfig``).
318+
Check the list of :ref:`configurations <email-configurations>` to see all accepted configs.

0 commit comments

Comments
 (0)