PHPMailer extension to send emails via Sendgrid (or save to file).
PHPMailerSendGrid is available on Packagist (using semantic versioning), and installation via Composer is the recommended way to install PHPMailer. Just add this line to your composer.json
file:
"rodrigoq/phpmailersendgrid": "^1.0"
or run
composer require rodrigoq/phpmailersendgrid
This project depends on
- PHPMailer
- sendgrid-php
- The Sendgrid service
<?php
// Import PHPMailerSendGrid classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailerSendGrid;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require '../vendor/autoload.php';
$mail = new PHPMailerSendGrid(true); // Passing `true` enables exceptions
try {
//SendGrid settings
$mail->isSendGrid(); // Set mailer to use SendGrid
// Set the SendGrid API Key, to do it securely check:
// https://github.com/sendgrid/sendgrid-php/blob/master/README.md
$mail->SendGridApiKey = ''; // SendGrid API Key.
//Uncomment to save email to file
// $mail->isFile();
// $mail->EmailFilePath = '/var/log/email/';
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients.';
$mail->send();
echo 'Message has been sent.';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
This software is distributed under the LGPL 3.0 license. Please read LICENSE for information on the software availability and distribution.