PHPMailer using Sendgrid, customizing each email but sending just one email

Viewed 38

Our main programmer died from covid. He had built a program in his code using Swiftmailer, which is no longer active.

I am going to reprogram it all using PHPmailer.

But I don't know how to customize each email with their first name, username in our website and their custom info.

In the PHP for swiftmailer, he did it like this:

$hdr = new SmtpApiHeader();
// Set all of the above variables
$hdr->addTo($toList);
$hdr->addSubVal('~USERNAME~', $usernameList);
$hdr->addSubVal('~FirstName~', $firstnameList);
$hdr->addSubVal('~code~', $codeList);

~code~ was a merge field to replace a security code when it sent an email to them, like if they changed their password, and they needed to get a code to authenticate it was from them. or update their profile.

so those are where he added their personalizations, those were arrays.

$toList // this would be an array of every email. then it matched the position of the other arrays for their personal info, to put in each email.

He built these as the function to send one email, or many, depending on what the email is for. All based upon subscribers who have opted in to get transactional emails from the system.

So how do we do this with PHP Mailer? I have been reading since last night, everything I could find and I cannot see how to do it.

can someone point me to a place online with the right documentation? Most of what I read, none of it touched on it.

I feel like I've been spinning my wheels.

Thanks for any pointers.

-Rich

1 Answers

You can set all the message parameters through equivalent properties in PHPMailer, such as From, FromName, Subject (and many more, see the docs), but PHPMailer does not have built-in templating. There are many ways to do that, and many packages that can help you do it (Smarty, Twig, etc), so I recommend you use them.

For sending to lists, look at the mailing list example provided with PHPMailer.

Alternatively, while SwiftMailer itself is no longer supported separately, that only happened because it became Symfony's mail component. I've not looked at it, but I strongly suspect that it will be largely unchanged, so you may find it easier to use that.

Related