使用PHPmailer发送多封电子邮件 [英] Sending multiple emails with PHPmailer

查看:473
本文介绍了使用PHPmailer发送多封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忘了我自己创建了SendMail();函数,这就是为什么解释开始时没有提到它的作用的原因.

I forgot I'd created the SendMail(); function myself, which is why the explanation doesn't mention at first what it does.

我在使用PHPMailer时遇到了麻烦( https://github.com/PHPMailer/PHPMailer ),当您尝试发送两封电子邮件时,一封紧接在另一封电子邮件之后.

I'm having some trouble with PHPMailer (https://github.com/PHPMailer/PHPMailer) when attempting to send two emails, one directly after the other.

该脚本几乎完全是开箱即用"的,仅作了一些修改,例如foreach循环以允许多个地址,并且一切仍然正常运行.

The script is almost completely 'out of the box', with only a few modifications such as a foreach loop to allow for multiple addresses, and everything still works perfectly.

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

However, if I attempt to call more than one instance of SendMail(); I get the error message:

Fatal error: Cannot override final method Exception::__clone() in .... online 0

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

Previously I was using the in-built mail(); function, which allowed me to use it as many times as I liked in quick succession , but it doesn't appear to be that simple with 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);

以上内容将导致两封相同的电子邮件发送给不同的人,但是我无法轻松地使用PHPmailer复制此功能.

The above would result in two identical emails being sent to different people, however I can't easily replicate this functionality with PHPmailer.

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

Is there a way of stacking these requests so that I can send successive emails without it failing? Forcing the script to wait until the first email has been sent would also be acceptable, although not preferential.

正如我提到的,我知道只有一个实例被调用时它可以工作,但是我似乎无法重用该函数.

As I mentioned I know it works when only one instance is called, but I don't seem to be able to re-use the function.

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

I haven't included the source code, although it is all available on the link provided above.

预先感谢

根据要求编辑

// 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();

上面是我希望它能像mail();一样工作的方式.第一封电子邮件可以正常运行,第二封电子邮件则无法正常运行.

The above is how I want this to work, as it would work with mail();. The first email will work fine, the second will not.

SendMail()代码

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

This is from the PHPmailer website, and is what is defined as SendMail();. The only difference from the example is the loop for AddAddress, and the inclusion of $to as a global variable.

$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";

推荐答案

您尚未发布此代码以使我得出一个完整的结论,但这是从Exception以及您在函数内部定义了覆盖类的方式得出的,您每次都会这样加载class.phpmailer.php:

You haven't posted this code that lets me make this a complete conclusion, but from the Exception and the way you've defined an overriding class inside a function, you probably have class.phpmailer.php loading every time like this:

require('class.phpmailer.php');

include('class.phpmailer.php');

您应该将该行更改为

require_once('class.phpmailer.php');

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

The reason you need to change it to require_once is so that PHP will not load the class file the second time when you try to create the new/second PHPMailer class. Otherwise, the line class PHPMailer throws the __clone() exception.

这篇关于使用PHPmailer发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆