Send mail through SMTP error in php - php

I want to send mail with php my codes are these;
<?php
include 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'mertcankarayilan#gmail.com';
$mail->Password = '*******';
$mail->SetFrom($mail->Username, 'Pegasus Havayolları?');
$mail->AddAddress('mertcankarayilan#hotmail.com', 'mertcankarayilan');
$mail->CharSet = 'UTF-8';
$mail->Subject = 'Ekomak';
$content = 'mailde yazan';
$mail->MsgHTML($content);
if($mail->Send()) {
echo "<script>window.location.href='islem-basari.php#gosterb';</script>";
} else {
echo 'Mail gönderilirken bir hata oluştu: ' . $mail->ErrorInfo;
}
?>
and I see a error like this:
The following From address failed: mertcankarayilan#gmail.com : MAIL not accepted from server
SMTP server error: 5.7.0 Must issue a STARTTLS command first. k6sm56314517eep.17 - gsmtp

you must enter method 'TLS':
$mail->SMTPSecure = "tls";

Related

Can't send email using PHPMailer using Gmail(Failed to connect server)

i'm trying to send an email using PHPMailer. I've seen some topics about sending an email by Gmail servers using this package but i couldn't succeed.
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; // or 587
$mail->Username = "myEmailAddress#gmail.com";
$mail->Password = "MyGmailPassword";
$mail->setFrom('myEmailAddress#gmail.com', 'First Last');
$mail->addAddress('MyTargetEmail#example.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'Text to be sent';
if(!$mail->send()){
echo 'message was not sent: ' . $mail->ErrorInfo;
}
else{
echo 'Successfully sent';
}
my response :
2018-11-29 14:56:37 SMTP ERROR: Failed to connect to server: (0)
<br>
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
<br>
message was not sent: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
You are sending the email to the wrong port/configuration. In your code you have it sent to Port 587 which handles stmp connections but you are sending it configured over ssl (which would need to be sent over port 465).
You ONLY need to change where it says ssl to "tls" and it will work.
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; // or 587
Try this with composer
require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;enter code here
$mail->Username = '#gmail.com';
$mail->Password = '';
$mail->setFrom('#gmail.com', '');
$mail->addReplyTo('#gmail.com', '');
$mail->addAddress('#gmail.com', ' Name');
$mail->Subject = 'This for Testing PHPMailer';
//$mail->msgHTML(file_get_contents('message.html'), __DIR__);
$mail->Body = 'This is a plain text message body';
//$mail->addAttachment('test.txt');
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'The email message was sent.';
}
?>

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting while in hosting

While running the PHPMailer in localhost mail will be sending but while hosting the PHPMailer shows the error as SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting. please help me.
here is my code
<?php
//include PHPMailerAutoload.php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'mail#gmail.com';
$mail->Password = 'password';
$mail->setFrom('mail#gmail.com', 'mail');
$mail->addAddress('mail#gmail.com');
$mail->Subject = 'SMTP email test';
$mail->Body = 'Thanks for commenting';
if ($mail->send())
echo "mail is sent";
else
echo $mail->ErrorInfo;
?>
Ok let's make it clearly :
Make sure your SMTP Port code is same with SMPT port in your host
Auth to your account is not wrong
Check phpmailer class are success to call like vardump or something else
Sample of my Code :
require(https://example.com/PHPMailerAutoload.php');
require(https://example.com/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'mail.example.com';
$mail->SMTPAuth = TRUE;
$mail->Username = 'customer_service#example.com';
$mail->Password = 'blablabla';
$mail->From = 'customer_service#example.com';
$mail->setFrom('customer_service#example.com', 'My Cool Website');
$mail->AddAddress('recipient#destination.com', 'Recipient Full Name');
$mail->WordWrap = 70;
$mail->Subject = 'PHP Mailer';
$mail->Body = 'Awesome';
$mail->IsHTML(TRUE);

SMTP connect() failed error on localhost

I have just started coding in PHP. I'm trying to send a mail but it throws an error Mailer Error: SMTP connect() failed.
Below is the code i am using :
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->AuthType = 'LOGIN';
$mail->Username = 'abc#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('abc#gmail.com','Abc') ;
$mail->addReplyTo('abc#gmail.com');
$mail->addAddress("abc#gmail.com",'No REPLY');
$mail->isHTML(true);
$mail->Body = "12345";
$mail->Subject = "<b>OTP for password reset<b>";
$mail->Body = "Hi! Your OTP is : ";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo 'Message has been sent.';
}
Any help would be appreciated.
Change from
$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
to
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
And check for open_ssl enabled
Open your php.ini and search for this line
;extension=php_openssl.dll and remove the semi colon before it , save the file and restart your webserver.

PHPMailer SMTP ERROR: Failed to connect to server

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Username = "123#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "123#gmail.com";
$mail->FromName = "Webmaster";
$mail->AddAddress("asd#hotmail.com");
$mail->AddReplyTo("123#gmail.com", "Webmaster");
$mail->IsHTML(true);
$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.';
}
?>
It returns an error
2016-04-01 08:41:43 SMTP ERROR: Failed to connect to server: (0)
2016-04-01 08:41:43 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I am hosting this php on my xampp local server. The extension=php_openssl.dll on php.ini is already uncommended.
Your configuration might be wrong. I believe that if you change your host into smtp.gmail.com it might solve your problem.
I noticed that you set security tls but you want to connect with ssl as well.
Change $mail->Host = "ssl://smtp.gmail.com"; into $mail->Host = "smtp.gmail.com"; and security to ssl.
From this answer:
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

PHPMailer is not working at all

I'm trying to configure PHPMailer I've uploaded 1 file which is class.phpmailer.php and created another php file with this content:
<?php
require('class.phpmailer.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("the same email address");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("the same email address");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
and i get nothing at all not a success message nor a failure message. http://www.mawk3y.net/mailtest/test.php
all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:
<?php
include_once(FCPATH.'PHPMailer/src/PHPMailer.php');
include_once(FCPATH.'PHPMailer/src/SMTP.php');
include_once(FCPATH.'PHPMailer/src/Exception.php');
$msj="My complete message";
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//authentication SMTP enabled
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
//indico el puerto que usa Gmail 465 or 587
$mail->Port = 465;
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx#xxxxx.com","Name");
$mail->AddReplyTo("xxx#xxx.com","Name Replay");
$mail->Subject = "Test";
$mail->MsgHTML($msj);
$mail->AddAddress("xxxxxx#xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
The problem is with the require method.
You first have to extract all file of phpMailer repository.
instead of writing
require('class.phpmailer.php');
you need to include path where your PHPMailerAutoload.php file extracted.so you can replace it with.
require('path-of-extracted-folder/PHPMailerAutoload.php');
for more reference you can visit it's GitHub link
https://github.com/PHPMailer/PHPMailer
I had same problem but I solved.This is the way I code
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'abc#gmail.com'; // SMTP username
$mail->Password = 'abc'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Set who the message is to be sent from
$mail->setFrom('abc1#gmail.com', 'First Last');
//Set an alternative reply-to address
//Set who the message is to be sent to
$mail->addAddress('abc#gmail.com', 'Shehan');
//Set the subject line
$mail->Subject = 'Test Mail';
$mail->Body = 'This is Test Mail';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

Categories