how to send mail using phpmailer with smtp.office365.com - phpmailer

I am using below code to send mail but this is not working for me. Please help me to resolve this problem.
require 'class.phpmailer.php';
require 'class.smtp.php';
$subject="Test PHP mailer";
$message="Hi";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier - TLS/SSL
$mail->Host = "smtp.office365.com"; // sets GMAIL as the SMTP server - smtp.gmail.com
$mail->Port = 587; // set the SMTP port for the GMAIL server - Gmail SMTP port (SSL): 465 **** Gmail SMTP port (TLS): 587
$mail->Username = ""; // other email address
$mail->Password = ""; // password
$mail->SetFrom("customercare#test.com", "John");
$mail->addReplyTo('customercare#test.com', 'John');
$mail->Subject = $subject;
//$mail->IsHTML(true);
$mail->MsgHTML($message);
$mail->AddAddress('abc#example.com');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
It display error message:
Mailer Error: SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: 5.7.60 SMTP; Client does not have permissions to send as this sender SMTP code: 550

Looking at the error message, it's not a problem with the code, but a problem with the permissions for the user your are trying to send as.
You will either need to get the user you're trying to send as to allow their account to be used in such a way, or you'll have to find a different smtp service to use.

Related

EOF caught while checking if connected

I would like to send an email from localhost by using PHP Mailer.
i have already look for others question and that solve some of the error that i got.
Except for this last error.i did not find the answer that fit to solve the error. I am really hope for help to solve this error.
This is what being diplayed after try to run the code.
2018-08-03 04:57:44 SERVER -> CLIENT:
2018-08-03 04:57:44 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
Email could not be sent. Mailer Error: SMTP Error: Could not authenticate.
This is my code
<html>
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('test#gmail.com', 'Mailer');
$mail->addAddress('test#gmail.com', 'Naura'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email testing';
$mail->Body = 'Hi. This is a test email.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Email has been sent';
} catch (Exception $e) {
echo 'Email could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
</html>

PHPMailer can't send via postfix

I already setup postfix in my debian linux VPS.
I can send email via ssh console using postfix.
I can send email using PHPMailer using my gmail account.
I can receive email in my debian linux VPS account.
Then I want to send email using PHPMailer via Postfix using my debian linux vps account. But it's failed with log below.
SERVER -> CLIENT: <br>
CLIENT -> SERVER: EHLO android<br>
SERVER -> CLIENT: <br>
SMTP ERROR: EHLO command failed: <br>
SMTP NOTICE: EOF caught while checking if connected<br>
SMTP Error: Could not connect to SMTP host.<br>
How to send email using PHPMailer using postfix ? should i use sasl ?
My sendmail.php
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Subject = 'hello postfix phpmailer';
$mail->msgHTML(file_get_contents('contentemail.html'), dirname(__FILE__));
$mail->Host = 'yyyy.zzzz.com';
$mail->Username = "xxxx";
$mail->setFrom('xxxx#yyyy.zzzz.com', 'Ceramah Islam');
$mail->addReplyTo('xxxx#yyyy.zzzz.com', 'Ceramah Islam');
$mail->Password = "aaaaaa";
$mail->addAddress('bbbb#gmail.com', 'bbbb');
$mail->send();
I think the problem is caused by the "snakeoil" certificate and private key that come with Postfix. The trick is not to verify them. Here is what I found worked:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "localhost";
$mail->Port = 25;
$mail->SMTPSecure = "tls";
$mail->SMTPOptions = array
(
'ssl' => array
(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('server#example.org', 'My Server');
$mail->addAddress('user#example.com', 'My User');
$mail->Subject = 'Message from PHPMailer and Postfix';
$mail->Body = 'Whatever';
if ($mail->send())
// SMTP message send success
{
// Put success logic here
}
else
// SMTP message send failure
{
// Put failure logic here
}

try to use phpmailer but it not work: SMTP Error: Could not authenticate

$mail= new PHPmailer();
$mail-> IsSMTP();//telling the class to use SMTP
$mail->Host = gethostbyname('smtp.gmail.com');//SMTP server
$mail->SMTPDdebug =2; //enables SMTP debug information
$mail->SMTPAuth = true;//enable SMTP authentication
$mail->SMTPSecure = "tsl";//set the prefix to the server
$mail->Port =587;//set the SMTP port for thr GMAIL server
$mail->Username = "andriyvyhor#gmail.com";//GAMAIL username
$mail->Password ="andriyvyhor";
$mail->SetFrom=("andriyvyhor#gmail.com");
$mail->AddAddress("andriyvyhor#gmail.com");
$mail->Subject="TEXT";
$mail->Body="You sell your shares";
if(!$mail->Send())
{
echo "Mailer erro:".$mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
}
am getting this error
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

Failed to connect to server: Connection refused (111)

I am using PHPMailer to send the smtp emails. If I use the same setting in a WordPress site, it works fine. But my priority is to use in a custom php page. And there, it is giving the following errors
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
My Code is here below
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'emails/PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "mzubim#gmail.com";
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('mzubim#gmail.com', 'Zubair Mushtaq');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#gmail.com', 'Secure Developer');
//Set who the message is to be sent to
$mail->addAddress('abulogics#gmail.com', 'Abulogicss');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("convert HTML into a basic plain-text alternative body");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
try this its working fine
<?php
include "emails/PHPMailer/PHPMailerAutoload.php";
//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
//Username to use for SMTP authentication
$mail->Username = "#gmail.com";
$mail->Password = "";
//Set who the message is to be sent from
$mail->setFrom('mzubim#gmail.com', 'Zubair Mushtaq');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#gmail.com', 'Secure Developer');
//Set who the message is to be sent to
$mail->addAddress('abulogics#gmail.com', 'Abulogicss');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("convert HTML into a basic plain-text alternative body");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
the PHPMailer docs says that if it
-> //TCP port to connect to; use 587 if you have set SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS
And in your case u used ssl.
Consider increasing the debugging whilst you have the problem:
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL ;
(see PHPMailer Debug messages)
My issue finally turned out to be the ports on my mail requesting server were not open to even get to the mail server (i.e. fix the firewall).

phpmailer No Relay Access Allowed Error

My website is hosted on godaddy, and I just figured out that my website is no more sending emails through phpmailer since last few months. I uploaded the latest version of phpmailer but still no success. The online web mail of my website runs fine. If I use php's "mail" function, it does send emails to gmail, but not to yahoo accounts.
I tried all the three ports 25, 465, and 587, but no luck
I am getting the following error from phpmailer:
SERVER -> CLIENT: 554 p3plsmtpa07-10.prod.phx3.secureserver.net ESMTP No Relay Access Allowed From 50.63.196.51
CLIENT -> SERVER: EHLO lostandfound.pakproject.com
SERVER -> CLIENT:
SMTP ERROR: EHLO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is my code that I am trying to test. (User name, passwords, emails are changed...)
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtpout.... my_server";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "here_i_used_my_website_email";
$mail->Password = "here_password";
$mail->setFrom('website_email', 'From name');
$mail->addReplyTo('website_email', 'From name');
$mail->addAddress('another_email', 'name_used_here');
$mail->Subject = 'About the task';
$mail->Body = 'This is email body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
It worked for me.
Keep in mind
https://co.godaddy.com/help/mail-server-addresses-and-ports-for-business-email-24071**
I was able to resolve my issue with the following code/settings of phpmailer
<?php
$recipient = "abc#def.com"
$subject = "Subject here";
$emailBody = "This is body";
// PHP MAILER CODE STARTS FROM HERE //
require '../phpmailermaster/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpout.secureserver.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#yyyy.com'; // SMTP username
$mail->Password = '3344123'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465;
$mail->Port = 80; // TCP port to connect to [THIS PORT ALLOWING EMAILS]
$mail->setFrom('xxx#yyyy.com', 'hello');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($recipient); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $emailBody;
$mail->AltBody = $emailBody;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
// PHP MAILER CODE ENDS HERE ==
?>

Resources