GithubHelp home page GithubHelp logo

kunal732 / sendgrid-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sendgrid/sendgrid-php

0.0 3.0 0.0 854 KB

SendGrid (http://sendgrid.com) PHP helper library.

Home Page: http://sendgrid.github.io/sendgrid-php/

sendgrid-php's Introduction

Sendgrid-php

This library allows you to quickly and easily send emails through SendGrid using PHP.

Important: This library requires PHP 5.3 or higher.

BuildStatus

$sendgrid = new SendGrid('username', 'password');
$mail     = new SendGrid\Mail();
$mail->addTo('[email protected]')->
       addTo('[email protected]')->
       setFrom('[email protected]')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('<strong>Hello World!</strong>');

$sendgrid->web->send($mail);

Installation

The following recommended installation requires composer. If you are unfamiliar with composer see the composer installation instructions.

Add the following to your composer.json file:

{  
  "require": {
    "sendgrid/sendgrid": "1.0.9"
  }
}

Install sendgrid-php and its dependencies:

composer install

Load sendgrid-php and its dependencies into your project:

require 'vendor/autoload.php';

Alternative Installation

If you choose not to use composer you can do the following alternative installation using git:

git clone https://github.com/sendgrid/sendgrid-php.git

Then require the autoloader from your php script:

require '../path/to/sendgrid-php/SendGrid_loader.php';

Finally, install SwiftMailer using pear.

pear channel-discover pear.swiftmailer.org
pear install swift/swift

IMPORTANT: If you do not plan on sending using SMTP, you can skip installation of swiftmailer. In which case your send line of code will look like this.

$sendgrid->web->send($mail);

Instead of this.

$sendgrid->smtp->send($mail);

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.

Pre-Usage

Before we begin using the library, its important to understand a few things about the library architecture...

  • The SendGrid Mail object is the means of setting mail data. In general, data can be set in three ways for most elements:
    1. set - reset the data, and initialize it to the given element. This will destroy previous data
    2. set (List) - for array based elements, we provide a way of passing the entire array in at once. This will also destroy previous data. 3. add - append data to the list of elements.
  • Sending an email is as simple as :
    1. Creating a SendGrid Instance
    2. Creating a SendGrid Mail object, and setting its data
    3. Sending the mail using either SMTP API or Web API.

Usage

To begin using this library, initialize the SendGrid object with your SendGrid credentials.

$sendgrid = new SendGrid('username', 'password');

Create a new SendGrid Mail object and add your message details.

$mail = new SendGrid\Mail();
$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);

addTo

You can add one or multiple TO addresses using addTo.

$mail = new SendGrid\Mail();
$mail->addTo('[email protected]')->
       addTo('[email protected]');
$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.

$mail   = new SendGrid\Mail();
$emails = array("[email protected]", "[email protected]", "[email protected]");
$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\Mail();
$mail->addTo('[email protected]');
$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\Mail();
$mail->addTo('[email protected]');
$mail->removeTo('[email protected]');

setFrom

$mail   = new SendGrid\Mail();
$mail->setFrom('[email protected]');
$sendgrid->web->send($mail);

setFromName

$mail   = new SendGrid\Mail();
$mail->setFrom('[email protected]');
$mail->setFromName('Foo Bar');
$mail->setFrom('[email protected]');
$mail->setFromName('Other Guy');
$sendgrid->web->send($mail);

setReplyTo

$mail   = new SendGrid\Mail();
$mail->setReplyTo('[email protected]');
$sendgrid->web->send($mail);

addCc

$mail   = new SendGrid\Mail();
$mail->addCc('[email protected]');
$sendgrid->web->send($mail);

Bcc

Use multiple addTos as a superior alternative to setBcc.

$mail = new SendGrid\Mail();
$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\Mail();
$mail->addBcc('[email protected]');
$sendgrid->web->send($mail);

setSubject

$mail   = new SendGrid\Mail();
$mail->setSubject('This is a subject');
$sendgrid->web->send($mail);

setText

$mail   = new SendGrid\Mail();
$mail->setText('This is some text');
$sendgrid->web->send($mail);

setHtml

$mail   = new SendGrid\Mail();
$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\Mail();
$mail->addTo('[email protected]')->
       ...
       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\Mail();
$mail->addTo('[email protected]')->
       ...
       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.

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.

So just remember, when thinking 'bcc', instead use multiple addTos.

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\Mail();
$mail->addTo('[email protected]')->
       setReplyTo('[email protected]')->
       setFromName('John Doe')->
       ...

Substitutions

Substitutions can be used to customize multi-recipient emails, and tailor them for the user

$mail = new SendGrid\Mail();
$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

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\Mail();
$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

Unique Arguments are used for tracking purposes

$mail = new SendGrid\Mail();
$mail->addTo('[email protected]')->
       ...
       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\Mail();
$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>");

Headers

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\Mail();
$mail->addTo('[email protected]')->
       ...
       addHeader("category", "My New Category");

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

$recipients = array("[email protected]", "[email protected]", "[email protected]");
$names      = array("Alpha", "Beta", "Zeta");

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

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:

composer update --dev
vendor/bin/phpunit Test/
```

or if you already have PHPUnit installed globally.

```bash
phpunit Test/
```

## 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).

sendgrid-php's People

Contributors

bpneal avatar cjbuchmann avatar f21 avatar hellogerard avatar kander-zz avatar markwilson avatar martyndavies avatar mattketmo avatar motdotla avatar nquinlan avatar sander-bol avatar theycallmeswift avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.