使用 swiftmailer 向多个收件人发送电子邮件

Mar*_*own 6 php forms email newsletter swiftmailer

我正在尝试在我的项目中使用 swiftmailer,以便我可以向多个用户发送 html 时事通讯。我已经彻底搜索过,但我所得到的一切都没有为我工作。我想在以逗号分隔的表单输入字段中粘贴多个收件人并将 html 电子邮件发送给他们。我将收件人设置为一个变量($recipients_emails)并将其传递给发送代码中的 setTo() 方法,与html_email.

问题是: Q1 如何从收件人输入字段发送给多个收件人。

我试过这个:

if (isset($_POST['recipients_emails'])) {
    $recipients_emails  = array($_POST['recipients_emails'] );
    $recipients_emails= implode(',',$recipients_emails);
}
Run Code Online (Sandbox Code Playgroud)

Q2 如何在 heredoc 标签中制作 Html。当我尝试像这样连接时->setBody('<<<EOT'.$html_email.'EOT;', 'text/html');,我的消息会与 heredoc 标签一起出现。

if (isset($_POST['html_email'])) {
    $html_email = $_POST['html_email'];
}
Run Code Online (Sandbox Code Playgroud)

我如何$_POST['html_email'];在 EOT 标签内输入;

这是 swiftmailer 发送脚本的一部分;

$message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($recipients_emails)
        ->setBody($html_email, 'text/html');
Run Code Online (Sandbox Code Playgroud)

注意:我还在学习这些东西。

Huy*_*ịnh 8

根据这份文件

// Using setTo() to set all recipients in one go
$message->setTo([
  'person1@example.org',
  'person2@otherdomain.org' => 'Person 2 Name',
  'person3@example.org',
  'person4@example.org',
  'person5@example.org' => 'Person 5 Name'
]);
Run Code Online (Sandbox Code Playgroud)

可以直接将数组输入到setTo、setCc或setBcc函数中,不需要转换成字符串


小智 0

好的,首先您需要创建一个形成您的定界文档内容的变量。Heredoc 的结束标记前面必须没有空格,然后使用该变量。(要在heredoc中输出变量,您需要用大括号将它们括起来)请参阅示例..

$body = <<<EOT
 Here is the email {$html_email}

EOT;

$message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($recipients_emails)
        ->setBody($body, 'text/html');
Run Code Online (Sandbox Code Playgroud)