Skip to content

Latest commit

 

History

History
154 lines (104 loc) · 3.23 KB

mail.md

File metadata and controls

154 lines (104 loc) · 3.23 KB

Title: Mail Description: Custom email system.


!!

Getting Started

To WordPress send email WordPress provides the wp_mail() function. With new service \TypeRocket\Services\MailerService loaded in your app.servies config file you can extend the WordPress email system with additional mail drivers.

Configuration

To configure your mail service to a specific mail driver see the app/mail.php config file. By default, TypeRocket uses its wp mail driver to emulate the wp_mail() function. There are three mail drivers included with TypeRocket:

You can set your default mail driver in the mail.default config setting. The default driver will then be used by the wp_mail() function and the TypeRocket \TypeRocketPro\Utility\Mail class.

wp_mail( $to, $subject, $message);

\TypeRocketPro\Utility\Mail::new()
    ->to($to)->subject($subject)
    ->message($message)
    ->send();

Mail Class

The fluent TypeRocket Mail class enables you to send mail in a more intuitive way. To start working with the \TypeRocketPro\Utility\Mail class create an instance of it.

$mail = \TypeRocketPro\Utility\Mail::new();

To

$mail->to('[email protected]');

echo $mail->to();
// Outputs: [email protected]

$mail->to(['[email protected]', '[email protected]']);

echo $mail->to();
// Outputs: [email protected], [email protected]

From

$mail->from('[email protected]');

echo $mail->from();
// Outputs: [email protected]

$mail->from(['[email protected]', '[email protected]']);

echo $mail->from();
// Outputs: [email protected], [email protected]

Reply To

$mail->replyTo('[email protected]');

echo $mail->replyTo();
// Outputs: [email protected]

Subject

$mail->subject('Open this email');

echo $mail->subject();
// Outputs: Open this email

Message

By default, the mail is sent as HTML.

$mail->message('<p>Your message!<p>');

echo $mail->message();
// Outputs: <p>Your message!<p>

To send a message as plain text:

$mail->asText();
// or,
$mail->header('Content-Type', 'text/plain; charset=UTF-8');

You can also send views as your email message.

<?php // resources/views/mail/simple.php ?>
<p>Your view message!<p>
// You will need a view
$mail->view('mail.simple');

echo $mail->message();
// Outputs: <p>Your view message!<p>

Headers

$mail->header('From', '[email protected]');
$mail->headers([
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
]);

Attachments

$attachments = [
    WP_CONTENT_DIR . '/uploads/2020/11/image.png',
    WP_CONTENT_DIR . '/uploads/2020/11/image2.png',
];

$mail->attachments($attachments);

Driver

If you do not want to use the default mail driver your can specify a different one.

$mail->driver(new TypeRocketPro\Utility\Mailers\MailgunMailDriver);

Send

Once you are ready to send use the send() method.

$mail->send();