Skip to content

Instantly share code, notes, and snippets.

@JAMEMSU
Created February 23, 2020 10:52
Show Gist options
  • Save JAMEMSU/871b6e4a81b0ff30f14d961e6251146f to your computer and use it in GitHub Desktop.
Save JAMEMSU/871b6e4a81b0ff30f14d961e6251146f to your computer and use it in GitHub Desktop.

Revisions

  1. JAMEMSU created this gist Feb 23, 2020.
    45 changes: 45 additions & 0 deletions sendgrid-email-list.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    require 'vendor/autoload.php';

    use SendGrid\Mail\Personalization;
    use SendGrid\Mail\To;


    // Declare a new SendGrid Mail object
    $email = new \SendGrid\Mail\Mail();

    // Define the primary email
    $email->setFrom("[email protected]", "Your Name");
    $email->setSubject("Group Email to Twilio Subscribers");

    // Define the additional email addresses
    $email_addresses = [
    "[email protected]","[email protected]",
    ];

    foreach ( $email_addresses as $email_address ) {

    $personalization = new Personalization();
    $personalization->addTo( new To( $email_address ) );

    $email->addPersonalization( $personalization );
    }


    $email->addContent("text/plain", "Sending bulk emails is easy with SendGrid");
    $email->addContent("text/html", "Sending bulk emails is easy with SendGrid");

    $sendgrid = new \SendGrid('API_KEY');

    try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
    echo "email sent!\n";
    } catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
    }

    ?>