php SMTP connect() failed while sending mail - php

I am trying to use PHPMailer to send e-mails over SMTP but so far I have had no luck. I have gone through a number of questions, PHPMailer tutorials and forum posts but I still cannot find a way to get it to work. I'm still getting an error Message could not be sent.Mailer Error: SMTP connect() failed.
Here is the code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = 'myemail#gmail.com';
$mail->Password = '************';
$mail->Port = "995";
$mail->From = 'myemail#gmail.com';
$mail->FromName = 'Tim Cullen';
$mail->addAddress('james#example.com', 'James Johnson');
$mail->addReplyTo('myemail#gmail.com', 'Tim Cullen');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo "EMail has been sent!";
//echo 'Message saved to Send folder!';
?>

You can try SMTPDebug to see what is actually happening
$mail->SMTPDebug = 2;
It looks like you're using the wrong port. Try 465 and open that port up.
iptables -A INPUT -p tcp --dport 465 -j ACCEPT

Related

PHP mailer not working on cpanel

my php mailer is working fine on localhost but when I m running same code on cpanel I m getting Error message :SMTP connect() failed
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "tls://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
$mail->Port = 587;
$mail->setFrom('sender#gmail.com', 'Mailer');
$mail->AddAddress('receiver#gmail.com', 'Joe User');
$mail->addReplyTo('sender#gmail.com', 'Information');
$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';
}
?>
Pls help me, where I am getting wrong?
CPanel blocks access to external SMTP servers by default.
Disable this restriction in whm > security center > SMTP Restrictions disable
This works
<?php
require_once('./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.mail.yahoo.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx#ymail.com";
$mail->Password = "xxxxxx";
$mail->SetFrom("xxxxx#ymail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxx#ymail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}?>
You might have forgotten to enable SMTP access (it's part of IMAP access in settings) to your GMail account.
Also, "tls://smtp.gmail.com" is not a valid SMTP server address. Use $mail->SMTPSecure = "tls"; if you want to use TLS.
Thanks for your suggestions, It's really appreciated :-)
I got the solution is as follows.
1>give Absolute path for PHPMailerAutoload.php
2>host name as "localhost"
3>create dummy emailId on server
<?php
require'/home/username/public_html/phpmailertesting/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug =3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#hostname.com'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info#hostname.com', 'Mailer');
$mail->addAddress('*******#gmail.com'); // Name is optional
//$mail->addReplyTo('info#hostname.com', 'Information');
//$mail->addCC('*******#gmail.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit();
} else {
echo 'Message has been sent';
}
?>
You need to buy Cpanel/WHM from the ISP company to enable access to external SMTP emails like Gmail. first, disable: WHM > security center > SMTP Restrictions is Disable.
and make sure CSF Firewall is "SMTP_BLOCK" setting is enabled.
if you using Gmail in your security setting enable less security which you can smtp auth.
PHPMailer recommended using SSL for all firewalls and security issues.

use phpmailer to send email from localhost to gmail: SMTP connect() failed

i want to send an email from localhost to gmail, but it says "SMTP connect() failed"
here is my code. I've also tried to change configuration php.ini and sendmail.ini but no luck.
Thank you.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'asd#gmail.com';
$mail->Password = 'password';
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('asd#gmail.com');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Email'
$mail->Body = "This is body";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Try out the following :
Ensure that the PHPMailer library is included in the code.
Ensure that you are internet connection from localhost is not inhibited by any proxy.
Happy Coding :)
check the line $mail->Subject = 'Email' for a missing ;

sending mail from xampp using phpmailer :Error occured

i am using xampp version 1.8.1 and i have also removed semicolon from php.ini file in extension=php_openssl.dll but still I am getting this error stream_socket_enable_crypto(): this stream does not support SSL/crypto and Message could not be sent.Mailer Error: SMTP connect() failed.
My php mailer code is:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'urusername#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->From = 'urusername#gmail.com';
$mail->FromName = 'Example';
$mail->addAddress('receivermail#gmail.com', 'xxxxx');
$mail->addReplyTo('receivermail#gmail.com', 'xxxxxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
kindly help me
Set the port to:
$mail->Port = 587;
Google SMTP uses port 587.

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!";
}

Trying to send mail using yahoo smtp in php

Please can some one suggest me codes to send mails using yahoo smatp, and php..
require("class.phpmailer.php"); // be sure to change this to your location!
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.mail.yahoo.com"; // sets yahoo as the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "sumthing#yahoo.com"; // yahoo username
$mail->Password = "password"; // yahoo password
$mail->From = "sumthing#yahoo.com";
$mail->FromName = "myname";
$mail->AddAddress("you#example.com");
$mail->Subject = "Test PHPMailer Message";
$mail->Body = "Hi! \n\n This was sent with phpMailer_example3.php.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
This code is giving me errrors 'Could not connect to SMTP host".
Many of the sites providing SMTP do not support SSL (SMTP-S). If supported is usually in different port, such as 465. Connecting to 465 often fails. Try commenting out
$mail->SMTPSecure = "ssl"
Try also first without authentication to make sure the connection works.

Categories