我正在尝试使用phpMailer将两个版本的同一封电子邮件发送给两个收件人
根据我需要发送一个或另一个版本的电子邮件。
目前我只能向两个电子邮件地址发送相同版本
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx@xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx@xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first@email.xxx", "second@email.xxx");
foreach($add as $address) {
if($address == "first@email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second@email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
答案 0 :(得分:2)
如果您希望完成所有设置,只需重复一次,只需在$mail
循环中克隆foreach($add)
对象:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first@mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
这为每个循环创建了一个单独的$mail
对象克隆,允许您添加不同的电子邮件正文,主题等,而无需重写所有连接设置。
答案 1 :(得分:-1)
我想您要搜索$maileremail
内的$add
并发送所需的电子邮件。
$add = array("first@email.xxx", "second@email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
<强>编辑:强> 我误解了这个问题。 Brian是对的。