简体   繁体   中英

How do I send mass mail in cakephp without using sendgrid

This is my html template

Dear ##name##(##email##),

Thank you for contacting us.

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?

This is what I've got so far

$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();

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.

Then you have to modify way of setting up your viewVars() and 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, eg:

$emails = array(
    'a@example.com' => 'apple',
    'b@example.com' => 'ball',
    'c@example.com' => 'cat'
);

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM