Connection error while sending email from host server - php

I have created a web application in php . I am using PHP Mailer 5.20 class. it work fine when I use XAMPP server on my local server but when we host it on a web-server and try to send a email the it return an error i show you the error below
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host.
I tried all configuration and check all but I receive the same error on the screen.
My PHP mailer class is below
<?php require("PHPMailer_5.2.0/class.phpmailer.php");
function send_email($tomail,$message,$subject)
{
//$dbObj = new Database();
//$dbObj->connect_db();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mail.yahoo.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
/////////////////////////////////( USER NAME AND PASSWORD )/////////////////////////////
$email = "xyxxxxx#yahoo.in";
$mail->Username = $email; // SMTP username
$mail->Password ="xxxxxx"; // SMTP password
$mail->From = $email;
$mail->AddAddress($tomail, $tomail);
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody =$message;
$mail->Subject = $subject;
$mail->FromName ="xyz (Online Booking Info)";
if(!$mail->Send())
{ $rr= $mail->ErrorInfo;
exit;
}
}
?>
Can some body tell me where am I wrong or miss configuration?

Related

phpmailer stop sending emails after sending one email

Hi I am using phpmailer 5.2 on ubuntu , I simply download the zip file extract it and upload the folder to my project folder. It working fine when send the first email but after sending one email it stops sending emails with connection time out error. Then maybe after half or one hour can send one email again and again it will stop working .This is the code:
require_once('path/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.abc.com"; // SMTP servers change to localhost
$mail->Smtp_port ="465"; // change smtp port
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "admin#abc.com"; // SMTP username
$mail->Password = "abc"; // SMTP password
$mail->CharSet="UTF-8";
$mail->From = $from;
$mail->FromName = $from_name;
$mail->SMTPDebug = 3;
$mail->AddAddress($to);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->Send())
{
$error = 'Mail error: '.$mail->ErrorInfo;
//echo $error;
return $error;
}
else{
$error = 'Message sent!';
// echo $error;
return $error;
}
I get this error: SMTP -> ERROR: Failed to connect to server: Connection timed out (110)The following From address failed: admin#abc.com : Called Mail() without being connected.
I stuck on this for 3 days already. I do not know how what is wrong. I will be very grateful if someone can help.thanks

PHPMailer, SMTP Failed to connect to server

I'm using PHPMailer.
But when i upload to my host i get the error SMTP connect() failed.
My environment
1. Server OS:CentOS 7
2. Web Services: XAMPP 5.6.8
3. PHPMailer:5.2.4
this is my code:
<?php
require_once('PHPMailer_old/class.phpmailer.php');
try{
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->SMTPAutoTLS = false; // close TLS
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "Test#gmail.com"; // GMAIL username
$mail->Password = "Test"; // GMAIpassword
$mail->FromName = "Test Manager";
$mail->From = "Test#gmail.com";
$to = "Test#hotmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->WordWrap = 80;
$body = "success!";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
echo "Message has been sent.";
}catch(phpmailerException $e){
echo "Sending Failed";
echo $e -> errorMessage();
}
?>
Error Message In web:
SMTP -> ERROR: Failed to connect to server: (0)
Sending FailedSMTP Error: Could not connect to SMTP host.
Thanks Everyone Help!
My Solution is uninstall XAMPP and respectively install apache, PHP, MySQL

Unable to connect to SMTP server

I have a server with mail support, say example.com. I configured the server and added MX records via cpanel, so that I can receive and send mails via outlook.com with address myaddr#example.com. The MX records are got from domains.live.com.
Now I need to send mail programmatically using PHP using SMTP. I tried PHPmailer using the following script. But it is showing the error
Mailer Error: SMTP Connect() failed.
(But I can send and receive emails via outlook.com using myaddr#example.com)
$body = $_POST['message'];
$to = "support#example.org";
$from = 'fromAddress#gmail.com';
$fName = 'first name';
$lName = 'last name';
$subject = 'my subject';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
// $body = eregi_replace("[\]",'',$body);
$mail->Host = "mail.example.org"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "myaddr#example.org"; // SMTP account username example
$mail->Password = "password";
$mail->SetFrom($from, $fName.' '.$lName);
$mail->Subject = $subject;
$mail->AddAddress($to, "Support Team");
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
How can I resolve the issue.
Finally I just solved the issue by replacing some of the settings as below and it worked :).
$mail->Host = "smtp-mail.outlook.com"; // SMTP server example
$mail->Port = 587;
$mail->SMTPSecure = 'tls';

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate

include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******#gmail.com"; // your SMTP username or your gmail username
$mail->Password = "******"; // your SMTP password or your gmail password
$from = "***********#example.com"; // Reply to this email
$to="******#gmail.com"; // Recipients email ID
$name=" Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, 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";
}
am getting this error
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
Since you are using SSL. Add this line of code
$mail->SMTPSecure = 'ssl';
Also change your
$mail->Host = "ssl://smtp.gmail.com";
to
$mail->Host = "smtp.gmail.com";
Also, make sure if you have enabled the extension php_openssl.dll
if you are sure your username and password are correct
then do these steps
1 $mail -> SMTPSecure = 'ssl';
2 $mail->Host = "smtp.gmail.com";
3 https://support.google.com/mail/answer/78754
4 go to google acocunts then connected app and change to less secure apps on
follow all steps in number 3

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