【问题标题】:Sendgrid email bulk couldn't send PHPSendgrid 电子邮件批量无法发送 PHP
【发布时间】:2021-03-22 18:39:43
【问题描述】:
<?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"

我该怎么做?

【问题讨论】:

  • &lt;?pre 不应该是&lt;?php 吗?另外:你确定isset($_POST['send']) 计算结果为真吗?
  • 您到底想达到什么目标?如果任何变量应该以另一种格式给出,是什么阻止您更改代码中的格式?

标签: php sendgrid-api-v3 bulk-email


【解决方案1】:

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);
        }
           
    
    }
    ?>

【讨论】:

  • 谢谢兄弟,你减轻了我的压力 :)
  • @Gihan 很高兴为您提供帮助。顺便说一句,Sendgrid API 提供了一次性发送多封电子邮件的设施,而不是循环发送(但我通过研究得到的是该设施是付费的)。你也可以看看
【解决方案2】:

嘿嘿,

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

~提姆

【讨论】:

  • uVulpos 我需要你的答案相反的方法,所以我使用explode() 将字符串转换为数组谢谢你的回答
猜你喜欢
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多