2017 © Pedro Peláez
 

silverstripe-module silverstripe-sendgrid

Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid

image

tony13tv/silverstripe-sendgrid

Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid

  • Monday, October 3, 2016
  • by tony13tv
  • Repository
  • 1 Watchers
  • 1 Stars
  • 268 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 1 % Grown

The README.md

Sendgrid-php

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)

BuildStatus Latest Stable Version, (*3)

$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., (*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';

Alternative: Install source from GitHub

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();

Optional

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 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/., (*9)

This library implements a common interface to make it very easy to use either API., (*10)

Example App

There is a sendgrid-php-example app to help jumpstart your development., (*11)

Usage

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);

addTo

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);

setTos

If you prefer, you can add multiple TO addresses as an array using the setTos method. This will unset any previous addTos 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);

getTos

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();

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., (*19)

$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 addTos 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);

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., (*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

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

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 addTos. 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 addTos because setBcc is not supported with attachments. This is by design., (*29)

So just remember, when thinking 'bcc', instead use multiple addTos., (*30)

From-Name and Reply-To

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

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

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

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

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

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");

Message Headers

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');

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., (*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);

Ignoring SSL certificate verification

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);

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Running Tests

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

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.

The Versions

03/10 2016

dev-master

9999999-dev http://github.com/sendgrid/sendgrid-php

Sendgrid integration with Silverstripe Forked from sendgrid/sendgrid

  Sources   Download

MIT

The Requires

 

The Development Requires

email grid silverstripe sendgrid send ss3