Jump to content

Sendgrid Assistance


searls03

Recommended Posts

I am working on trying to integrate send grid into my website.  I am a bit newer at this.

I am working on trying to set up batchids and scheduled sending (sendAt).  Scheduling works when there is only one email that it is going to, however It will send right away when there is more than one email.  Can anyone offer any assistance on how to get this properly set up?

require '../sendgrid/vendor/autoload.php'; // If you're using Composer (recommended)

use SendGrid\Mail\From;
use SendGrid\Mail\HtmlContent;
use SendGrid\Mail\Mail;
use SendGrid\Mail\PlainTextContent;
use SendGrid\Mail\To;
use SendGrid\Mail\SendAt;
use SendGrid\Mail\BatchId;


$from = new From("email", "name");
$sql1 = "SELECT * from database where sent='no'";
	  
	  $tos=array();
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
  // output data of each row
  while($row1 = $result1->fetch_assoc()) {
    $first= $row1["first"];
    $last= $row1["last"];
    $date= $row1["date"];
	$email=$row1['email'];
	  	 
	  
	   $tos[]= new To(
		
        "$email",
        "$first $last",
        [
            '{{First_Name}}' => $first,
            '{{Last_Name}}' => $last,
			'{{testing}}' => $date,
			'{{email}}' => $email,


        ],
        "View your Testing Report Card!"
		);

	  
 
	  }
  }
$globalSubstitutions = [
    '-time-' => "2018-05-03 23:10:29"
];
$plainTextContent = new PlainTextContent(
    "Good Morning!<br>
<br>
{{First_Name}}'s report card from the {{testing}} testing is ready to view.<br>
<br>
Please click the link below to view.  <br>
<br>Login with the following:<br>
Name: {{First_Name}}<br>
Email: {{email}}.<br>
<br>
"
);
$htmlContent = new HtmlContent(
     "Good Morning!<br>
<br>
{{First_Name}}'s report card from the {{testing}} testing is ready to view.<br>
<br>
Please click the link below to view.  <br>
<br>Login with the following:<br>
Name: {{First_Name}}<br>
Email: {{email}}.<br>
<br>
"
);

 
$sendgrid = new \SendGrid('apikey');
try {
    $response = $sendgrid->client->mail()->batch()->post();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    $batch= $response->body() . "\n";
	echo $batch."<br>
<br>
<br>
";
} catch (Exception $ex) {
    echo 'Caught exception: '.  $ex->getMessage();
}
  
  $data = json_decode($batch);
  $batchnew= $data->batch_id;
  

$sendtime=strtotime("today 9:02");
$email = new Mail(
    $from,
    $tos,
    $subject, // or array of subjects, these take precedence
    $plainTextContent,
    $htmlContent,
    $globalSubstitutions
);
$email->setSendAt($sendtime);


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

I edited the code to not include personal information.  

As you can see, I run a loop at the start to create an array of data for the to addresses.  This sends multiple emails (not just one email with multiple to's).  

 

However, no matter what I put in the for sendat time, it sends it right away when there is more than one email present.  

 

I tried creating a batch id then getting the id via json and then setting the batch id, but it still sends right away.  Any help would be appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.