Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
e674191d
Commit
e674191d
authored
May 29, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mail tutorial created
parent
9e320daf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
1 deletion
+134
-1
README.md
docs/guide/README.md
+1
-1
tutorial-mailing.md
docs/guide/tutorial-mailing.md
+133
-0
No files found.
docs/guide/README.md
View file @
e674191d
...
...
@@ -171,7 +171,7 @@ Special Topics
*
[
Handling Errors
](
tutorial-handling-errors.md
)
*
[
Internationalization
](
tutorial-i18n.md
)
*
[
Logging
](
tutorial-logging.md
)
*
**TBD**
[
Mailing
](
tutorial-mailing.md
)
*
[
Mailing
](
tutorial-mailing.md
)
*
[
Performance Tuning
](
tutorial-performance-tuning.md
)
*
**TBD**
[
Shared Hosting Environment
](
tutorial-shared-hosting.md
)
*
[
Template Engines
](
tutorial-template-engines.md
)
...
...
docs/guide/tutorial-mailing.md
0 → 100644
View file @
e674191d
Mailing
=======
> Note: This section is under development.
Yii supports composition and sending of the email messages. However, the framework core provides
only the content composition functionality and basic interface. Actual mail sending mechanism should
be provided by the extension, because different projects may require its different implementation and
it usually depends on the external services and libraries.
For the most common cases you can use
[
yii2-swiftmailer
](
https://github.com/yiisoft/yii2/tree/master/extensions/swiftmailer
)
official extension.
Configuration
-------------
Mail component configuration depends on the extension you have chosen.
In general your application configuration should look like:
```
php
return
[
//....
'components'
=>
[
'mail'
=>
[
'class'
=>
'yii\swiftmailer\Mailer'
,
],
],
];
```
Basic usage
-----------
Once 'mail' component is configured, you can use the following code to send an email message:
```
php
Yii
::
$app
->
mail
->
compose
()
->
setFrom
(
'from@domain.com'
)
->
setTo
(
'to@domain.com'
)
->
setSubject
(
'Message subject'
)
->
setTextBody
(
'Plain text content'
)
->
setHtmlBody
(
'<b>HTML content</b>'
)
->
send
();
```
In above example method
`compose()`
creates an instance of the mail message, which then is populated and sent.
You may put more complex logic in this process if needed:
```
php
$message
=
Yii
::
$app
->
mail
->
compose
();
if
(
Yii
::
$app
->
user
->
isGuest
)
{
$message
->
setFrom
(
'from@domain.com'
)
}
else
{
$message
->
setFrom
(
Yii
::
$app
->
user
->
identity
->
email
)
}
$message
->
setTo
(
Yii
::
$app
->
params
[
'adminEmail'
])
->
setSubject
(
'Message subject'
)
->
setTextBody
(
'Plain text content'
)
->
send
();
```
> Note: each 'mail' extension comes in 2 major classes: 'Mailer' and 'Message'. 'Mailer' always knows
the class name and specific of the 'Message'. Do not attempt ot instantiate 'Message' object directly -
always use
`compose()`
method for it.
You may also send several messages at once:
```
php
$messages
=
[];
foreach
(
$users
as
$user
)
{
$messages
[]
=
Yii
::
$app
->
mail
->
compose
()
// ...
->
setTo
(
$user
->
email
);
}
Yii
::
$app
->
mail
->
sendMultiple
(
$messages
);
```
Some particular mail extensions may benefit from this approach, using single network message etc.
Composing mail content
----------------------
Yii allows composition of the actual mail messages content via special view files.
By default these files should be located at '@app/mail' path.
Example mail view file content:
```
php
<?php
use
yii\helpers\Html
;
use
yii\helpers\Url
;
/**
* @var \yii\web\View $this view component instance
* @var \yii\mail\BaseMessage $message instance of newly created mail message
*/
?>
<h2>
This message allows you to visit out site home page by one click
</h2>
<?=
Html
::
a
(
'Go to home page'
,
Url
::
home
(
'http'
))
?>
```
In order to compose message content via view file simply pass view name to the
`compose()`
method:
```
php
Yii
::
$app
->
mail
->
compose
(
'homelink'
)
// message body becomes a view rendering result here
->
setFrom
(
'from@domain.com'
)
->
setTo
(
'to@domain.com'
)
->
setSubject
(
'Message subject'
)
->
send
();
```
You may pass additional view parameters to
`compose()`
method, which will be available inside the view files:
```
php
Yii
::
$app
->
mail
->
compose
(
'greetings'
,
[
'user'
=>
Yii
::
$app
->
user
->
identity
,
'advertisement'
=>
$adContent
,
]);
```
File attachment
---------------
Embed images
------------
Creating your own mail extension
--------------------------------
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment