This library allows you to quickly and easily send emails through SendGrid using PHP.
Important: This library requires PHP 5.3 or higher.
$sendgrid = new SendGrid('username', 'password');
$email = new SendGrid\Email();
$email->addTo('[email protected]')->
addTo('[email protected]')->
setFrom('[email protected]')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->web->send($email);
Add SendGrid to your composer.json
file. If you are not using Composer, you should be. It's an excellent way to manage dependencies in your PHP application.
{
"minimum-stability" : "dev",
"require": {
"sendgrid/sendgrid": "1.1.7"
}
}
Then at the top of your PHP script require the autoloader:
require 'vendor/autoload.php';
If you don't want to use Composer, you can install from source.
git clone https://github.com/Mashape/unirest-php.git
git clone https://github.com/sendgrid/sendgrid-php.git
And include it in your PHP script:
require_once '/path/to/unirest-php/lib/Unirest.php';
require_once '/path/to/sendgrid-php/lib/SendGrid.php';
SendGrid::register_autoloader();
IF using the smtp
option, you need swiftmailer. This is not necessary if using the web API approach.
git clone git://github.com/swiftmailer/swiftmailer.git
ln -s swiftmailer/lib/swift_required.php swift_required.php
SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails. For an explanation of the benefits of each, refer to http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/.
This library implements a common interface to make it very easy to use either API.
There is a sendgrid-php-example app to help jumpstart your development.
To begin using this library, initialize the SendGrid object with your SendGrid credentials.
$sendgrid = new SendGrid('username', 'password');
Create a new SendGrid Email object and add your message details.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
setFrom('[email protected]')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
Send it using the API of your choice (Web or SMTP)
$sendgrid->web->send($mail);
Or
$sendgrid->smtp->send($mail);
You can add one or multiple TO addresses using addTo
.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
addTo('[email protected]');
$sendgrid->web->send($mail);
If you prefer, you can add multiple TO addresses as an array using the setTos
method. This will unset any previous addTo
s you appended.
$mail = new SendGrid\Email();
$emails = array("[email protected]", "[email protected]", "[email protected]");
$mail->setTos($emails);
$sendgrid->web->send($mail);
Sometimes you might find yourself wanting to list the currently set Tos. You can do that with getTos
.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]');
$mail->getTos();
You might also find yourself wanting to remove a single TO from your set list of TOs. You can do that with removeTo
. You can pass a string or regex to the removeTo method.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]');
$mail->removeTo('[email protected]');
$mail = new SendGrid\Email();
$mail->setFrom('[email protected]');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->setFrom('[email protected]');
$mail->setFromName('Foo Bar');
$mail->setFrom('[email protected]');
$mail->setFromName('Other Guy');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->setReplyTo('[email protected]');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->addCc('[email protected]');
$sendgrid->web->send($mail);
Use multiple addTo
s as a superior alternative to setBcc
.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
addTo('[email protected]')->
addTo('[email protected]')->
...
But if you do still have a need for Bcc you can do the following.
$mail = new SendGrid\Email();
$mail->addBcc('[email protected]');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->setSubject('This is a subject');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->setText('This is some text');
$sendgrid->web->send($mail);
$mail = new SendGrid\Email();
$mail->setHtml('<h1>This is an html email</h1>');
$sendgrid->web->send($mail);
For the smtp API you can optionally choose to set the Port and the Hostname.
$sendgrid->smtp->setPort('1234567');
$sendgrid->smtp->setHostname('smtp.dude.com');
This is useful if you are using a local relay, as documented in here.
Categories are used to group email statistics provided by SendGrid.
To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addCategory("Category 1")->
addCategory("Category 2");
Attachments are currently file based only, with future plans for an in memory implementation as well.
File attachments are limited to 7 MB per file.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addAttachment("../path/to/file.txt");
Important Gotcha: setBcc
is not supported with attachments. This is by design. Instead use multiple addTo
s. Each user will receive their own personalized email with that setup, and only see their own email.
Standard setBcc
will hide who the email is addressed to. If you use the multiple addTo, each user will receive a personalized email showing *only their email. This is more friendly and more personal. Additionally, it is a good idea to use multiple addTo
s because setBcc is not supported with attachments. This is by design.
So just remember, when thinking 'bcc', instead use multiple addTo
s.
There are two handy helper methods for setting the From-Name and Reply-To for a message
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
setReplyTo('[email protected]')->
setFromName('John Doe')->
...
Substitutions can be used to customize multi-recipient emails, and tailor them for the user
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
addTo("[email protected]")->
addTo("[email protected]")->
...
setHtml("Hey %name%, we've seen that you've been gone for a while")->
addSubstitution("%name%", array("John", "Harry", "Bob"));
Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
addTo("[email protected]")->
addTo("[email protected]")->
...
setHtml("Hey %name%, you work at %place%")->
addSubstitution("%name%", array("John", "Harry", "Bob"))->
addSubstitution("%place%", array("%office%", "%office%", "%home%"))->
addSection("%office%", "an office")->
addSection("%home%", "your house");
Unique Arguments are used for tracking purposes
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addUniqueArgument("Customer", "Someone")->
addUniqueArgument("location", "Somewhere");
Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addFilterSetting("gravatar", "enable", 1)->
addFilterSetting("footer", "enable", 1)->
addFilterSetting("footer", "text/plain", "Here is a plain text footer")->
addFilterSetting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>");
Smtp API Headers can be used to add existing sendgrid functionality (such as for categories or filters), or custom headers can be added as necessary.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addSmtpapiHeader("category", "My New Category");
You can add standard email message headers as necessary.
$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
...
addMessageHeader('X-Sent-Using', 'SendGrid-API')->
addMessageHeader('X-Transport', 'web');
Sometimes you might want to send 1,000s of emails in one request. You can do that. It is recommended you break each batch up in 1,000 increements. So if you need to send to 5,000 emails, then you'd break this into a loop of 1,000 emails at a time.
$sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD);
$mail = new SendGrid\Email();
$recipients = array("[email protected]", "[email protected]", "[email protected]");
$names = array("Alpha", "Beta", "Zeta");
$email->setFrom("[email protected]")->
setSubject('[sendgrid-php-batch-email]')->
setTos($recipients)->
addSubstitution("%name%", $names)->
setText("Hey %name, we have an email for you")->
setHtml("<h1>Hey %name%, we have an email for you</h1>");
$result = $sendgrid->smtp->send($email);
You can optionally ignore verification of SSL certificate when using the Web API.
var options = array("turn_off_ssl_verification" => true);
$sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD, options);
$email = new SendGrid\Email();
...
$result = $sendgrid->web->send($email);
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
The existing tests in the test
directory can be run using PHPUnit with the following command:
composer update --dev
cd test
../vendor/bin/phpunit
```
or if you already have PHPUnit installed globally.
```bash
cd test
phpunit
```
## Known Issues
* When using the smtp version (which is built on top of swiftmailer), any FROM names with parentheses will have the content of the parentheses stripped out. If this happens please use the web version of the library. Read more about this in [issue #27](https://github.com/sendgrid/sendgrid-php/issues/27).