2016-12-28 10 views
0

私はphpmailerを使用していますので、forループを使って複数のメールに同時に送信したいと思います。そして、私はあることを望んでいない - あなたの電子メールクライアントで、それはまた、他の電子メールを出力し、一緒に含まれる「TO」でチェックインしたとき、phpmailerが複数のメールをループしています[PHP PHPMAILER]

require 'phpmailer/PHPMailerAutoload.php'; 
$mail = new PHPMailer;      
$mail->isSMTP();          // Set mailer to   use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = 'yyy.com';     // SMTP username 
$mail->Password = 'yyy';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 
$person = array(0 => 'xxx.com', 'xxx2.com'); 
for ($x = 0; $x < 2; $x++) { 
$mail->addAddress($person[$x]); 
} 
$mail->setFrom('yyy.com'); 
$mail->isHTML(true);         // Set email format to HTML 
$mail->Subject = 'Here is the subject'; 
$mail->Body = "xxx"; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

if(!$mail->send()) { 
echo 'Message could not be sent.'; 
echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
echo 'Message has been sent'; 
} 

、コードは動作しますが、これは私の現在の符号化です。 xxx.comにメールすると、xxx2.comの「TO」フィールドにTO <name1> <xxx.com>, <name2> <xxx2.com>と表示されます。どうすればそれを避けることができますか? $mail = new PHPMailer;からループを作成する必要がありますか?

+0

送信する前に受信者リストに電子メールアドレスを追加するだけです。また、 '' $ mail-> send() ''をループ内に置く必要があります。 – CUGreen

+0

私のコードはどのように見えるでしょうか? –

+0

私の答えを参照してください – CUGreen

答えて

0

これは、毎回クラスを再作成しない別の方法です。

<?php 

require 'phpmailer/PHPMailerAutoload.php'; 
$mail = new PHPMailer; 
$mail->isSMTP();          // Set mailer to   use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = 'yyy.com';     // SMTP username 
$mail->Password = 'yyy';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 
$person = array(0 => 'xxx.com', 'xxx2.com'); 

$mail->setFrom('yyy.com'); 
$mail->isHTML(true);         // Set email format to HTML 
$mail->Subject = 'Here is the subject'; 
$mail->Body = "xxx"; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

$errors = []; 
for ($x = 0; $x < 2; $x++) { 
    $mail->addAddress($person[$x]); 
    if(!$mail->send()) { 
     $errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo; 
    } 
    $mail->clearAddresses(); 
} 

if(empty($errors)) { 
    // success 
} else { 
    // handle errors 
} 

ループ内で$mail->clearAddresses();を使用する。

+0

'$ mail-> clearAddresses();'が私が探しているものです。ありがとうございます! –

0

Carbon Copyの受信者として追加する方が簡単です。

$mail->AddCC('[email protected]', 'Person One'); 
$mail->AddCC('[email protected]', 'Person Two'); 
// .. 

簡単にするには、配列をループする必要があります。

$recipients = array(
    '[email protected]' => 'Person One', 
    '[email protected]' => 'Person Two', 
    // .. 
); 
foreach($recipients as $email => $name) 
{ 
    $mail->AddCC($email, $name); 
} 

これは実装されていませんが、これはうまくいくと思います。

+0

BCCはより良いでしょう - すべての受信者のアドレスを誰にも公開しません。 – Synchro

関連する問題