PHPMailer sends duplicate email - php

PHPMailer
<?php
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPSecure = "tls";
$mail->Mailer = "smtp";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->setFrom('xxx', 'Website');
//Send to Admin
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$UserEmail = 'user.example#gmail.com';
$mail2->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail2->Body = $body2;
if(!$mail2->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail2->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I have an issue where when i send the emails, Admin will receive $mail and $mail2 when they supposed to just receive $mail. While there's no problem with User. Works fine by receiving $mail2. I've tried to put $mail->ClearAddresses(); but it's still the same. By chance, what is the problem?

Clone the email variable before adding the address and stuff of the admin. (As suggested in the comments)

You need to create different-different object for both admin and user email
//Send to Admin
$mail = new PHPMailer;
$mail->IsHTML(true);
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$mail = new PHPMailer;
$mail->IsHTML(true);
$UserEmail = 'user.example#gmail.com';
$mail->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail->Body = $body2;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}

Related

Php Mailer Ending Up In Spam In Proudction

Here is my code below. But it keeps ending up in spam
I already added authentication and changed the setfrom address but it still ends up in spam
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = true;
// $mail->SMTPAutoTLS = true;
$mail->Username = '******'; // SMTP username
$mail->Password = "******";
$mail->Port = 25;
$mail->setFrom('info#canbeltech.com', 'Canbel Tech');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Canbel Tech IMT Biometric Card Receipt';
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
exit;

Show's email success but not receiving email godaddy

Hello the phpmailer works when I'm using it on localhost but for some reason when I used a hosting from GoDaddy it won't work anymore.
I already followed almost everything related to this but it seems like I can't find any solution:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$message = "Test";
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->SMTPAuth = true;
$mail->Username = 'mycpanelusername';
$mail->Password = 'mycpanelpassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 25;
$mail->setFrom('testemail#gmail.com', 'Mailer');
$mail->addAddress($email, 'Joe User');
$mail->addAddress('ellen#example.com');
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = $message ;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You have to use this to actually send the email, at the end:
$mail->send();
And to get more infos:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

PHPMailer email not send and not get any error

The below function to send email but got any mail or error :
function send_Email($email, $subject, $body ,$cc = '')
{
$this->Log("Sending email with To: ". $email. " <br>Subject: ". $subject ." <br>Body: ".$body);
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = email_host;
$mail->Username = email_username;
$mail->Password = email_password;
$mail->SetFrom('support#xyv.com', 'zzz Support');
$mail->AddReplyTo('support#xyv.com', 'zzz Support');
$mail->AddAddress($email);
$mail->AddBCC('support#xyv.com');
$mail->AddBCC('zzz#zzz.com');
if(!empty($cc)) {
$mail->addCC($cc);
}
$mail->Subject = $subject;
$mail->Body = $body;
echo "<pre>"; print_r($mail);echo "</pre>";
$response = $mail->Send();
var_dump($response);
return $response;
}
Debug : IN DEBUG MODE NOT GET ERROR
[error_count:protected] => 0
It would really help to read the docs and base your code on the examples provided, which show how to get info about errors, and show debug output. You're not getting any output because you're not asking for any. From the example in the readme:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
That will show you the reason for any sending failure. To enable SMTP debug output, do this:
$mail->SMTPDebug = 2;

sending email to gmail account using phpmailer

<?php
require_once 'PHPMailer-master/class.phpmailer.php';
require_once 'PHPMailer-master/class.phpmaileroauthgoogle.php';
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
//$mail->SMTPDebug =2;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'zhaider113#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$from = 'shahghafoor439#gmail.com';
$mail->setFrom($from, 'Ghafoor Shah');
$mail->addReplyTo($from, 'Ghafoor Shah');
$mail->addAddress('zhaider113#gmail.com', 'zeeshan');
$mail->Subject = 'This is subject';
$mail->Body = 'This is the body of email';
$mail->AltBody = 'This is the body of email';
$mail->send();
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
?>
I try to send email but it not send and give error message which is:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting */
try with $mail->SMTPSecure = 'tls';
See full example at PHPMailer's gmail example below: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Make sure you include correct PHPMailer Library.
And make this changes
Some Servers not response for SSL(Secure). So that change this $mail->SMTPSecure = 'tls';
And In your code you have two Mail sending option $mail->send();
//$mail->send();//Comment this
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
Now either mail sent Or either It will print Error log

How to setup PHPMailer SMTP

I'm trying to send some mail using php mailer, the boring thing is that one email eas sent the first time and I have no idea what I've done that makes this code to now fail.
ERROR: SMTP server error: authentication required
require("../mailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtpout.secureserver.net"; // SMTP server
$mail->From = "site email";
$mail->AddAddress("useremail");
$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.';
}
I'm hosting with GoDaddy.
Thanks to Jake.
Incase someone goes through this again here is what i put in my code.
CODE
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtpout.secureserver.net"; // SMTP server
$mail->Username = "email#domain";
$mail->Password = "pwd";
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->SMTPDebug = 2; /// i guess this is for reporting...
$mail->From = "email#domain";
$mail->AddAddress("recipient email");
$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.';
}

Categories