tony13tv / silverstripe-sendgrid
Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid
Installs: 281
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Type:silverstripe-module
Requires
- php: >=5.3
- mashape/unirest-php: ^1.2.1
- swiftmailer/swiftmailer: ~5.0.1
Requires (Dev)
- phpunit/phpunit: 3.7.*
- vlucas/phpdotenv: 1.0.2
This package is auto-updated.
Last update: 2024-10-27 04:43:42 UTC
README
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('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);
Installation
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';
Alternative: Install source from GitHub
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();
Optional
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 APIs
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.
Example App
There is a sendgrid-php-example app to help jumpstart your development.
Usage
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('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)
$sendgrid->web->send($mail);
Or
$sendgrid->smtp->send($mail);
addTo
You can add one or multiple TO addresses using addTo
.
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> addTo('another@another.com'); $sendgrid->web->send($mail);
setTos
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("foo@bar.com", "another@another.com", "other@other.com"); $mail->setTos($emails); $sendgrid->web->send($mail);
getTos
Sometimes you might find yourself wanting to list the currently set Tos. You can do that with getTos
.
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com'); $mail->getTos();
removeTo
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('foo@bar.com'); $mail->removeTo('foo@bar.com');
setFrom
$mail = new SendGrid\Email(); $mail->setFrom('foo@bar.com'); $sendgrid->web->send($mail);
setFromName
$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);
setReplyTo
$mail = new SendGrid\Email(); $mail->setReplyTo('foo@bar.com'); $sendgrid->web->send($mail);
addCc
$mail = new SendGrid\Email(); $mail->addCc('foo@bar.com'); $sendgrid->web->send($mail);
Bcc
Use multiple addTo
s as a superior alternative to setBcc
.
$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.
$mail = new SendGrid\Email(); $mail->addBcc('foo@bar.com'); $sendgrid->web->send($mail);
setSubject
$mail = new SendGrid\Email(); $mail->setSubject('This is a subject'); $sendgrid->web->send($mail);
setText
$mail = new SendGrid\Email(); $mail->setText('This is some text'); $sendgrid->web->send($mail);
setHtml
$mail = new SendGrid\Email(); $mail->setHtml('<h1>This is an html email</h1>'); $sendgrid->web->send($mail);
Port and Hostname
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
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('foo@bar.com')-> ... addCategory("Category 1")-> addCategory("Category 2");
Attachments
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('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.
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.
From-Name and Reply-To
There are two handy helper methods for setting the From-Name and Reply-To for a message
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> setReplyTo('someone.else@example.com')-> setFromName('John Doe')-> ...
Substitutions
Substitutions can be used to customize multi-recipient emails, and tailor them for the user
$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
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('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
Unique Arguments are used for tracking purposes
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... addUniqueArgument("Customer", "Someone")-> addUniqueArgument("location", "Somewhere");
Filter Settings
Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
$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
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('foo@bar.com')-> ... addSmtpapiHeader("category", "My New Category");
Message Headers
You can add standard email message headers as necessary.
$mail = new SendGrid\Email(); $mail->addTo('foo@bar.com')-> ... addMessageHeader('X-Sent-Using', 'SendGrid-API')-> addMessageHeader('X-Transport', 'web');
Sending to 1,000s of emails in one batch
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("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("<h1>Hey %name%, we have an email for you</h1>"); $result = $sendgrid->smtp->send($email);
Ignoring SSL certificate verification
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);
Contributing
- 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
Running Tests
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).