如何在不使用sendgrid的情况下在cakephp中发送大量邮件 [英] How do I send mass mail in cakephp without using sendgrid
问题描述
这是我的html模板
Dear ##name##(##email##),
Thank you for contacting us.
我要用接收者的姓名和电子邮件替换## name ##和## email ##获取它的人将在数组中提供。
I want to replace ##name## and ##email## with the receiver's name and email of the person who gets it which will be provided in the array. How do I do it?
这是我到目前为止
$to_email = array('a@example.com', 'b@example.com', 'c@example.com');
$to_name = array('apple', 'ball', 'cat');
$Email = new CakeEmail();
$Email->from($from);
$Email->to($to_email );
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('data' => $body));
$Email->template('bulk');
$Email->send();
推荐答案
您应该先创建 template 为您的电子邮件,其中将包括您的当前内容(我在下面的示例中使用名称 example_template.ctp
):
You should start with creating template for your email, which will be including your current content (I use name example_template.ctp
in my samples below):
Dear <?php echo $name; ?> <?php echo $email; ?>,
Thank you for contacting us.
然后你必须修改设置 viewVars code>和
template()
:
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
还需要更改发送电子邮件的方式以通过电子邮件,而不是发送所有收件人领域。因此,将输入数组合并为一个,例如:
There is also required to change way of sending emails to loop over emails instead of sending all recipients in one field. So combine your input arrays into one, e.g.:
$emails = array(
'a@example.com' => 'apple',
'b@example.com' => 'ball',
'c@example.com' => 'cat'
);
然后只需foreach到你的数组并发送邮件:
Then just foreach over your array and send mails:
$Email = new CakeEmail();
foreach ($emails as $email => $name) {
$Email->from($from);
$Email->to($email);
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
$Email->send();
$Email->reset(); // for cleaning up CakeEmail object
}
这篇关于如何在不使用sendgrid的情况下在cakephp中发送大量邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!