searls03 Posted October 26, 2022 Share Posted October 26, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/315457-sendgrid-assistance/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.