I am trying to send an email using Mailer but getting below error
Connection: opening 2017-05-25 08:22:07 SMTP ERROR: Failed to connect
to server: (0) SMTP connect() failed. Mailer Error: SMTP connect()
failed.
php_openssl extension & IMAP both are enabled. I tried to find it on google but still no luck.
Code:
function sendMail($subject='',$to='',$emailcontent='',$attach='')
{
global $_mailmsg;
//echo $emailcontent;exit;
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->SMTPAuth = true;
$mail->Username = 'xx#gmail.com';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'ssl';
$mail->From = 'xx#gmail.com';
$mail->FromName = 'Test';
$mail->addAddress($to); // Add a recipient
if(!empty($cc)){ $mail->addCC($cc); }
if(!empty($bcc)){ $mail->addBCC($bcc); }
$mail->WordWrap = 50;
if($attach != ''){
$mail->addAttachment($attach);
}
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = 'Test';
try
{
if($mail->send()) {
return 1;
exit;
}
else
{
echo 'Mailer Error: ' . $mail->ErrorInfo;
return 0;
}
}
catch(Exception $e)
{
return 0;
}
}
This looks like your server is not permitted to connect to remote SMTP servers, something very common at big ISPs like GoDaddy. If you do the steps described in the troubleshooting guide you can figure out what's blocking you. The fact that there is no link to the guide in your error message tells me that you're using a very old version of PHPMailer, so get the latest.
PHPMailer has nothing to do with IMAP; that's for inbound mail only.
As you are using Gmail, just turn on "Allow less secure apps":
https://myaccount.google.com/u/0/lesssecureapps
And also you'll probably need to allow access to your Google account without unlock captcha:
https://accounts.google.com/DisplayUnlockCaptcha
Related
i'm developing a website to send reminders to employees in my company using phpmailer, the emails are working fine, but when i connect to the workplace internet connection it stops working with no errors but:
Connection: opening to smtp.mail.yahoo.com:587, timeout=300, options=array ( )
i'm guessing it might be a closed port, so i tried using 587 and 25.
i'm not that good in networking, is there's any way i can know what is the exact error?
function SendOutlook(){
//$recipient = 'xxxxx#yahoo.com';
require 'PHPMailer/PHPMailerAutoload.php';
$Emp_recipient = "employee#yahoo.com";
$mail = new PHPMailer;
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "smtp.mail.yahoo.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx#yahoo.com'; // SMTP username (server username)
$mail->Password = 'xxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('xxxxx#yahoo.com', 'One');
$mail->addAddress($Emp_recipient,"name"); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'test';
$mail->Body = "scheduled on your outlook calender";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit();
}else {
echo '<p>Message has been sent</p>';
}
echo !extension_loaded('openssl')?"Not Available":"Available";
}
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
<?php
require '/etc/PHPMailer/PHPMailerAutoload.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.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "my#gmail.com";
$mail->Password = "password";
$mail->SetFrom("from#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("to#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
This is my mail functionality code & it gives me the error
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No address associated with hostname
I don't have extension=php_openssl.dll in my php.ini file but still I've included this and the same issue
To avoid the problem of dynamic IP (every time i ping smtp.gmail.com I see a slight difference in the last 3digit chunk), you just have to use php built-in gethostbyname() to read the IP in real-time.
$smtp_host_ip = gethostbyname('smtp.gmail.com');
Then use that as host.
Hope it helps.
I am using the following code to sent mail for a contact form. The mail works perfectly when using a windows server setup with xampp, but fails in Linux server RHEL 5.
I am getting "SMTP Error: Could not connect to SMTP host". I tried telnet the host from terminal. It is getting connected
public function smtpmailer($to,$cc, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->IsHTML();
$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.gmail.com';
$mail->Port = 465;
$mail->Username = 'XXX#gmail.com';
$mail->Password = 'XXXXXXXX';
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->AddCC($cc);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
While Debugging is set to 1 I am getting the following error
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP Error: Could not connect to SMTP host.
Seems as if name resolving is not working. Check /etc/resolv.conf, it should contain a line like this:
nameserver 8.8.8.8
Replace 8.8.8.8 by the IP of your desired nameserver.
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.