How to send a multiple emails at a time in cakephp

后端 未结 3 1923
日久生厌
日久生厌 2020-12-18 06:44

I need to send multiple emails at a time, can any one have example? or any idea ? I need to send mail to all my site users at a time (Mail content is same for all)

C

相关标签:
3条回答
  • 2020-12-18 07:00

    In Cakephp 2.0 I used the following code:

    $result = $email->template($template, 'default')
        ->emailFormat('html')
        ->to(array('first@gmail.com', 'second@gmail.com', 'third@gmail.com')))
        ->from($from_email)
        ->subject($subject)
        ->viewVars($data);
    
    0 讨论(0)
  • 2020-12-18 07:09

    Try this:

    $tests = array();
    foreach($users as $user) {
        $tests[] = $user['User']['email'];
    }
    
    $mail = new CakeEmail();
    $mail->to($tests) 
        ->from('<no-reply@noreply.com>')
        ->subject('ALERT')
        ->emailFormat('html')
        ->send('Your message here');
    
    0 讨论(0)
  • 2020-12-18 07:14

    I think you have 2 possibilities:

    foreach

    Let's assume you have a function mail_users within your UsersController

    function mail_users($subject = 'Sample subject') {
        $users = $this->User->find('all', array('fields' => array('email'));
        foreach ($users as $user) {
            $this->Email->reset();
            $this->Email->from     = '<no-reply@noreply.com>';
            $this->Email->to       =  $user['email'];
            $this->Email->subject  =  $subject ;
            $this->Email->sendAs   = 'html';
            $this->Email->send('Your message body');
        }
    }
    

    In this function the $this->Email->reset() is important.

    using BCC

    function mail_users($subject = 'Sample subject') {
        $users = $this->User->find('all', array('fields' => array('email'));
        $bcc = '';
        foreach ($users as $user) {
            $bcc .= $user['email'].',';
        }
        $this->Email->from     = '<no-reply@noreply.com>';
        $this->Email->bcc      = $bcc;
        $this->Email->subject  = $subject;
        $this->Email->sendAs   = 'html';
        $this->Email->send('Your message body');
    }
    

    Now you can just call this method with a link to /users/mail_users/subject

    For more information be sure to read the manual on the Email Component.

    0 讨论(0)
提交回复
热议问题