PHPMailer sends the same email twice - php

I am using PHPMailer to send emails from a PHP file.
Here you have all the code for it:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function php_mailer($destinatario,$nombre,$order,$texto,$nom){
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "...";
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "...";
$mail->Password = "...";
$mail->setFrom("...", "..");
$mail->addAddress($destinatario, $nombre);
$mail->Subject = 'Your Order #:'.$order." at ".$nom;
$mail->msgHTML($texto);
$mail->AltBody = 'HTML messaging not supported';
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
And here is how am I calling the php_mailer function:
php_mailer($email,"Online Customer",$num_order,$completo,$nombre);
My issue is that PHPMailer is sending every email twice.

I suspect your browser is sending repeated requests due to a plugin. This is not an unusual problem; there is an article about it in the PHPMailer wiki. Try turning off plug-ins and appending random numbers to your subject line, or check your web logs for the repeated requests to be certain.
While I’m here, would you find a PHPMailer video course useful? I’m thinking of creating one and I’m trying to gauge interest.

Refactor a bit. Can you please try this:
// $status = $mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

Related

PHPMailer does not execute code after $mail = new PHPMailer

I have made a website on my localhost. After everything was ready to go I hosted the website. However, now PHPMailer seems to cause the trouble. Mails are not sending and it seems like PHP code does not execute after $mail = new PHPMailer command. Here is part of the code:
if($conn->query($sql)){
require_once '/home/ziptie/public_html/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = true;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // your email id
$mail->Password = 'mypassword'; // your password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('mygmail#gmail.com', 'ZIPTIE');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$v="http://ziptie.rs/verification-page.php?sifra=$token";
$body = file_get_contents('/home/ziptie/public_html/verificationemail.html');
$body = str_replace('promenljiva', $v, $body);
$mail->Subject = 'Verifikacija kupca, ZIPTIE';
$mail->Body = $body;
$mail->addAttachment("naruzbine ".date("d-m-Y").".zip");
$mail->send();
header("Refresh:0; url=https://www.ziptie.rs/verification-page.php");
//echo '<script>alert("Verifikacioni link je poslat, proverite Vašu mejl adresu.")</script>';
/*if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}*/
}
else{
echo $conn->error;
}
Does anybody know the solution? Thanks!
found solution in upgrading to PHPMailer 6.3

EOF caught while checking if connectedThe following From address failed

I am trying to send emails using phpmailer and getting the error as mentioned in the title. I am using XAMPP for apache server.
I have searched almost every link on google and everywhere but nothing is working for me.
<?php
ini_set('display_errors', 1);
require 'autoload.php';
require 'class.phpmailer.php';
//require 'class.smtp.php';
$mail = new PHPMailer;
$mail->IsSMTP();
//$mail->SetLanguage("en", 'language');
$mail->Mailer = "smtp";
//$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->SMTPDebug = 2;
$mail->SMTPAuth=False;
$mail->Host="smtp.gmail.com";
$mail->Username = "email#gmail.com";
$mail->Password = "password";
//$mail->setFrom("email#gmail.com");
$mail->From = "email#gmail.com";
$mail->addAddress('email#gmail.com','Rishabh Goel');
$mail->isHTML(false);
$mail->Subject = 'First Subject';
$mail->Body = 'Mail body in HTML';
$mail->AltBody = 'This is the plain text version of the email content';
/*try{
$mail->Send();
echo "Thanks. Bug report successfully sent.
We will get in touch if we have any more questions!";
} catch(Exception $e){
//Something went bad
echo "Mailer Error: - " . $mail->ErrorInfo;
}*/
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
?>

How to get current email id in body using PHP Mailer

Currently I am working on a PHP email script using PHPMailer` library. I am sending a mass mail using BCC for all the email addresses.
I want each email to contain the current recipient's email address in the message body.
Below is my sample code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('noreply#example.com');
$arrMail [] = array('bcc1#example.com','bcc2#example.com');
for($i=0;$i<count( $arrMail);$i++)
{
$mail->addBCC($arrMail[$i]);
$htmlversion = 'Hello '.$arrMail[$i].' !'.
}
// $htmlversion = 'Hello <email_id needed here> !'.
$mail->Body = $htmlversion;
$mail->AltBody = $textVersion;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Mail sent';
}
Problem: If bcc1#example.com receives the email, its message body should contain their email address. Currently I am getting the first email address in the message body for every recipient.
Note: I dont want to send mail one-by-one using To like mentioned in other pages.
Also is it possible by using some session or database logic?
I am using php 5.5.9.
Your code is reusing the same email address because you didn't put the creation of the mail body in the loop. If you use a loop then you also don't need BCC.
$arrMail [] = array('bcc1#example.com', 'bcc2#example.com');
$total = count($arrMail);
for($i = 0; $i < $total; $i++) {
$email = $arrMail[$i];
$htmlversion = "Hello $email !";
$mail->Body = $htmlversion;
$mail->AltBody = $textVersion;
$mail->AddAddress($email);
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Mail sent';
}
}
I dont want to send mail one-by-one using To like mentioned in other pages.
Unfortunately, BCC by its very nature sends the same email to multiple recipients. If you want to customise each email for each person, you have to send them individual emails.

PHPMailer, no Email and no Error

I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}

email analytics with smtp2go and phpmailer

I used PHPMailer and smtp2go to build a simple function that i can you to send emails , now how can i do analytics on emails , get the emails sent and the emails opened , bounced mails ?
My function is :
require("PHPMailer-master/class.phpmailer.php");
// path to the PHPMailer class.
function sendmail(){
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "smtpcorp.com";
//Enter your SMTP2GO account's SMTP server.
$mail->Port = "2525";
// 8025, 587 and 25 can also be used. Use Port 465 for SSL.
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'ssl';
// Uncomment this line if you want to use SSL.
$mail->Username = "";
$mail->Password = "";
$mail->From = "";
$mail->FromName = "";
$mail->AddAddress("", "Rachel Recipient");
$mail->AddReplyTo("", "Sender's Name");
$mail->Subject = "Hi!";
$mail->Body = "Hi! How are you?";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Message has been sent.';
}
}
sendmail();
Regards,
This is Charlie from SMTP2GO.
To access bounces data/reports, you have 3 options:
1) Login to your SMTP2GO control panel, and click on 'Reports > Bounces' where you can view and download past bounces.
2) Use our API.
3) Use our Callbacks functionality (Settings > More > Callbacks).
Please contact us directly from the following page if you'd like any information about the above, and our team will help you out:
http://www.smtp2go.com/support

Categories