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

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;
}

Related

PHP Mailer: I'm getting an error "Could not access file" while trying to upload the file through form and sending it as a attachment

Here's The PHP Code
I'm able to send the mail. If I comment out the $_FILES['attachment']['name'];
$conn = new mysqli($servername, $username, $password, $dbname);
$category = $_POST['category'];
$attachment = $_FILES['attachment']['name'];
$query = "select Name, email, status, category from mailer where status='subscribed' and category='$category'";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
$name = $row['Name'];
$email = $row['email'];
$status = $row['status'];
$category1 = $row['category'];
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//send code
//Recipients
$mail->addAddress($email);
$mail->AddAttachment($attachment);
Change $attachment = $_FILES['attachment']['name']; to $attachment = $_FILES['attachment']['tmp_name']; as name refers to the original file name and tmp_name to the (temporary) file path on the server. And you need to specify the file on the server in AddAttachment() call.
$message = 'This for sending an attachment by using php mailer';
$Sender = 'youremail#xyz.com';
$subject = "Attachment Test";
$Recipiant = 'recipient#xyz.com';
$subject = $subject;
$host= "yourdomain.com"; // sets GMAIL as the SMTP server
$port=465; // set the SMTP port for the GMAIL server
$username="info#yourdomain.com"; // GMAIL username
$pwd="domain mail password";
include 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsMail();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = $host; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server
$mail->Username = $username; // GMAIL username
$mail->Password = $pwd; // GMAIL password
$mail->AddReplyTo($Sender,"");
$mail->From = $Sender;
$mail->FromName = $SenderName;
$mail->Sender = $Sender;
$mail->Subject = $subject;
$FileName = 'PRO1158.pdf';
$mail->Body = "<br>".$message."<br>";
if($FileName!=""){
$mail->AddAttachment("Invoices/".$FileName); // attach files
}
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
//$mail->MsgHTML($body);
$mail->AddAddress("$Recipiant","");
$mail->IsHTML(true); // send as HTML
$mail->Send();
$mail->ClearAddresses();
First you need to upload the file into a folder then place the file path in
$mail->AddAttachment();
exapmle :
$attachment="D:\uploads\img.jpg"
$mail->AddAttachment($attachment);

How to use phpMailer isSMTP on Bluehost?

It's taken me days to get the right settings so I thought I would post a php script that works on Bluehost. In initial tests using isSMTP is faster than isMAIL.
<?php
require_once '../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "box1311.bluehost.com"; // specify bluehost as outgoing server
$mail->SMTPSecure = "tls"; // sets the prefix to the server do not use ssl
$mail->SMTPDebug = 3; // comment out if you don't need debug info
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "USER#EXAMPLE.COM"; // SMTP username (your email account)
$mail->Password = "PASSWORD"; // SMTP password
$mail->Port = 25;
$mail->From = 'USER#EXAMPLE.COM';
$mail->FromName = "USER#EXAMPLE.COM";
$mail->AddAddress('CLIENT#gmail.com');
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'test message';
$body = '<!DOCTYPE html>
<html><header>
</header>
<body lang=EN-US>
<div style="text-align:center">
<h2>this is a test</h2>
</div>
</body>
</html>';
$mail->Body = $body;
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo '<h1>message sent</h1>';
}
?>
This code work for me.
include "phpmailer/class.phpmailer.php";
include "phpmailer/class.smtp.php";
$email_user = "email#host.com";
$email_password = "pass123";
$the_subject = "Title";
$from_name = "Sender";
$phpmailer = new PHPMailer();
// ---------- datos de la cuenta de correo -----------------------------
$phpmailer->Username = $email_user;
$phpmailer->Password = $email_password;
//---------------------------------------------------------------------
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Host = "box6171.bluehost.com";
$phpmailer->Port = 26;
//$phpmailer->SMTPDebug = 2;
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;
$phpmailer->setFrom($phpmailer->Username,$from_name);
$phpmailer->AddAddress("to#host.com");
$phpmailer->Subject = $the_subject;
$phpmailer->Body .="<h1 style='color:#3498db;'>Attachment:</h1>";
$phpmailer->Body .= "<h3>".$attach1."</h3>";
$phpmailer->AddAttachment($attach, "attach1");
$phpmailer->AddBCC("hidecopy#host.com", "bcc1");
$phpmailer->IsHTML(true);
$enviado = $phpmailer->Send();
if($enviado) {
echo 'email send successful';
}
2022 update
$phpmailer->Host = [your fully qualified domain]
$phpmailer->Port = 465
$phpmailer->SMTPSecure = 'ssl'
Important that $phpmailer->From and $phpmailer->Username must be the same.

php mailer function not working on aws

I have used the PHP mailer function to send a mail. This code has worked on XAMPP and 000webhost.com, but its not working on aws.
<?php
require("\PHPMailer\PHPMailer\class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "****#gmail.com"; // Enter your SMTP username
$mail->Password = "************"; // SMTP password
$webmaster_email = $_POST['email']; //Add reply-to email address
$email= "***#gmail.com"; // Add recipients email address
$name= "Recipient's name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = $_POST['name'];
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,$_POST['name']);
$mail->WordWrap = 500; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "website inquiry - Support";
$mail->Body = nl2br($_POST['message']);
$mail->AltBody = nl2br($_POST['message']); //Plain Text Body
if($mail->Send())
echo 1;
else
echo 0;
?>
Please tell me where I am going wrong.
When I am executing this code on aws..its echoing 0
Thanks in advance!
Try with throwing errors by adding "true" to constructor:
try {
new PHPMailer(true);
...your code here...
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
So perhaps there are some more informations about the error.

php mailer sender form user

i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");

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