Home > Back-end >  sendgrid Sendat sending immediately
sendgrid Sendat sending immediately

Time:10-25

I am working on integrating Sendgrid into my php application. My sending script is working, except for the scheduling part of it. I am new to using sendgrid and incorporating their scripts.

    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;


$from = new From("[email protected]", "Tiger-Rock");


      
       $tos= [new To(
        
        "$email",
        "$first $last",
        [
            '{{First_Name}}' => $first,
            '{{Last_Name}}' => $last,
            '{{X_Days}}' => $days,


        ],
        "We Miss You"
        ),
]
$globalSubstitutions = [
    '-time-' => "2018-05-03 23:10:29"
];
$plainTextContent = new PlainTextContent(
    "$phrase"
);
$htmlContent = new HtmlContent(
    "$phrase"
);
$sendat=new SendAt(1666639700);

print_r($sendat);

$email = new Mail(
    $from,
    $tos,
    $subject, // or array of subjects, these take precedence
    $plainTextContent,
    $htmlContent,
    $globalSubstitutions,
    $sendat

);


$sendgrid = new \SendGrid('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');
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";
}

The $email, $first, etc in the $tos area would be replaced with actual info.

am I trying to incorporate the sendat function properly? Or would anyone be able to offer any advice on how to get it working? The printing of the sendat gives this:

SendGrid\Mail\SendAt Object ( [send_at:SendGrid\Mail\SendAt:private] => 1666639700 )

CodePudding user response:

As I can see here: https://github.com/sendgrid/sendgrid-php/blob/main/lib/mail/Mail.php

The Mail() constructor definition haven't got a $sendat parameter.

You need to use setSendAt() method.

So, try this:

$email = new Mail(
    $from,
    $tos,
    $subject, // or array of subjects, these take precedence
    $plainTextContent,
    $htmlContent,
    $globalSubstitutions
);
$email->setSendAt($sendat);

Also, you don't need to instantiate a SendAt object, because this is automatically done inside setSendAt() method. So, you can do:

$sendat = 1666639700; //Instead of new SendAt(1666639700);

Or just remove $sendat var and do directly:

$email->setSendAt(1666639700);
  • Related