PHPmailer duplicate email issue - conditional statement with $mail->Send() - php

I came across a bizarre issue with PHPmailer (version 5.1) that I'm trying to workaround. I've seen quite a bit of good feedback on here, so I thought I would give it a try. I've found that when I attempt to create a customized confirmation message with a conditional statement based on $mail->send(), I receive duplicate emails. I can duplicate it with the generic testemail.php script that comes with the PHPMailer download. Here's the code:
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.domain.com"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "password"; // SMTP server password
$mail->IsSendmail();
$mail->From = "example_from#domain.com";
$mail->FromName = "First Last";
$to = "example#domain.com";
$mail->AddAddress($to);
$mail->Subject = "PHP Mailer test";
$message = "This is a test. \n";
$mail->Body = $message;
$mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
The above code echoes the "Message has been sent" confirmation, but then sends two emails. If I comment out the $mail->send() line, I still receive the "message has been sent" confirmation and only get one message. If I remove the conditional statement and leave the $mail->send() line commented out, no email is sent.
Why does adding a conditional statement cause an email to be sent without calling the $mail->send() method?What is the proper way of adding a customized confirmation message?

When you put $mail->Send() in your conditional, you're actually calling it again, sending another message, and checking if that second message was sent.
If you just keep
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
and get rid of the original, unconditional Send call, you should be fine.
Alternatively, if it's clearer for you or if you need to do some processing elsewhere that depends on whether the message was successfully sent, you could do the essentially equivalent:
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

Related

$mail->send() in PHPMailer is returning true but I didn't receive mail

I'm using PHPMailer to send emails from my website but $mail->send() is returning true and mail is not sending. No error is reporting in my error log. I hosted my site in Bigrock. I didn't find any errors in my code.
<?php
if(isset($_POST['submit']))
{
require 'class.smtp.php';
require 'PHPMailerAutoload.php';
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'exmaple#gmail.com');
$fromrec=$_POST['from'];
$from="example#gmail.com";
$subject=$_POST['sf'];
$message=$_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->setFrom($from, 'Rahul');
$mail->addAddress("example1#gmail.com");
$mail->Subject = $subject;
$mail->Body = "From:".$fromrec."".$message;
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else if($mail->send()) {
echo 'Message has been sent.';
echo $mail->ErrorInfo;
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
?>
On this issue, I have consulted Bigrock customer care and I made a chat with them. It's simple in the above code the from address email id must be domain specific and to address would be anything. After changing the email id to domain specific and Host to mail.example.com. My problem has been solved. If anyone got the same problem please try this.

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 Fatal Error

Hi guys i'm trying to use a php mailer however I keep getting issues for example in the sampler my emails simply won't send. So I changed to this new method and when testing i'm getting this error: Fatal error: Class 'SMTP' not found in /home3/hutch/public_html/Murphy/class.phpmailer.php on line 1325 anyone know why this is. I tried chanign the require to ("PHPMailerAutoload.php"); Which still didn't work still got the same error. I got both class.phpmailer.php and PHPMailerAutoload.php from the Github hosted documentation so I'm sure they are up to date. Any ideas?
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.sulmaxmarketing.com"; // SMTP server
$mail->From = "info#sulmaxmarketing.com";
$mail->AddAddress("sulmaxcp#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else {
echo 'Message has been sent.';
}
?>
You are sending email using SMTP, so you need to include the SMTP class file:
require("class.smtp.php");

phpmailer not sending email to gmail,yahoo,hotmail or these are blocking email sent by phpmailer

i am using PHPmailer to send emails
here is code that i have used:
$mail = new PHPMailer();
$subject = "test";
$to = "test_patel#yahoo.com"
$mail->SetFrom("PDSociety#aol.com","Punjab Dental Society");
$mail->AddReplyTo("PDSociety#aol.com", "Punjab Dental Society");
$mail->Subject = $subject;
$mail->MsgHTML($str);
$mail->AddAddress($to, "Punjab Dental Society");
if(!$mail->Send())
{
$err = "Mailer Error: " . $mail->ErrorInfo;
//echo $err;
} else {
$msg = "Message sent!";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
if i change email address from yahoo to gmail or hotmail, still email are not sent.
i checked by echoing error, but no errors.
can anyone explain what is the issue ?
After trying various ways, i found following code working with almost all email providers
$to['email'] = "recipients email address";
$to['name'] = "name";
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = 'xyz#domainname.com';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('xyz#domainname.com', 'any name');
$mail->AddAddress($to['email'],$to['name']);
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;
}
$mail->ClearAddresses();
variable values needs to be changed accordingly.
Hope these helps people having issues with PHPmailer
PHPMailer is only involved in submitting the message to your own mail server, and you're not having any problem there. After that, your mail server takes on the responsibility of sending it on, so you will find the answer in your mail server's logs.
There is no simple way to ensure messages end up in the inbox and not spam - if there was, spammers would be using it and filtering would be useless. Make sure your DNS resolves backwards and forwards, that you have valid SPF records, that you sign your messages with DKIM (especially important for Yahoo) and most importantly, that you don't send messages that your recipients think are spam.
Try this :
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->AddAddress($to['email'],$to['name']);
$mail->FromName = '';
$mail->Subject = $subject;
$mail->MsgHTML($message);
$send = true;
return $mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$e->getMessage(); //Boring error messages from anything else!
}
It will help you if any exception error.
Have you looked on the post here: Using PHPMailer Results in many blocked emails? The asker solved the issue by changing the email subject:
Well I solved the issue; the code above was not the problem and works
great.
In my subject, I used a phrase regarding "verify your account
information" and that got it blocked on a few ISP's.
So the lesson is, your subject matters. I was looking at my php code
and my body content before I realized this.
The content of the email and its subject can make ISPs ban it. You could try taking the content of one of your received emails from your inbox and see if that goes through.

Can not send mail from external SMTP on heroku?

I used the following code in my app to send a mail from my heroku app
<?php
require("PHPMailer-master/class.phpmailer.php");
require("PHPMailer-master/class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "suneetha.itham#gmail.com";
$mail->AddAddress("suneetha#yantranet.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}else {
echo 'Message has been sent.';
}
?>
But, my app is showing up
"Message was not sent.Mailer error: The following From address failed: suneetha.itham#gmail.com : MAIL FROM command failed,530,5.7.0 Must issue a STARTTLS command first. h20sm18987465qen.5 - gsmtp" \n
Can any one help me to solve this problem.Thanks.
I think this more a issue with your smtp config's See this answer: https://stackoverflow.com/a/16048485/959041
Does your code work on your local machine?

Categories