Email is Sending Twice - php

I have a script that I'm running using phpmailer - it's supposed to send 2 emails - a verification email and a request email. for some reason it's sending the verification email twice. I've gone over the code 100 times and I can't figure out why. Relevant code is below - it's not nested in any loop or condition. the "code" is a get variable and "auth" is pulled from a database.
<?php
if ($code==$auth) {
$mailtitle="message 1";
$message="message text";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.mysite.org";
$mail->From = "authorize#mysite.org";
$mail->FromName = "mysite";
$mail->AddAddress($dre);
$mail->SMTPAuth = "true";
$mail->SMTPSecure = 'tls';
$mail->Username = "authorize#mysite.org";
$mail->Password = "thepassword";
$mail->Port = "587";
$mail->Subject = $mailtitle;
$mail->Body = $message;
$mail->isHTML(true);
$mail->WordWrap = 180;
$mail->send();
$mailtitle="message 2 title";
$message="message text";
$mail1 = new PHPMailer();
$mail1->IsSMTP();
$mail1->Host = "mail.mysite.org";
$mail1->From = "authorize#mysite.org";
$mail1->FromName = "mysite";
$mail1->AddAddress('authorize#mysite.org');
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}
$mail1->SMTPAuth = "true";
$mail1->SMTPSecure = 'tls';
$mail1->Username = "authorize#mysite.org";
$mail1->Password = "thepassword";
$mail1->Port = "587";
$mail1->Subject = $mailtitle;
$mail1->Body = $message;
$mail1->isHTML(true);
$mail1->WordWrap = 180;
$mail1->send();
}
?>```

You may disable the BCC by REMOVING the following
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}

Related

Getting authentication error while sending mail with PHPmailer using PHP

I am getting the following error while sending the email using PHPMailer.
Error:
SMTP Error: Could not authenticate.
I am explaining my code below.
<?php
require_once('/var/www/oditek.in/subhra/phpmailer/class.phpmailer.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
I have also turned on the the less secured app option in gmail but still same error is coming. This is the implementation like contact us form in website. User will send the email request to admin(here tuurbus). Please help me to resolve this issue.
Download Phpmailer package from https://github.com/PHPMailer/PHPMailer/ link & copy into your project and extract zip folder into your project and change the your code as following also check your password ,email id. Its works by my side.
<?php
include_once('PHPMailerAutoload.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
Try:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

Sending multiple mails in php mail

I am trying to send the multiple mails through my mailer from the below code, But it is showing error
apache http error occurred.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list in
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list into a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>to a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>

How to send multiple emails by php variable in Addcc

From $list i am getting all the emails that are posted by form after using explode
$list = explode(',', $guest_email);
foreach ($list as $k => $v) {
$mail = new PHPmailer();
$mail->From = $smtp_user;
$mail->FromName = $fromname;
$mail->Mailer = "mail";
$mail->Subject = "";
$mail->IsHTML(true);
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = ;
$mail->SMTPSecure = '';
$mail->Host = $smtp;
$mail->Port = $port;
$mail->Password = $sm;
$mail->Username = $sm;
$mail->Body = $bodyContent;
in below line the mail are going to all posted email address, in next line i want to add all the emails in cc, but it is not working
$mail->AddAddress($v);
$mail->Addcc($v)
$mail->AddReplyTo($smtp_user);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "SUCCESS";
}
}
how to send all email in $mail->Addcc($v)

Username and Password not accepted

I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}

Problem with using PHPMailer for SMTP

I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?

Categories