如何使用Sendgrid调用动态html文件中的姓名和邮件地址?

How to call the name and mail address in dynamic html file using Sendgrid?

我需要的!

我需要在 HTML 邮件模板中的任意位置包含邮件地址。

我做什么!

每天发送大约 1000 封邮件,所以我的邮件模板应该是动态的。

我必须上传一个 HTML 设计,它应该从 CSV 文件中获取姓名和电子邮件地址。

获取信息对我来说工作正常,但我需要在 HTML 模板中使用我需要的数据。

请告诉我该怎么做?谢谢

      <?php 
        require 'vendor/autoload.php'; 
        use Exception;
        use SendGrid;
        use SendGrid\Mail\Subject;
        use SendGrid\Mail\From;
        use SendGrid\Mail\Content;
        use SendGrid\Mail\Mail;
        use SendGrid\Mail\TypeException;
        use SendGrid\Mail\Personalization;
        use SendGrid\Mail\To;
        use SendGrid\Response;

        //  Define API key first (must be a string) - or quit if you don't have the API key for SendGrid
        $apiKey = "xxxxxxxx";

        //  This collection is for email addresses
        //  Maybe better to verify entries first
        $text= file_get_contents('design.html');
        $content =  $text;

        $file = 'info.csv';
        $content = file($file);

        $array = array();

        for($i = 0; $i < count($content); $i++) {
            $line = explode(',', $content[$i]);
            for($j = 0; $j < count($line); $j++) {
                $array[$i][$j + 1] = $line[$j];
            }
        }
        $emailAddresses = array_column($array, 1);
        $username = array_column($array, 2);

        //  Verify first if you really have addresses!
        if (count($emailAddresses) === 0) {
            die("Nope");
        }

        //  Create the initial mail to send to every recipient
        try {
            $mailFrom = new From("xxxxxxx");
            $mailSubject = new Subject("This is my subject");
            $mailContent = new Content("text/html", $text);

            //  Create the message
            $mail = new Mail($mailFrom);
            $mail->setSubject($mailSubject);
            $mail->addContent($mailContent);
            } catch (TypeException $e) {
            die("Your parameter is failing: " . $e->getMessage());
        }

        //  Iterate for every recipient
        foreach ($emailAddresses as $address) {
            try {
                //  Create new Personalization instance
                $personalization = new Personalization();
                //  Append To recipient
                $personalization->addTo(new To($address) ,
                [
                    "name" => $username,
                    "email" => $address
                ]);

                //  Append onto Mail
                $mail->addPersonalization($personalization);
                } catch (TypeException $e) {
                //  This address is failing!
                //  (Just ignore this address)
                echo "\nThis address is failing: " . $e->getMessage();
            }
        }

        //  Not having Personalizations added? Quit
        if ($mail->getPersonalizationCount() === 0) {
            die("No recipients for your mail. Sorry");
        }

        try {
            //  Create SendGrid instance
            $client = new SendGrid($apiKey);

            //  Send the mail and fetch Response
            // $response = $client->send($mail);

            //  Not having expected response?
            if (($response instanceof Response) === false) {
                //  Unknown response given
                die("Unclear response");
            }

            //  Review/evaluate response status and body (json formatted)
            echo "Got HTTP " . $response->statusCode() . ":\n" . $response->body();
            } catch (Exception $e) {
            die("It failed! " . $e->getMessage());
        }
     ?>

"design.html"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>{{name}}</h1>
    <h1>{{email}}</h1>
</body>
</html>

我收到的邮件

$personalization->addTo 行之后将其添加到您的代码中:

$personalization->addSubstitution('%name%', "John Doe");

然后您可以使用该变量。

你也可以试试这个:

$mail->addDynamicTemplateDatas([
    '%name%' => 'John Doe',
]);

之后,在您的 HTML:

中使用它
Hello, %name%!