-1
<?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"

我该怎么做?                                                                                                                                                                                                                                                                                                          

4

2 回答 2

0

嘿嘿,

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

〜蒂姆

于 2020-12-11T10:50:58.137 回答
0

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);
        }
           
    
    }
    ?>
于 2020-12-11T10:51:44.413 回答