dev-master
9999999-dev http://github.com/sendgrid/sendgrid-phpSendgrid integration with Silverstripe Forked from sendgrid/sendgrid
MIT
The Requires
The Development Requires
email grid silverstripe sendgrid send ss3
Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid
This library allows you to quickly and easily send emails through SendGrid using PHP., (*1)
Important: This library requires PHP 5.3 or higher., (*2)
$sendgrid = new SendGrid('username', 'password'); $email = new SendGrid\Email(); $email->addTo('foo@bar.com')-> addTo('dude@bar.com')-> setFrom('me@bar.com')-> 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., (*4)
{ "minimum-stability" : "dev", "require": { "sendgrid/sendgrid": "1.1.7" } }
Then at the top of your PHP script require the autoloader:, (*5)
require 'vendor/autoload.php';
If you don't want to use Composer, you can install from source., (*6)
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:, (*7)
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., (*8)
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/., (*9)
This library implements a common interface to make it very easy to use either API., (*10)
There is a sendgrid-php-example app to help jumpstart your development., (*11)
To begin using this library, initialize the SendGrid object with your SendGrid credentials., (*12)
$sendgrid = new SendGrid('username', 'password');
Create a new SendGrid Email object and add your message details., (*13)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> setFrom('me@bar.com')-> setSubject('Subject goes here')-> setText('Hello World!')-> setHtml('<strong>Hello World!</strong>');
Send it using the API of your choice (Web or SMTP), (*14)
$sendgrid->web->send($mail);
Or, (*15)
$sendgrid->smtp->send($mail);
You can add one or multiple TO addresses using addTo
., (*16)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> addTo('another@another.com'); $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., (*17)
$mail = new SendGrid\Email(); $emails = array("foo@bar.com", "another@another.com", "other@other.com"); $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
., (*18)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com'); $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., (*19)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com'); $mail->removeTo('foo@bar.com');
$mail = new SendGrid\Email(); $mail->setFrom('foo@bar.com'); $sendgrid->web->send($mail);
$mail = new SendGrid\Email(); $mail->setFrom('foo@bar.com'); $mail->setFromName('Foo Bar'); $mail->setFrom('other@example.com'); $mail->setFromName('Other Guy'); $sendgrid->web->send($mail);
$mail = new SendGrid\Email(); $mail->setReplyTo('foo@bar.com'); $sendgrid->web->send($mail);
$mail = new SendGrid\Email(); $mail->addCc('foo@bar.com'); $sendgrid->web->send($mail);
Use multiple addTo
s as a superior alternative to setBcc
., (*20)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> addTo('someotheraddress@bar.com')-> addTo('another@another.com')-> ...
But if you do still have a need for Bcc you can do the following., (*21)
$mail = new SendGrid\Email(); $mail->addBcc('foo@bar.com'); $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., (*22)
$sendgrid->smtp->setPort('1234567'); $sendgrid->smtp->setHostname('smtp.dude.com');
This is useful if you are using a local relay, as documented in here., (*23)
Categories are used to group email statistics provided by SendGrid., (*24)
To use a category, simply set the category name. Note: there is a maximum of 10 categories per email., (*25)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... addCategory("Category 1")-> addCategory("Category 2");
Attachments are currently file based only, with future plans for an in memory implementation as well., (*26)
File attachments are limited to 7 MB per file., (*27)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... 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., (*28)
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., (*29)
So just remember, when thinking 'bcc', instead use multiple addTo
s., (*30)
There are two handy helper methods for setting the From-Name and Reply-To for a message, (*31)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> setReplyTo('someone.else@example.com')-> setFromName('John Doe')-> ...
Substitutions can be used to customize multi-recipient emails, and tailor them for the user, (*32)
$mail = new SendGrid\Email(); $mail->addTo('john@somewhere.com')-> addTo("harry@somewhere.com")-> addTo("Bob@somewhere.com")-> ... 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., (*33)
$mail = new SendGrid\Email(); $mail->addTo('john@somewhere.com')-> addTo("harry@somewhere.com")-> addTo("Bob@somewhere.com")-> ... 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, (*34)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... addUniqueArgument("Customer", "Someone")-> addUniqueArgument("location", "Somewhere");
Filter Settings are used to enable and disable apps, and to pass parameters to those apps., (*35)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... 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., (*36)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... addSmtpapiHeader("category", "My New Category");
You can add standard email message headers as necessary., (*37)
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... 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., (*38)
$sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD); $mail = new SendGrid\Email(); $recipients = array("alpha@mailinator.com", "beta@mailinator.com", "zeta@mailinator.com"); $names = array("Alpha", "Beta", "Zeta"); $email->setFrom("from@mailinator.com")-> setSubject('[sendgrid-php-batch-email]')-> setTos($recipients)-> addSubstitution("%name%", $names)-> setText("Hey %name, we have an email for you")-> setHtml("Hey %name%, we have an email for you
"); $result = $sendgrid->smtp->send($email);
You can optionally ignore verification of SSL certificate when using the Web API., (*39)
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);
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)The existing tests in the test
directory can be run using PHPUnit with the following command:, (*40)
````bash composer update --dev cd test ../vendor/bin/phpunit, (*41)
or if you already have PHPUnit installed globally. ```bash cd test phpunit
Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid
MIT
email grid silverstripe sendgrid send ss3