Sending mail in BowPHP
- Introduction
- Configuration
- Usage
- The send method
- Message API
addHeader, adding custom headersto, sets the recipienttoList, adding multiple recipientsaddFile, lets you add an attachmentsubject, sets the email subjectfrom, sets the email sendertoHtml, sets the content type to text/htmltoText, sets the message bodyaddBcc, adds the Carbon Copy headeraddCc, adds the Carbon Copy headeraddReplyTo, adds the Reply-To headeraddReturnPath, adds the Return-Path header
- The raw method
Introductionβ
Sending emails in an application is a common task. BowPHP includes an easy-to-use email-sending system. You can use this system through several pilots commonly called drivers:
- SMTP - Direct sending via an SMTP server
- SES - You can also use SES
- MAIL - The system will use PHP's native
emailfunction
Configurationβ
You will find the configuration in the config/mail.php file, which is relatively simple; all the options are commented.
Install the additional packages
- ses "aws/aws-sdk-php"
Usageβ
To use the service, you must call the Bow\Mail\Mail::class class. There are two static methods for sending mail: email and raw_email.
The send methodβ
email lets you send emails based on a view in the application.
send prototypeβ
email($view, array $data, callable $callable)
| Parameter | Type |
|---|---|
| view | String The recipient's email |
| data | Array The information to pass to the view |
| callable | Closure The message builder |
$callable lets you build the message and takes as a parameter an instance of Bow\Mail\Message, which lets you add the recipient, the subject, the attachments, and the sender.
Example usage:
Consider the view email-view.tintin.php, which contains the following information:
Bonjour {{ $name }},
Nous vous informons que vous compte viens d'Γͺtre crΓ©diter de 100.000.000 F.
Cordialement,
Now let's write the code that sends the mail:
use Bow\Mail\Mail;
use Bow\Mail\Message;
$data = [
'name' => 'Franck',
];
Mail::send('email-view', $data, function (Message $message) {
$message->to('info@exemple.com');
$message->subject("Paiement !");
$message->from("Bow <info@exemple.com>");
});
Message APIβ
BowPHP will therefore pass an instance of Bow\Mail\Message, which will let you add extra information to your email, notably the recipient.
addHeader, adding custom headersβ
$message->addHeader(string $key, string $value)
to, sets the recipientβ
$message->to(string $to, string $name = null)
toList, adding multiple recipientsβ
$message->toList(array $list_desc)
addFile, lets you add an attachmentβ
$message->addFile(string $file)
subject, sets the email subjectβ
$message->subject(string $subject)
from, sets the email senderβ
$message->from(string $from, string $name = null)
toHtml, sets the content type to text/htmlβ
$message->html($html)
toText, sets the message bodyβ
$message->text(string $text)
addBcc, adds the Carbon Copy headerβ
$message->addBcc(string $mail, string $name = null)
addCc, adds the Carbon Copy headerβ
$message->addCc(string $mail, string $name = null)
addReplyTo, adds the Reply-To headerβ
$message->addReplyTo(string $mail, $name = null)
addReturnPath, adds the Return-Path headerβ
$message->addReturnPath(string $mail, $name = null)
The raw methodβ
raw, on the other hand, lets you send emails with a raw message.
raw prototypeβ
raw_email($to, $subject, $message, array $headers = [])
| Parameter | Type |
|---|---|
| to | String The recipient's email |
| subject | String The email subject |
| message | String The message to send |
| headers | Array The headers to send, empty by default |
use Bow\Mail\Mail;
$email = "hello@exemple.com";
$subject = "Hello, world";
$message = "C'est juste un email de teste";
Mail::raw($email, $subject, $message);
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.