在cakephp中使用多个电子邮件ID发送电子邮件

时间:2016-09-09 09:48:58

标签: php cakephp cakephp-3.x

我正在尝试发送包含多个电子邮件ID的电子邮件。 我的SMTP电子邮件详细信息:

$DomainEmailDetails = $this-> DomainEmailDetails->find('all');
$DomainEmailDetails = $DomainEmailDetails->toArray();

结果:

Array
(
    [0] => App\Model\Entity\DomainEmailDetail Object
        (
            [id] => 1
            [name] => email1
            [user_name] => abc@gmail.com
            [user_password] => abc123
            [host_name] => smtp.gmail.com
            [port] => 465
            [reply_email_id] => abc@gmail.com
        )

    [1] => App\Model\Entity\DomainEmailDetail Object
        (
            [id] => 2
            [name] => email2
            [user_name] => xyz@gmail.com
            [user_password] => xyz123
            [host_name] => smtp.gmail.com
            [port] => 465
            [reply_email_id] => xyz@gmail.com
        )


)

,电子邮件发送代码为:

for ($emp=0; $emp <20 ; $emp++) { 

    foreach ($DomainEmailDetails as $key => $DomainEmailDetails) {

                        $host     = 'ssl://'.$DomainEmailDetails['host_name'];
                        $username = $DomainEmailDetails['user_name'];
                        $password = $DomainEmailDetails['user_password'];
                        $port     = $DomainEmailDetails['port'];
                        $email_to = 'webtechnology@gmail.com';
                        $senderName = 'Web';
                        $ReplyTo    = $DomainEmailDetails['reply_email_id'];

                        Email::configTransport('WebMail', [
                                                            'className' => 'Smtp',
                                                            'host' => $host,
                                                            'port' => $port,
                                                            'timeout' => 30,
                                                            'username' => $username,
                                                            'password' => $password,
                                                            'client' => null,
                                                            'tls' => null,
                                              ]);

                        ////////// SEND MAIL 
                        $email = new Email('WebMail');
                        $email  ->template('default','default')
                                ->emailFormat('both')
                                ->from([$username => $senderName])
                                ->to($email_to)
                                ->replyTo($ReplyTo)
                                ->subject('Client Message');
                         for ($i=0; $i <10 ; $i++) { 
                              $response = $email->send($msgsend);
                         }
        }
    }

如何通过一个user_name发送每10封电子邮件中的第二封电子邮件登录详细信息。我的意思是说user_name = abc@gmail.com一次只能发送10封电子邮件,之后SMTP登录详细信息会自动设置第二个user_name = xyz@gmail.com,表示其余10封电子邮件将从第2个用户名发送

1 个答案:

答案 0 :(得分:0)

试试这个:

  $i = 0;
  for ($emp=0; $emp <20 ; $emp++) { 

     $host     = 'ssl://'.$DomainEmailDetails [$i]['host_name'];
     $username = $DomainEmailDetails [$i]['user_name'];
     $password = $DomainEmailDetails [$i]['user_password'];
     $port     = $DomainEmailDetails [$i]['port'];

    if(($emp % 10) == 0)
     $i++;
    ...
  }