Sendgrid 电子邮件群发无法发送 PHP

Sendgrid email bulk couldn't send PHP

<?php
    if (isset($_POST['send']))
    {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $body = $_POST['message'];

                $data = array(
                    "personalizations" => array(
                        array(
                            "to" => array(
                                array(
                                    "email" => $email,
                                    "name" => $name
                                )
                            )
                        )
                    ) ,
                    "from" => array(
                        "email" => $sender
                    ) ,
                    "subject" => $subject,
                    "content" => array(
                        array(
                            "type" => "text/html",
                            "value" => $body
                        )
                    )
                );
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);
            curl_close($ch);
    
           
    
    }
    ?>

我正在使用 Sendgrid API 通过 PHP 发送电子邮件 我可以一次发送到一封电子邮件,但是 我需要将 $email 变量作为大量电子邮件传递

array(
         "email" => $email,
         "name" => $name
         )


  

$email 变量将参数设置为电子邮件的集合 示例:$email="abc@mail.com,info@abc.com,def@gmail.com"

我该怎么做?

Heyho Gi,

您可以使用 implode() 将数组转换为字符串,这样您就可以将电子邮件作为数组获取,然后您可以说 $email = implode(",", $emails); 并且 $email 将是 abc@mail.com,info@abc.com,def@gmail.com。但是我不明白你为什么要用一个外部公司来打他的!您可以在您的机器上本地执行此操作!不要与其他公司共享太多数据 ;)

~蒂姆

a) explode() 电子邮件值 , 将其转换为数组

b) 在此数组上应用循环并发送邮件。

c) 确保检查真实值而不是 $_POST['send']

<?php
    if (!empty($_POST['email']) && !empty($_POST['subject']))
    {
        $emails = explode(',',$_POST['email']);
        $subject = $_POST['subject'];
        $body = $_POST['message'];
        
        foreach($emails as $email){

            $data = array(
                "personalizations" => array(
                    array(
                        "to" => array(
                            array(
                                "email" => $email,
                                "name" => $name
                            )
                        )
                    )
                ) ,
                "from" => array(
                    "email" => $sender
                ) ,
                "subject" => $subject,
                "content" => array(
                    array(
                        "type" => "text/html",
                        "value" => $body
                    )
                )
            );
        
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);
            curl_close($ch);
        }
           
    
    }
    ?>