How to send mail using php to gmail account using SMTP - php

I am trying code in php to send mail using SMTP.I am using xampp server to run php code. I am sending mail from neelabhsingh1986#gmail.com to neelabhsingh1000#gmail.com. I got the php code from this site and github. But I am getting message like
SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
Php code to send mail
<?php
require("D:xampp/htdocs/PHPMailer_5.2.0/class.PHPMailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "neelabhsingh1986#gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "neelabhsingh1986#gmail.com";
$mail->FromName = "Neelabh Singh";
$mail->AddAddress("neelabhsingh1000#gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$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;
exit;
}
echo "Message has been sent";
?>

In my setup I also have this:
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
Note that you will probably need to authorise this action on your account. You'll get an email from gmail with a link.

This the answer for my post. Please see the link. Download PHPMailer_5.2.0.zip and unzip. I am using xampp server and I created folder in D:\xampp\htdocs\ with name phpMail. I copied these two files(class.phpmailer.phpclass.smtp.php ) from PHPMailer_5.2.0 to phpMail. Now make file sendMail.php and paste following code.
<?php
require("class.PHPMailer.php");
$mail = new PHPMailer();
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourEmailAddress#gmail.com"; // SMTP username, sender email address
$mail->Password = "yourGmailPassword"; // SMTP password
$mail->From = "yourEmailAddress#gmail.com";
$mail->FromName = "YourName";
$mail->AddAddress("Receiver#gmail.com"); // Write the email of receiver. Who will get the mail.
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$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;
exit;
}
echo "Message has been sent";
?>
After writing the code your have to run server. In My case It was http://localhost/phpMail/sendMail.php. When You run this code from browser the you will get one mail regarding this app like Google Account: access for less secure apps has been enabled. When you click this link they asked for permission, Access for less secure apps click yes if you want. Thanks to #rjdown for help.

Related

PHPMailer : SMTP host connection error on Godaddy

Mailer Error: SMTP Error: Could not connect to SMTP host.;
this error is generating when i run this code in godaddy.
<?php
require("PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtpout.secureserver.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#domain.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From = "username#domain.com";
$mail->FromName = "User";
$mail->AddAddress("Sendto#gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$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;
exit;
}
echo "Message has been sent";
?>
Try below code it's working for me!
<?php
require("src/PHPMailer.php");
require("src/SMTP.php");
require("src/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
//Recipients
$mail->setFrom('from#gmail.com', 'Mailer');
$mail->addAddress('to#gmail.com', 'XYZTABC');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "<pre>";
print_r($e);
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
If i understand correctly, using the SMTP address you are using you need to specify the port too (465).
However, according to the godaddy documentation, you should be using the following SMTP server:
relay-hosting.secureserver.net
This code may help you.
$mail->Host = "smtpout.secureserver.net";
your host name should be like mydomain.com
$mail->Port = 465;
i solved this using localhost as smtp server, port 25, no ssl and no authentication.
I'm using wordpress, with divi theme on a godaddy server.

phpmailer : Could not connect to SMTP host [duplicate]

This question already has answers here:
Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
(13 answers)
Closed 6 years ago.
This is sample code from phpmailer file. I edit some of code and it gives me error. i tried to change port number as well but gives me same error ever time
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mygmail#gmail.com"; // SMTP username
$mail->Password = "password_is_correct"; // SMTP password
$mail->From = "mygmail#gmail.com";
$mail->FromName = "Harsh Patel";
$mail->AddAddress("mygmail#gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$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;
exit;
}
echo "Message has been sent";
?>
Try with these changes as you are using gmail smtp
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';

PHPMailer Authentification failed

At first; Sorry for my bad english skills. I allready try to improve them.
Here is my Problem. I want to send some mails using phpmailer. But the mailer can't authentificate with every of my mail accounts. the funny thing is two days ago everything worked well.
At least i allready tried the script from the readme file and entered my email information but it still dont work.
I have no idea what to do now and hope that you may help me
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp1.MyServer.de"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mymail#example.com"; // SMTP username
$mail->Password = "mypass"; // SMTP password
$mail->From = "mymail#example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("mymail2#example.com", "test");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$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;
exit;
}
echo "Message has been sent";
Add below lines and try:
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
or
$mail->SMTPSecure = "tls";
$mail->Port = 587;
Hope this helps

how to send mail in php?

can anyone provide me the way of sending mail through php with attached object as well .plz i am new in this kindly help me in this. is there any server to be installed for this? the mail should this on any email account aswell plz help me in this.can any one provide me the link of tutorial i used the tutorial HERE it display me the error Fatal error: Call to undefined function IsSMTP() in C:\wamp\www\EMS3\mail.php on line 13 plz help me in this
There is an error in the example provided there. Use the following code.
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
update you also need to set the external SMTP server your using. if your using google. i believe its smtp.gmail.com
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Secure = "ssl";
You can use an external PHP library for sending mail, which I have used internally on a localhost, but still configured the parameters to send to external sources. The package is Swift Mailer.
In localhost you can't send any mail. After hosting only this is possible.

Sending an email using PHPMailer and GMAIL SMTP

I have read every example out in the web and I still cant seem to connect to the GMAIL SMTP. Here is the code that I am running:
include("phpMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myUsername"; // SMTP username
$mail->Password = "myPassword"; // SMTP password
$mail->SMTPDebug = 1;
$webmaster_email = "webMasterEmail#gmail.com"; //Reply to this email ID
$email="someone#gmail.com"; // Recipients email ID
$name="SomeonesName"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Me";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
I tried setting the port in here and I also have the current set up with the following in the class.smtp.php file:
$host = "ssl://smtp.gmail.com";
$port = 465;
I keep getting the same error and I have made sure that ssl is enabled. The error I get is:
SMTP -> ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it.(10061)
As I mentioned above I had already enabled ssl by getting rid of the ; on the line with php_openssl.dll and restarting Apache. I did some more reading and I found out that some people also had "[PHP_OPENSSL]" before the enabling command. I added it and restarted Apache and everything is working! Thanks for all the comments
In php.ini:
[PHP_OPENSSL]
extension=php_openssl.dll

Categories