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.
Related
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
I need to send email using Office365 mail server from may webpage where user enters their email address.My configurations are as follows:
require("phpMailer/PHPMailerAutoload.php");
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = trim($username);//username
$mail->Password = trim($password);//password
$mail->SMTPSecure = 'tls';
$fromName = 'Test';
$from = $myemail;
$mail->AddReplyTo($from, $fromName);
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress($semail);//mail_id which i need to send mail
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($_body);
$mail->IsHTML(true); // send as HTML
When I run the page,it gives following error message
Connection: opening
SMTP ERROR: Failed to connect to server:
php_network_getaddresses: getaddrinfo failed:
Name or service not known (0)
SMTP connect() failed.
**Fatal error**: Uncaught exception 'phpmailerException' with message
'SMTP connect() failed.
When I use gmail server configuration details(host:smtp.gmail.com) ,this is working fine.But my client mail server is with Office365.
I have used the ping command in command prompt for the mail server.
ping mail.mailservername.com <enter>
It gives IP address and another address for the above mail address.I have used the new mail address for the host variable. It works
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 trying to use gmail smtp server to send emails via php mailer from a php form. i have opened the 465 port from windows firewall but the port doesn't appear in the cmd output when i type
netstat -an
command.the error that appears in the php page is:
Invalid address: SMTP Error: Could not connect to SMTP host.
there is no exact solution for my issue in the result of my searchs. my code is:
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
define('GUSER', 'from#gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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 = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}}
if(smtpmailer('from#gmail.com', 'to#gmail.com', 'name', 'test mail message', 'Hello World!')){
echo "sent";
if (!empty($error)) echo $error;}
?>
note: i am using localhost for my site
Try using port 25 or 587, also check your apache log.