fix php 5.6 in docker dev env (#1740)
[openemr.git] / vendor / zendframework / zend-mail / doc / book / transport / intro.md
blob8555d36039cdb72f9343bc0f2bfb81aedd16cd64
1 # Transports
3 Transports take care of the actual delivery of mail. Typically, you only need to
4 worry about two possibilities: using PHP's native `mail()` functionality, which
5 uses system resources to deliver mail, or using the SMTP protocol for delivering
6 mail via a remote server. Zend Framework also includes a "File" transport, which
7 creates a mail file for each message sent; these can later be introspected as
8 logs or consumed for the purposes of sending via an alternate transport
9 mechanism later.
11 The `Zend\Mail\Transport` interface defines exactly one method, `send()`. This
12 method accepts a `Zend\Mail\Message` instance, which it then introspects and
13 serializes in order to send.
15 ## Quick Start
17 Using a mail transport involves instantiating it, optionally configuring it, and
18 then passing a message to it.
20 ### Sendmail Transport Usage
22 ```php
23 use Zend\Mail\Message;
24 use Zend\Mail\Transport\Sendmail as SendmailTransport;
26 $message = new Message();
27 $message->addTo('matthew@example.org');
28 $message->addFrom('ralph@example.org');
29 $message->setSubject('Greetings and Salutations!');
30 $message->setBody("Sorry, I'm going to be late today!");
32 $transport = new SendmailTransport();
33 $transport->send($message);
34 ```
36 ### SMTP Transport Usage
38 ```php
39 use Zend\Mail\Message;
40 use Zend\Mail\Transport\Smtp as SmtpTransport;
41 use Zend\Mail\Transport\SmtpOptions;
43 $message = new Message();
44 $message->addTo('matthew@example.org');
45 $message->addFrom('ralph@example.org');
46 $message->setSubject('Greetings and Salutations!');
47 $message->setBody("Sorry, I'm going to be late today!");
49 // Setup SMTP transport using LOGIN authentication
50 $transport = new SmtpTransport();
51 $options   = new SmtpOptions([
52     'name'              => 'localhost.localdomain',
53     'host'              => '127.0.0.1',
54     'connection_class'  => 'login',
55     'connection_config' => [
56         'username' => 'user',
57         'password' => 'pass',
58     ],
59 ]);
60 $transport->setOptions($options);
61 $transport->send($message);
62 ```
64 ### File Transport Usage
66 ```php
67 use Zend\Mail\Message;
68 use Zend\Mail\Transport\File as FileTransport;
69 use Zend\Mail\Transport\FileOptions;
70 use Zend\Math\Rand;
72 $message = new Message();
73 $message->addTo('matthew@example.org');
74 $message->addFrom('ralph@example.org');
75 $message->setSubject('Greetings and Salutations!');
76 $message->setBody("Sorry, I'm going to be late today!");
78 // Setup File transport
79 $transport = new FileTransport();
80 $options   = new FileOptions([
81     'path'     => 'data/mail/',
82     'callback' => function (FileTransport $transport) {
83         return sprintf(
84             'Message_%f_%s.txt',
85             microtime(true),
86             Rand::getString(8)
87         );
88     },
89 ]);
90 $transport->setOptions($options);
91 $transport->send($message);
92 ```
94 ### InMemory Transport Usage
96 ```php
97 use Zend\Mail\Message;
98 use Zend\Mail\Transport\InMemory as InMemoryTransport;
100 $message = new Message();
101 $message->addTo('matthew@example.org');
102 $message->addFrom('ralph@example.org');
103 $message->setSubject('Greetings and Salutations!');
104 $message->setBody("Sorry, I'm going to be late today!");
106 // Setup InMemory transport
107 $transport = new InMemoryTransport();
108 $transport->send($message);
110 // Verify the message:
111 $received = $transport->getLastMessage();
114 The `InMemory` transport is primarily of interest when in development or when
115 testing.
117 ### Migration from 2.0-2.3 to 2.4+
119 Version 2.4 adds support for PHP 7. In PHP 7, `null` is a reserved keyword,
120 which required renaming the `Null` transport. If you were using the `Null`
121 transport directly previously, you will now receive an `E_USER_DEPRECATED`
122 notice on instantiation. Please update your code to refer to the `InMemory`
123 class instead.
125 Users pulling their `Null` transport instance from the transport factory
126 (`Zend\Mail\Transport\Factory`) receive an `InMemory` instance instead starting
127 in 2.4.0.
129 ## Configuration Options
131 Configuration options are per transport. Please follow the links below for
132 transport-specific options.
134 - [SMTP Transport Options](smtp-options.md)
135 - [File Transport Options](file-options.md)
137 ## Available Methods
139 ### send
141 ```php
142 send(Zend\Mail\Message $message) : void
145 Send a mail message.