Problem with using PHPMailer for SMTP - php

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?

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

PHPMailer: Attempt to assign property of non-object

I'm frustrated to fix this.
One time I made it work. The next day, an error annoys me:
Warning: Attempt to assign property of non-object in C:\XAMPP\htdocs\HAF\includes\sendmail.php on line 356
Help me fix this :(
Here's my code:
sendmail.php
class SendMail {
function notification($recipient, $name, $subject, $message) {
global $email;
$email->Host = "smtp.gmail.com";
$email->SMTPAuth = true;
$email->Username = "******#gmail.com";
$email->Password = "**********";
$email->SMTPSecure = "tls";
$email->Port = 465;
$email->setFrom('admin#gmail.com', 'My WebApp');
$email->addAddress($recipient);
$email->isHTML(true);
$email->Subject = $subject;
$email->Body = $message;
if(!$email->send()) {
return false;
} else {
return true;
}
}
}
index.php
$email = "jaydenjames#gmail.com";
$name = "Jayden James";
$message = "Welcome {$name}!";
$SendMail->notification($email, $name, 'Welcome guest!', $message);
update answer with gmail configuration....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername#gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = 'MyUsername#gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail#gmail.com');
$mail->AddReplyTo('phoenixd110#gmail.com', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}

Can solve my "SMTP Error: Could not authenticate" error

i have a problem with my PHPmailer function.
I want my registration form to send a mail to every new registered member.
I get this error code. SMTP Error: Could not authenticate.
I have contacted my webhost, and they say that i have the right username,password, smtp server etc.
include "class.smtp.php";
include "class.phpmailer.php";
$Host = "mail.smtp.com"; // SMTP servers
$Username = "support#mymail.com"; // SMTP password
$Password = "mymailpassword";
$From = "support#mymail.com";
$FromName = "My name";
$ToEmail = $mail;
$ToName = $username;
$header = str_replace('%homepage_url%', $hp_url, $_language->module['mail_subject']);
$Message = str_replace(Array('%nickname%', '%username%', '%password%', '%activationlink%', '%pagetitle%', '%homepage_url%'), Array(stripslashes($nickname), stripslashes($username), stripslashes($pwd1), stripslashes($validatelink), $hp_title, $hp_url), $_language->module['mail_text']);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($ToEmail , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $header;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($Message);
$mail->CharSet="UTF-8";
if(!$mail->Send()){
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
} else {
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
}

Trying to create an array with commas and semicolans

I want to create a loop for my mail system that uses the format
stan.smith#americandadcia.fx, Stan Smith
So that I can pass it through my function
function SendEmail($to,$fromName, $subject, $message)
{
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = smtp_host;
$mail->Port = smtp_port;
$mail->SMTPSecure = smtp_protocol;
$mail->SMTPAuth = true;
$mail->Username = smtp_user;
$mail->Password = smtp_pass;
$mail->setFrom(smtp_user, $fromName);
$mail->addReplyTo(smtp_user, $fromName);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
This code is to go through a loop, so say you have
Stan Smith, Roger Smith and Francine Smith the form input would have
stan.smith#americandad.fx, Stan Smith; roger.smith#amongyou.ufo, Roger Smith; francine.smith#desperatehousewives.fox, Francine Smith
The addresses would then be passed into my functions file and ran through the function to send the email
$mail = new MailSystem();
$emails=$_POST['emailTo'];
$email=explode(";",$emails);
foreach($email as $address) {
echo $mail->SendEmail($address, SITE_NAME." Newsletter", $_POST['emailSubject'], $_POST['emailBody']);
}
My Problem is that when the email attempts to send it's going to try to run $mail->addAddress($address) and I get the response that it's an invalid address. I can go in and manually set the addAddress with my email", "My name" but when it goes through this way it resolves to invalid address.
On the addAddress() you have to split the receiver name and address. Your function should look like the following:
function SendEmail($to,$fromName, $subject, $message)
{
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = smtp_host;
$mail->Port = smtp_port;
$mail->SMTPSecure = smtp_protocol;
$mail->SMTPAuth = true;
$mail->Username = smtp_user;
$mail->Password = smtp_pass;
$mail->setFrom(smtp_user, $fromName);
$mail->addReplyTo(smtp_user, $fromName);
//split the to on , to get mail address and receiver name.
$address = explode(',', $to);
//check if the name is available.
if (!isset($address[1])) {
$mail->addAddress($address[0]);
} else {
$mail->addAddress($address[0], $address[1]);
}
$mail->Subject = $subject;
$mail->msgHTML($message);
//remove the following line because duplicate sending!!!
//$mail->send();
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}

PHPMailer: The following From address failed

I have some troubles with my sending loop with PHP Mailer:
<?php
foreach ($packTabCible as $key => $pack) {
// PHPMailer
$mail = new PHPMailer;
$mail->Mailer = "smtp";
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->SMTPAuth = true;
$mail->Encoding = '8bit';
$mail->SMTPDebug = 3;
$mail->setLanguage('fr', './PHPMailer/language/');
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 10000;
$mail->Host = $hostname;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SMTPSecure = $tls;
$mail->Port = $port;
$mail->FromName = utf8_decode($from_name);
$mail->isHTML(true);
$mail->Subject = utf8_decode($sujet);
$mail->From = $from_mail;
foreach ($pack as $email => $md5) {
$mail->addAddress($email);
$header = str_replace("#miroir#", $link_miroir, $ip_header_html);
$footer = str_replace("#lien#", $link_unsub, $ip_footer_html);
$content = str_replace("<body>", "<body>".$header, $message);
$content = str_replace("</body>", $footer.'</body>', $content);
$content = str_replace("#email#", $email, $content);
$mail->Body = $content;
$mail->AltBody = str_replace("#miroir#", $link_miroir, utf8_decode($ip_header_txt))
. $txt
. str_replace("#lien#", $link_unsub, $ip_footer_txt);
$mail->send();
$mail->clearAddresses();
usleep($timer);
}
$mail->SmtpClose();
usleep(5000000);
}
?>
I'm 100% sure of the validity of the parameters I send to PHPMailer.
There is 2 loops, and I reset the connexion after each loop of $packTabCible with $mail->SmtpClose();
But, PHPMailer return the error:
The following From address failed: news#my-domain.com : MAIL FROM command failed,0 ok
,
2,SMTP server error: MAIL FROM command failed Detail: 0 ok
SMTP code:
2SMTP server error: MAIL FROM command failed Detail: 0 ok
SMTP code:
2
But news#my-domain.com is the good one so the problem is elsewhere...
If someone have an idea, big thanks

Categories