How to send email with PHPMailer from a google mail address? - php

I have tried to use PHPMailer to send an email from localhost (XAMPP Installation).
my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$body = "Message Body";
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemailaddress#gmail.com"; // SMTP username
$mail->Password = "secret"; // SMTP password
//$mail->IsSMTP(); // telling the class to use SMTP
$mail->SetFrom('from#address.com', 'First Last');
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('myemailaddress#gmail', 'First Last');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Please note that whenever I use myemailaddress#gmail.com in the above, it is replaced for my real email address, same goes for password secret.
I get following error
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
going to above mentioned website did not help. I tried following:
SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed

Related

I can't send mail using PHPMailer in Laravel

I am trying to send mail to reset password. But I faced one problem.
I used laravel, and I am using phpmailer to send mail.
However, mail is not sent. Time out error happens.
My code is as follow:
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('myemail#gmail.com', 'My name');
$mail->addAddress($request->email);
$mail->isHTML(true);
$mail->Subject = 'Reset Password';
$mail->Body = 'HTML message body in <b>'.$token.'</b> ';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';
$mail->send();
echo "Mail has been sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
The errors like this:
2021-08-03 13:29:47 SMTP ERROR: Failed to connect to server: Connection timed out (110)
Invalid hostentry:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
If you have the solution, please answer for me!
Thanks.

Authentication Error WHile Sending Mail in php

i am trying to send a mail through php..
i have tried through php's mail function and phpmailer() function too.
but i'm not able to send it
i have tried by changing settings in php.ini tooby setting port no. to 465,25
and some more settings by getting help over the net but still my mail is not working, my code
<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
date_default_timezone_set('asia/calcutta');
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$body = "testing message";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = $_POST["u"]; // GMAIL username
$mail->Password = $_POST["p"]; // GMAIL password
$mail->SetFrom($_POST["u"], 'First Last');
$mail->Subject = "hello";
$mail->MsgHTML($body);
$address = $_POST["to"];
$mail->AddAddress($address, "info");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
i have an other page taking username,password,sender's email and getting dem on dis page.and the error i am getting is something like this:
Mailer Error: The following From address failed: s********#g***l.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp
SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 https:/support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp
SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 https:/support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp
sometimes i also get an error message saying:
called mail() without being connected mailer error in php
please help me anyone....
And Thanks in advance
This is my phpMailer.
Hope it will help you
require RB_ROOT.'/PHPMailer-master/PHPMailerAutoload.php';
define('GLAVNIMAIL', 'yoursMail#gmail.com');
define('PASSMAIL', 'xxxxxxxxx'); // enable 2 way notification on gmail to get this code
$mail = new PHPMailer;
//$mail->SMTPDebug = 4;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = GLAVNIMAIL;
$mail->Password = PASSMAIL;
$mail->From = GLAVNIMAIL;
$mail->FromName = 'Title From';
$mail->isHTML(true);
$mail->addAddress($email, 'Nov Korisnik'); // Add a recipient
//$mail->addReplyTo($email, $korpaime.' '.$korpaprezime);
//$mail->addCC('cc#example.com');
$mail->addBCC(GLAVNIMAIL);
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->Subject = 'Registracija korisnika '.$email;
$mail->Body = $bodyMail;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
die;
} else {
echo 'OK poslat mail';
This is link for PHP MAILER
Hope it helps

I want to send email from my gmail account using php i am searching for answer on net since 4 hours but error appears

I am using wamp server and I have following code in my final.php. I am also
adding error that appears as below after code as comment kindly check that as.
I am newer to php. I am finding simple code so that I can sent message through my account, but I don't understand why this code do not have option for password.
Is password require? Please help me on this with detail and I also need to send email to multiple users simultaneously.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "63yogesh#gmail.com";
$mail->AddAddress("11ce028#charusat.edu.in");
$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.';
}
?>
//The following From address failed: 63yogesh#gmail.com Message was not sent.Mailer error: The following From address failed: 63yogesh#gmail.com
SMTP server error: 5.7.0 Must issue a STARTTLS command first. jw8sm18271654pbc.73 - gsmtp
The example here says you have to set several data, such as authentication:
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
and SMTP port number:
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
EDIT:
This is the Basic Example using SMTP from their official site, try to edit it with your infos
require_once('../class.phpmailer.php'); //include("class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.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->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

unable to connect while sending the mail in php using the PHP mailer

I downlaoded a mailer file from https://github.com/PHPMailer/PHPMailer and tried to used in my program to send email, but it lead me to some error . first let have a look at the code
include 'PHPMailer-master/class.phpmailer.php';
include 'PHPMailer-master/class.smtp.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 = 'ssl://smtp.gmail.com';
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "........#gmail.com";
$mail->Password = "*****";
$mail->SetFrom('.....#gmail.com');
$mail->Subject = "Test";
$mail->Body = "this the mail for subscription";
$mail->AddAddress('......#gmail.com');
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
after running this code i get the error message as:
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (1546610735)SMTP Connect() failed. Mailer Error: SMTP Connect() failed.
please let me know the problem, as i have seen other post...but i did not get any hint
Just replace
$mail->Host = 'smtp.gmail.com';
No need to specify the SSL here.
for mailing with phpmailer use code like below
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
// optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
You may get complete help from here

PHP to connect to Hotmail to send email?

currently I am trying to use PHPmailer to send email out. Here are the codes below
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer(); // ---------- adjust these lines --------------------------- ------------
$mail->Username = "(username#hotmail.com)"; // your hotmail user name
$mail->Password = "password";
$mail->AddAddress"(username#hotmail.com)"; // recipients email
$mail->FromName = "test"; // readable name
$mail->Subject = "Subject title";
$mail->Body = "Here is the message you want to send to your friend.";
//-----------------------------------------------------------------------
$mail->Host = "smtp.live.com"; // GMail
$mail->Port = 25; $mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
I've tried SSL, port 587 for smtp.live.com with PHPMailer, why doesn't it work?
The error is "SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."
I cannot telnet smtp.live.com 25,587.
smtp.gmail.com etc etc.. What should i do? :(
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$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.live.com"; // sets hotmil as the SMTP server
$mail->Port = 587; // set the SMTP port for the hotmail server
$mail->Username = "useyourownemail#hotmail.com"; // hotmail username
$mail->Password = "useyourownpassword"; // hotmail password
$mail->SetFrom('yourname#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (hotmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "anemail#domain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Talha answer is works for me. Try to comment $mail->IsSMTP(); and I also commented this part $mail->Port = 587;
port 587 worked for me.
no need to run IsSMTP(). Comment it out as it will throw exceptions.
Dont forget to mark it as answer if it solves your problem :)

Categories