I'm setting up an email to go to multiple recipients, but they shouldn't see each other on the To line. That's working. But when I add a setSendAt to the email object it's being applied to the first email, the 0 index in the personalization array.
Here's the function I'm using to add and send the emails. It's been working as expected until I needed to add in the send schedule. I feel like I might just be missing something obvious, I'd really appreciate any guidance!
public function setupEmailAndSend($template_id, $company_id) {
$this->recipients = self::populateRecipients($company_id); //returns an array of To objects
$this->from = new From($this->sender_email, $this->sender_name);
$this->email = new Mail(
$this->from,
$this->recipients
);
$this->email->setSendAt(self::getTodayAtTwoUnix());
$this->email->setTemplateId($template_id);
$this->status = self::sendMail();
return self::testSuccess($this->status);
}
getTodayAtTwoUnix() just returns the unix timestamp, as an INT, for today at 2pm EST.
The top-level Mail object var dumps:
["send_at":"SendGrid\Mail\Mail":private]=>
NULL
but personalization ->0 has the correct info:
["send_at":"SendGrid\Mail\Personalization":private]=>
object(SendGrid\Mail\SendAt)#23 (1) {
["send_at":"SendGrid\Mail\SendAt":private]=>
int(1570644000)
}
The rest of the personalization objects are send_at null.
Ok, so to answer my own question, here's how I've accomplished what I'm looking to do. Please let me know if there's a more logical solution that I've missed. Thanks!
$this->send_time = $this->getTodayAtTwoUnix();
$this->recipients = $this->populateRecipients($company_id);
$this->from = new From($this->sender_email, $this->sender_name);
$this->email = new Mail(
$this->from,
$this->recipients
);
//once the recipients are set they have personalization objects built, so now I get those objects and fill in the send at directly for each
foreach ($this->email->getPersonalizations() as $key => $value) {
$this->email->setSendAt($this->send_time, $key, $value);
}
Something I do still have a question about:
If I set up an email to send at 2pm EST and it's after that time, 2:30 EST. Does it send right away?
Hello @bjornmann,
Thanks for taking the time to share the solution! We don't have a sendAt specific use case written up just yet, but in the meantime, hopefully the kitchen sync example is useful.
In my testing, if the sendAt time is in the past, it will send immediately.
With best regards,
Elmer
Most helpful comment
Ok, so to answer my own question, here's how I've accomplished what I'm looking to do. Please let me know if there's a more logical solution that I've missed. Thanks!