PHP code example of godpod / sendgrid

1. Go to this page and download the library: Download godpod/sendgrid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

godpod / sendgrid example snippets


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

$sendgrid->send($email);



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

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       addTo('[email protected]');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$emails = array("[email protected]", "[email protected]", "[email protected]");
$email->setTos($emails);
$sendgrid->send($email);

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

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

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       setReplyTo('[email protected]')->
       setFromName('John Doe')->
       ...

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

$email   = new SendGrid\Email();
$email->setCc('[email protected]');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$emails = array("[email protected]", "[email protected]", "[email protected]");
$email->setCcs($emails);
$sendgrid->send($email);

$email->removeCc('[email protected]');

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       addTo('[email protected]')->
       addTo('[email protected]')->
       ...

$email   = new SendGrid\Email();
$email->addBcc('[email protected]');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setBcc('[email protected]');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$emails = array("[email protected]", "[email protected]", "[email protected]");
$email->setBccs($emails);
$sendgrid->send($email);

$email->removeBcc('[email protected]');

$email   = new SendGrid\Email();
$email->setSubject('This is a subject');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setText('This is some text');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setHtml('<h1>This is an html email</h1>');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setDate('Wed, 17 Dec 2014 19:21:16 +0000');
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setSendAt(1409348513);
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->setSendEachAt(array(1409348513, 1409348514, 1409348515));
$sendgrid->send($email);

$email   = new SendGrid\Email();
$email->addSendEachAt(1409348513);
$email->addSendEachAt(1409348514);
$email->addSendEachAt(1409348515);
$sendgrid->send($email);

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addCategory("Category 1")->
       addCategory("Category 2");

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       setCategory("Category 1");

$email = new SendGrid\Email();
$categories = array("Category 1", "Category 2", "Category 3");
$email->setCategories($categories);

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       removeCategory("Category 1");

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addAttachment("../path/to/file.txt");

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       setAttachment("../path/to/file.txt");

$email = new SendGrid\Email();
$attachments = array("../path/to/file1.txt", "../path/to/file2.txt");
$email->addTo('[email protected]')->
       ...
       setAttachments($attachments);

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addAttachment("../path/to/file.txt");
$email->removeAttachment("../path/to/file.txt");

$email = new SendGrid\Email();
$email->addTo('[email protected]')
      ->setHtml('<div>Our logo:<img src="cid:file-cid"></div>')
      ->addAttachment("../path/to/file.txt", "super_file.txt", "file-cid");

$email = new SendGrid\Email();
$email->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'));

$email = new SendGrid\Email();
$email->addTos(array('[email protected]', '[email protected]', '[email protected]'))
    ->setSubject('%subject%')
    ->addSubstitution('%subject%', array('Subject to John', 'Subject to Harry', 'Subject to Bob'))
    ...;

$email = new SendGrid\Email();
$email->addTos(array('[email protected]', '[email protected]', '[email protected]'))
    ->setSubject('%subject%')
    ->setSubstitutions(array('%name%' => array('John', 'Harry', 'Bob') , '%subject%' => array('Subject to John', 'Subject to Harry', 'Subject to Bob')))
    ...;

$email = new SendGrid\Email();
$email->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");

$email = new SendGrid\Email();
$email->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%"))->
       setSections(array("%office%" => "an office", "%home%" => "your house"));

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addUniqueArg("Customer", "Someone")->
       addUniqueArg("location", "Somewhere")->

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       setUniqueArgs(array('cow' => 'chicken'));

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addFilter("gravatar", "enable", 1)->
       addFilter("footer", "enable", 1)->
       addFilter("footer", "text/plain", "Here is a plain text footer")->
       addFilter("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>");

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       setFilters(array("gravatar" => array("settings" => array("enable" => 1))))

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addHeader('X-Sent-Using', 'SendGrid-API')->
       addHeader('X-Transport', 'web');

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       setHeaders(array('X-Sent-Using' => 'SendGrid-API', 'X-Transport' => 'web'));

$email = new SendGrid\Email();
$email->addTo('[email protected]')->
       ...
       addHeader('X-Sent-Using', 'SendGrid-API')->
       addHeader('X-Transport', 'web');
$email->removeHeader('X-Transport');

$options = array(
  'turn_off_ssl_verification' => false,
  'protocol' => 'https',
  'host' => 'api.sendgrid.com',
  'endpoint' => '/api/mail.send.json',
  'port' => null,
  'url' => null
);
$sendgrid = new SendGrid('username', 'password', $options);

$sendgrid = new SendGrid('username', 'password', array( 'protocol' => 'http', 'host' => 'sendgrid.org', 'endpoint' => '/send', 'port' => '80' ));

$sendgrid = new SendGrid('username', 'password', array( 'url' => 'http://sendgrid.org:80/send'));

$sendgrid   = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD,  array("turn_off_ssl_verification" => true));

$sendgrid   = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD);
$email       = 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->send($email);
bash