|
| 1 | +# Introduction to Zend\\Mail |
| 2 | + |
| 3 | +## Getting started |
| 4 | + |
| 5 | +`Zend\Mail` provides generalized functionality to compose and send both text and *MIME*-compliant |
| 6 | +multipart email messages. Mail can be sent with `Zend\Mail` via the `Mail\Transport\Sendmail`, |
| 7 | +`Mail\Transport\Smtp` or the `Mail\Transport\File` transport. Of course, you can also implement your |
| 8 | +own transport by implementing the `Mail\Transport\TransportInterface`. |
| 9 | + |
| 10 | +### Simple email with Zend\\Mail |
| 11 | + |
| 12 | +A simple email consists of one or more recipients, a subject, a body and a sender. To send such a |
| 13 | +mail using `Zend\Mail\Transport\Sendmail`, do the following: |
| 14 | + |
| 15 | +```php |
| 16 | +use Zend\Mail; |
| 17 | + |
| 18 | +$mail = new Mail\Message(); |
| 19 | +$mail->setBody('This is the text of the email.'); |
| 20 | +$mail->setFrom(' [email protected]', 'Sender\'s name'); |
| 21 | +$mail->addTo(' [email protected]', 'Name of recipient'); |
| 22 | +$mail->setSubject('TestSubject'); |
| 23 | + |
| 24 | +$transport = new Mail\Transport\Sendmail(); |
| 25 | +$transport->send($mail); |
| 26 | +``` |
| 27 | + |
| 28 | +> ## Note |
| 29 | +#### Minimum definitions |
| 30 | +In order to send an email using `Zend\Mail` you have to specify at least one recipient as well as a |
| 31 | +message body. Please note that each Transport may require additional parameters to be set. |
| 32 | + |
| 33 | +For most mail attributes there are "get" methods to read the information stored in the message |
| 34 | +object. for further details, please refer to the *API* documentation. |
| 35 | + |
| 36 | +You also can use most methods of the `Mail\Message` object with a convenient fluent interface. |
| 37 | + |
| 38 | +```php |
| 39 | +use Zend\Mail; |
| 40 | + |
| 41 | +$mail = new Mail\Message(); |
| 42 | +$mail->setBody('This is the text of the mail.') |
| 43 | + ->setFrom(' [email protected]', 'Some Sender') |
| 44 | + ->addTo(' [email protected]', 'Some Recipient') |
| 45 | + ->setSubject('TestSubject'); |
| 46 | +``` |
| 47 | + |
| 48 | +## Configuring the default sendmail transport |
| 49 | + |
| 50 | +The most simple to use transport is the `Mail\Transport\Sendmail` transport class. It is essentially |
| 51 | +a wrapper to the *PHP* [mail()](http://php.net/mail) function. If you wish to pass additional |
| 52 | +parameters to the [mail()](http://php.net/mail) function, simply create a new transport instance and |
| 53 | +pass your parameters to the constructor. |
| 54 | + |
| 55 | +### Passing additional parameters |
| 56 | + |
| 57 | +This example shows how to change the Return-Path of the [mail()](http://php.net/mail) function. |
| 58 | + |
| 59 | +```php |
| 60 | +use Zend\Mail; |
| 61 | + |
| 62 | +$mail = new Mail\Message(); |
| 63 | +$mail->setBody('This is the text of the email.'); |
| 64 | +$mail->setFrom(' [email protected]', 'Dolf'); |
| 65 | +$mail->addTo(' [email protected]', 'Matthew'); |
| 66 | +$mail->setSubject('TestSubject'); |
| 67 | + |
| 68 | +$transport = new Mail\Transport\Sendmail(' [email protected]'); |
| 69 | +$transport->send($mail); |
| 70 | +``` |
| 71 | + |
| 72 | +> ## Note |
| 73 | +#### Safe mode restrictions |
| 74 | +Supplying additional parameters to the transport will cause the [mail()](http://php.net/mail) |
| 75 | +function to fail if *PHP* is running in safe mode. |
| 76 | + |
| 77 | +> ## Note |
| 78 | +#### Choosing your transport wisely |
| 79 | +Although the sendmail transport is the transport that requires only minimal configuration, it may |
| 80 | +not be suitable for your production environment. This is because emails sent using the sendmail |
| 81 | +transport will be more often delivered to SPAM-boxes. This can partly be remedied by using the |
| 82 | +\[SMTP Transport\](zend.mail.transport.quick-start.smtp-usage) combined with an SMTP server that has |
| 83 | +an overall good reputation. Additionally, techniques such as SPF and DKIM may be employed to ensure |
| 84 | +even more email messages are delivered as should. |
| 85 | + |
| 86 | +> ## Warning |
| 87 | +#### Sendmail Transport and Windows |
| 88 | +As the *PHP* manual states the `mail()` function has different behaviour on Windows and on \*nix |
| 89 | +based systems. Using the Sendmail Transport on Windows will not work in combination with `addBcc()`. |
| 90 | +The `mail()` function will sent to the BCC recipient such that all the other recipients can see him |
| 91 | +as recipient! |
| 92 | +Therefore if you want to use BCC on a windows server, use the SMTP transport for sending! |
0 commit comments