I can't send mail using PHPMailer in Laravel - php

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.

Related

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

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

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

SMTP ERROR: Failed to connect to server: Connection timed out (110)

Connection: opening to smtp.mandrillapp.com:587, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Message could not be sent.Mailer Error: SMTP connect() failed.
I used mandrillAPI to send email. It works on my localhost. I can send mails from my localhost. But when I used this on the test server it is not connecting to the mandrill server. It shows the above error. I checked the port and it is open. I can use gmail smtp from my server. It works fine. But I need to use mandrillAPI. I also added DKIM and SPF record on domain. But still not connecting to the smtp.mandrillapi.com
here is the code I used.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPDebug = 4;
$mail->Debugoutput='html';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'apikey'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example#gmail.com';
$mail->FromName = 'example';
$mail->AddAddress('example#gmail.com', 'Nidheesh'); // Add a recipient
// Name is optional
$mail->addReplyTo('example#gmail.com');
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
?>
`

PHP Mailer fails on MS Exchange

I am trying to send an email via PHP mailer and am failing miserably. The error message I am getting is as follows:
2014-08-12 12:21:40 SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060) 2014-08-12 12:21:40 SMTP connect() failed. Mailer Error: SMTP connect() failed.
My code is as follows. I do not know where I am going wrong with this. I am pretty sure all information is correct, with the exception of the port. Given this is using microsoft exchange I am using port 587 -is this where I am going wrong?
<?php
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$body = "HellooooO";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPSecure = "tls";
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;
$mail->Host = "My server name";
$mail->Port = 587;
$mail->Username = "My MS exchange email address";
$mail->Password = "Password";
$mail->SetFrom('My MS exchange email address', '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 = "test email address";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
-----------EDIT-------------------
Following Synchro's remark that I am not using the latest version of PHP Mailer, I have amended the code as follows. I am still not able to send emails and the error message is the same... How do I check whether the TLS port is open and working as expected?
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail ->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = 'My Server';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'My email address';
$mail->Password = 'My Password';
$mail->SMTPSecure = 'tls';
$mail->From = 'My email address';
$mail->FromName = 'Mailer';
$mail->addAddress('My Test Email address', 'Joe User');
$mail->WordWrap = 50;
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Regards and thank you,
G.
Not sure if this has been cured, however this is typical firewall behavior. You could use something like Wireshark to see if the server is recieving the request.

Sending mail with hotmail smtp in php

I am trying to send email through PHPMailer. And while sending it gives me error as
SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.
I really can't figure it out what's wrong. Is it the server configuration problem or code configuration problem. If anyone knows the answer please let me know.
Code below:
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(); //Initialize a new PHPMailer object;
$body = eregi_replace("[\]",'',$body); //Replace unwanted characters of the content
$mail->CharSet ="ISO-8859-1";
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->From = 'email';
$mail->FromName = 'name';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = 'smtp.live.com';
$mail->Port = '465';
$mail->Username = 'email ';
$mail->Password = 'password';
$mail->SetFrom('email', 'who');
$mail->AddReplyTo('email','who');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, '');
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Message sent successfully!";
}

Categories