使用PHPmailer发送多封电子邮件

Joe*_*oeP 1 php phpmailer

编辑:我忘记了我自己创建了这个SendMail();函数,这就是为什么解释最初没有提到它的作用.

在尝试发送两封电子邮件时,我遇到了PHPMailer(https://github.com/PHPMailer/PHPMailer)的问题.

该脚本几乎完全"开箱即用",只有一些修改,例如foreach允许多个地址的循环,一切仍然完美.

但是,如果我尝试调用多个实例,SendMail();我会收到错误消息:

Fatal error: Cannot override final method Exception::__clone() in .... online 0
Run Code Online (Sandbox Code Playgroud)

以前我使用的是内置mail();函数,它允许我快速连续使用它多次,但使用PHPmailer看起来并不那么简单:

$to = me@me.com;
$to2 = me2@me2.com';
$headers = 'php headers etc';
$subject = 'generic subject';
$message = 'generic message';
mail($to, $subject, $message, $headers);
mail($to2, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)

以上将导致两个相同的电子邮件发送给不同的人,但我不能轻易地使用PHPmailer复制此功能.

有没有办法堆叠这些请求,以便我可以发送连续的电子邮件,而不会失败?强制脚本等到第一封电子邮件发送后也是可以接受的,尽管不是优先的.

正如我所提到的,我知道它只在调用一个实例时有效,但我似乎无法重用该函数.

我没有包含源代码,尽管它可以在上面提供的链接上找到.

提前致谢

按要求编辑

// First Email
$to = array(
'test@test.com',
 'test2@test.com',);
$subject = "Subject";
$message = $message_start.$message_ONE.$message_end;

sendMail();

// Second Email
$to = array(
'test@test.com',
 'test2@test.com',);
$subject = "Subject";
$message = $message_start.$message_TWO.$message_end;

sendMail();
Run Code Online (Sandbox Code Playgroud)

以上就是我希望它如何工作,因为它可以使用mail();.第一封电子邮件可以正常工作,第二封邮件不会.

SendMail()代码

这是来自PHPmailer网站,并且被定义为SendMail();.与示例的唯一区别是循环for AddAddress和包含$to作为全局变量.

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "smtp1.example.com;smtp2.example.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->From = "from@example.com";
$mail->FromName = "Mailer";
foreach($to as $to_add){
$mail->AddAddress($to_add);                  // name is optional
}
$mail->AddReplyTo("info@example.com", "Information");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
Run Code Online (Sandbox Code Playgroud)

sja*_*agr 10

你还没有发布这个代码让我得出一个完整的结论,但是从异常和你在函数中定义一个重写类的方式,你可能class.phpmailer.php每次都加载如下:

require('class.phpmailer.php');
Run Code Online (Sandbox Code Playgroud)

要么

include('class.phpmailer.php');
Run Code Online (Sandbox Code Playgroud)

您应该将该行更改为

require_once('class.phpmailer.php');
Run Code Online (Sandbox Code Playgroud)

您需要将其更改require_once为以便PHP在第二次尝试创建新/第二PHPMailer类时不会加载类文件的原因.否则,该行class PHPMailer抛出__clone()异常.

  • PHPMailer现在提供了一个自动加载器,您应该优先使用它来显式加载类,只需要`require'PHPMailerAutoload.php';`它将会找到它需要的任何其他类. (2认同)