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.
Related
I tried to create a mail-confirm signup system but when I signup, database save my data but there is a error that says:
SMTP Error: Could not authenticate.
note: I am using class.phpmailer.php
I could not understand the problem. Here is my code:
sendmail.php
<?php
require 'PHPMailerAutoload.php';
function send_mail($sendto, $subject, $body)
{
$mail = new PHPMailer;
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**.****.****#gmail.com'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('e.d***.c**#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->addAddress($sendto); // Name is optional
$mail->addReplyTo('e.d****.c***#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'Please open this on Chrome';
if (!$mail->send()) {
//echo 'Message could not be sent.';
return 'Mailer Error: ' . $mail->ErrorInfo;
} else {
return 'Message has been sent';
}
}
Use port 465
port 587 if your client begins with plain text before issuing the STARTTLS command.
i'm trying to send an email using PHPMailer. I've seen some topics about sending an email by Gmail servers using this package but i couldn't succeed.
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = 587; // or 587
$mail->Username = "myEmailAddress#gmail.com";
$mail->Password = "MyGmailPassword";
$mail->setFrom('myEmailAddress#gmail.com', 'First Last');
$mail->addAddress('MyTargetEmail#example.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'Text to be sent';
if(!$mail->send()){
echo 'message was not sent: ' . $mail->ErrorInfo;
}
else{
echo 'Successfully sent';
}
my response :
2018-11-29 14:56:37 SMTP ERROR: Failed to connect to server: (0)
<br>
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
<br>
message was not sent: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
You are sending the email to the wrong port/configuration. In your code you have it sent to Port 587 which handles stmp connections but you are sending it configured over ssl (which would need to be sent over port 465).
You ONLY need to change where it says ssl to "tls" and it will work.
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; // or 587
Try this with composer
require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;enter code here
$mail->Username = '#gmail.com';
$mail->Password = '';
$mail->setFrom('#gmail.com', '');
$mail->addReplyTo('#gmail.com', '');
$mail->addAddress('#gmail.com', ' Name');
$mail->Subject = 'This for Testing PHPMailer';
//$mail->msgHTML(file_get_contents('message.html'), __DIR__);
$mail->Body = 'This is a plain text message body';
//$mail->addAttachment('test.txt');
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'The email message was sent.';
}
?>
I am trying following code to send mail
but it is showing
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
and gmail is informing me that Sign-in attempt prevented
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sss#gmail.com';
$mail->Password = '*******';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = 'abc#gmail.com';
$mail->FromName = 'asdf ';
$mail->addAddress('abc#gmail.com', 'sadf ');
$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";
?>
How to resolve above problem?
Try the following steps:
enable debug mode to catch possible errors
$mail->SMTPDebug = 1;
enable SMTP authentication
$mail->SMTPAuth = true;
also check for SSL support in php configuration file (php.ini)
extension=php_openssl.dll
You need set permission for send mail with gmail.
- Login Google Account
- Go Privacy Page
- Allow third party apps
After try this code:
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. Do the following steps.
I hope this helps.
Check if IMAP is enabled
Check here and enable less secure apps
Display Unlock Captcha
change the following in your code
isSMTP() to IsSMTP() , addAddress() to AddAddress() & isHTML() to IsHTML().
and yes check the ports also. sometoimes port also off which do not let connection to be established.
Hope it will work!
I think you have to enable POP and IMAP in you gamil. Try this
Sign in to Gmail
Click the gear in the top right.
Select Settings.
Click Forwarding and POP/IMAP.
Select Enable IMAP.
Click Save Changes.
<?
$account="email_address#gmail.com";
$password="accountpassword";
$to="mail#subinsb.com";
$from="email_address#gmail.com";
$from_name="Name";
$msg="<strong>This is a bold text.</strong>"; // HTML message
$subject="Database Backup";
/*End Config*/
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
?>
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
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.