PHPMailer sending mail twice on ECHO


#1

I have a PHPMailer setup that is run through a web-browser and the input variables are sent to the PHPMailer for completion from a user input form. I then want to see the resulting form output to my web-browser as well as sending the email, so that I can then send another form etc. This result is done by echoing the $html_content variable at time of sending the email. The problem is that the echo causes PHPMailer to send the mail a second time. If I comment it out, it only sends it once. The $html_content works fine.
Any comments appreciated…


#################################
	####Email Code Start####
#################################

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';
	$mail = new PHPMailer;
	
	$mail->isSMTP();
	$mail->isHTML(true);
	$mail->Host 		= 'mail.smtp2go.com';
	$mail->SMTPAuth 	= true;
	$mail->Username 	= '******@ic-express.co.za';
	$mail->Password 	= '******';
	$mail->SMTPSecure 	= 'tls';
	$mail->Port 		= 2525;
	$mail->setFrom('******@ic-express.co.za', 'Admin - IC-Express');	
		$mail->addAddress('******@ic-express.co.za', 'A Boshoff');	
	$subject  			= 'Order confirmation email';	
	$mail->addStringAttachment(file_get_contents(HTTP_IMAGE."order/ICE00034203.pdf"), "ICE00034203.pdf");

$html_content = '
<table align="center" cellspacing="0" width="960px" style="border:4px solid #f1592b; margin:0 auto;font-family:Arial, Helvetica,sans-serif;font-size:12px;">
<tr>
<td>
<table width="100%"  style="background:#f1592b">
<tr>
<td>
<table width="100%" >
<tr>
<td><img src="'.HTTP_IMAGE.'order/logo.jpg" /></td>
<td>
<table width="100%" >
<tr>
<td style="vertical-align:top">
<table width="100%"  style="color:#FFFFFF;font-family:Arial, Helvetica,sans-serif;font-size:11px;line-height:11px;"><tr><td style="vertical-align:top">
<img src="'.HTTP_IMAGE.'order/address-icon.png"/></td><td style="vertical-align:top">P.O Box 2241<br/>
Glenashley<br/>
4022</td></tr>
<tr><td style="vertical-align:top"><img src="'.HTTP_IMAGE.'order/phone-icon.png"/></td><td>Phone: 031-0300 300</td></tr>
</table>
</td>
<td style="vertical-align:top">
<table width="100%"  style="color:#FFFFFF;font-family:Arial, Helvetica,sans-serif;font-size:11px;line-height:11px;"><tr><td style="vertical-align:top"><img src="'.HTTP_IMAGE.'order/fax-icon.png"/></td><td>Fax: 031-0300 303</td></tr>
<tr><td style="vertical-align:top"><img src="'.HTTP_IMAGE.'order/email-icon.png"/></td><td>admin@ic-express.co.za</td></tr>
<tr><td style="vertical-align:top"><img src="'.HTTP_IMAGE.'order/web-icon.png"/></td><td>www.ic-express.co.za</td></tr>
</table>
</td>
</tr>
</table>

</td>
</tr>
</table>
</td>

</tr>
<tr><td align="center" style="font-family:Arial, Helvetica,sans-serif;font-size:13px;color:#FFFFFF;line-height:16px;font-weight:bold;">PRINT ATTACHED WAYBILL AND ATTACH THIS TO YOUR SHIPMENT.</td></tr>
</table>

</td>
</tr>

<tr><td style="padding:10px 20px"><p><strong>Dear '.$user_name.',</strong></p>

<p>Many thanks for your order as follows:-<br/>
Your order will be processed once payment has been confirmed</p>';

if ($customer_id) {
	$html_content .= '<p>To view your order please click on the link below:<br/><a style="color:#f1592b" href="'.$link.'">'.$link.'</a></p>';
}

echo $html_content;

$mail->Subject 	= $subject;
$mail->Body 	= $html_content;
$mail->send();


#################################
	####Email Code End####
#################################

#2

It’s really difficult to see how your code is actually set up - for example there are multiple lines in your forum post where the start is commented out, but after a couple of line terminators there’s another important bit. I can’t tell whether those lines are on the same line in your script, or just in the forum post, but it’d be clearer if they could be separated out.

Also one or two typos that again may be just in the forum post.

Does it do the same if you substitute a much smaller version of $html_content for testing?


#3

@droopsnoot, your idea of shortening the $html_content for testing is great. I have edited the code above and removed comments to make it easier to read. The edit does not appear to be copying/pasting correctly though.
After shortening the code, PHPMailer still send two copies of the email, if I echo it at the end.


#4

@adrian39

When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I would normally do it for you first time, but I fear I may screw it up if I try with this!


#5

I have edited the code as per your recommendation @gandalf458 and the format seems good now. So, after shortening the code so that it is easier to read and understand, I have tested it. It sends the email content correctly, but as per my original problem, it sends it twice if I echo $html_content near the end of the code. To recap, I need to echo $html_content so that it displays what it has sent in the local browser.


#6

Try to put your echo after $mail->send();


#8

I tried moving the echo statement as per @YiuJia and it made no difference.
I am sure it is probably something really easy I am overlooking.


#9

Try to put : ob_start(); at the beginning of your script.

And put : ob_get_clean(); after $mail->send();

Put the echo $html_content after ob_get_clean();


#10

Tried this and the error still remains - email is sent twice? @YiuJia


#11

In another page, initialize a counter to 0 and save it in session variable. In the phpmailer page, increment this session variable and echo it to see how many times the script is called.


#12

I eventually created another “order confirmation” php file and through $_SESSION I just echoed the variable(the one which was causing the mail to be sent twice) to the new file. I would really have liked to find out why the message was sent twice when echoed but ran out of time trying.