Skip to content
Advertisement

Sending multiple emails with PHPmailer

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

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

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.

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

JavaScript

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:

JavaScript

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.

Thanks in advance

Edit as requested

JavaScript

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() code

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.

JavaScript

Advertisement

Answer

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:

JavaScript

or

JavaScript

You should change that line to

JavaScript

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement