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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Sendgrid调用动态html文件中的名称和邮件地址?相关的知识,希望对你有一定的参考价值。

我需要什么!

我需要在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>

我从邮件中收到的内容

enter image description here

答案

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

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

然后您可以使用该变量。

您也可以尝试以下方法:

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

之后,在您的HTML中使用它:

Hello, %name%!

以上是关于如何使用Sendgrid调用动态html文件中的名称和邮件地址?的主要内容,如果未能解决你的问题,请参考以下文章

如何将动态数据传递到sendgrid webapp上设计的电子邮件模板? : - | Sendgrid

如何使用SendGrid将图像嵌入到Node.js中的html邮件中

如何通过 API C# 和模板将自定义变量添加到 SendGrid 电子邮件

如何在我的 Sendgrid 事务模板中包含 Jinja2 模板

我们如何删除sendgrid电子邮件中的丑陋链接?

Sendgrid 集成:如何在与 CNAME 重定向相同的域上托管 AASA 文件