Skip to main content
Version: 4.x

Sending mail in BowPHP

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 email function

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)
ParameterType
viewString The recipient's email
dataArray The information to pass to the view
callableClosure 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 = [])
ParameterType
toString The recipient's email
subjectString The email subject
messageString The message to send
headersArray 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.