[英]Sending email to multiple Recipients with swiftmailer
我正在尝试在项目中使用swiftmailer,以便可以将html新闻稿发送给多个用户。 我已经进行了彻底的搜索,但我所得到的一切从未为我工作。 我想在用逗号分隔的表单输入字段中粘贴多个收件人,并将html电子邮件发送给他们。 我将收件人设置为变量($recipients_emails)
,并将其传递给发送代码中的html_email
()方法,与html_email
相同。
问题是:问题1我如何从收件人输入字段发送给多个收件人。
我尝试了这个:
if (isset($_POST['recipients_emails'])) {
$recipients_emails = array($_POST['recipients_emails'] );
$recipients_emails= implode(',',$recipients_emails);
}
Q2我如何在heredoc标签内制作HTML。 当我尝试像这样串联时->setBody('<<<EOT'.$html_email.'EOT;', 'text/html');
,我的消息将与heredoc标签一起出现。
if (isset($_POST['html_email'])) {
$html_email = $_POST['html_email'];
}
如何从$_POST['html_email'];
在EOT标签内;
这是swiftmailer发送脚本的一部分;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($html_email, 'text/html');
Nota bene:我还在学习这些东西。
根据这份文件
// 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'
]);
您可以将数组直接输入到setTo,setCc或setBcc函数中,无需将其转换为字符串
好的,首先您需要创建一个构成Heredoc内容的变量。 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');
您应该首先将输入数据分解为单个电子邮件地址,然后将有效数据推送到数组中,以验证输入数据。 之后,您可以将生成的数组提供给setTo()。
<input type="text" name="recipients" value="email1@host.com;email2@host.com;...">
提交时
$recipients = array();
$emails = preg_split('/[;,]/', $_POST['recipients']);
foreach($emails as $email){
//check and trim the Data
if($valid){
$recipients[] = trim($email);
// do something else if valid
}else{
// Error-Handling goes here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.