I have been reading some older questions and I haven't found yet the solution to my problem.
Here it goes.
I'm developing a cool website with some mail functions, limited for admin users.
Right now I'm developing the site on localhost, but I've been provided with a Gmail account that will be used for the website.
I've been seeking through the web and the PHPMailer module seems a good election.
My idea is to send emails from my localhost to any other email address using the Gmail account.
Here are the codes I'm using.
For the Apache2.2 server
LoadModule ssl_module modules/mod_ssl.so
For php.ini
[mail function]
SMTP = smtp.gmail.com
smtp_port = 465
sendmail_from = admins.domaing#gmail.com
And the php codes
<?php
date_default_timezone_set("Europe/Madrid");
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$body = 'It works!';
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "admins.domaing#gmail.com";
$mail->Password = "*********";
$mail->SetFrom('admins.domaing#gmail.com', 'Admin');
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->MsgHTML($body);
$address = "user#email.com";
$mail->AddAddress($address, "user name");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Actually the error I'm getting is:
Invalid XML: SMTP -> ERROR: Failed to connect to server: (0)
Any suggestions?
Solution from the original poster:
Just change
$mail->Username = "admins.domaing#gmail.com";
to
$mail->Username = "admins.domaing";
Related
I am using PHPMailer to send mails. I have deployed my website on Google Cloud VM Instance
Here is my mail sending code
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new \PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->addAddress($_POST['email']);
$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hello, Below are the details of contact ";
$mail->Body = "<table><tr><td>Name:</td><td>".$_POST['name']."</td></tr><tr><td>Email Address:</td><td>".$_POST['email']."</td></tr><tr><td>Subject :</td><td>".$_POST['subject']."</td></tr><tr><td> Message :</td>".$_POST['$POST['msg']."</td></tr></table>";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
But the mail is not be able to send. Can anyone help me out? I can provide further code references if anyone needs
Try using SMTP to send email:
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
You have commented out isSMTP() so you are not using SMTP (so none of your SMTP settings will do anything), but the default mail() transport. This requires that you have a mail server installed in your server, which you apparently don’t.
To fix, either install a mail server (postfix is good) or enable SMTP, as chirag suggested.
All of this is covered in the PHPMailer troubleshooting guide.
I have installed php mailer using composer composer require phpmailer/phpmailer. I followed instructions from this website https://www.geeksforgeeks.org/how-to-send-an-email-using-phpmailer.
Below is my code for sending email.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try
{
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('owner#gmail.com', 'Owner');
$mail->addAddress('receipent#gmail.com');
$mail->isHTML(true);
$mail->Subject = "Hello this is subject";
$mail->Body = "Hello this is message;
$mail->send();
echo "Mail has been sent successfully!";
}
catch (Exception $e)
{
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
When I comment $mail->isSMTP(); and send mail, I get the result as message sent but I dont get it in my Gmail inbox.
when I uncomment $mail->isSMTP(); I get error message as shown in below image.
My project is hosted in godaddy server.
Even if I use php mail() function to send mail, response is mail send successfully, but it does not get delivered into my Gmail inbox
If you want to use Gmail, you should follow the steps here to manually add Google MX records to your GoDaddy account:
https://uk.godaddy.com/help/point-my-domains-email-service-to-google-7936
Unfortunately it takes some days for changes to be reflected.
Then you need to disable security features, change your php mailer config like:
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
Hope this solves.
------EDIT------
Later I figured out that the emails go to spam folder when the port is 25. I changed the connection to TLS and now I receive them as normal.
Here is the code from my website, it works on Godaddy, I receive e-mails to my gmail account.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'true';
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "********#gmail.com";
$mail->Password = "**********";
$mail->SetFrom("********#gmail.com");
$mail->AddAddress("********#gmail.com");
$mail->Subject = " your subject";
$mail->Body = "your body";
I have developed a PHP and MySQL webapp which also uses phpmailer..It ran without any errors when I used it in my WAMP. But When I uploaded my site to online server via EC2 instance in aws and picking redhat AMI..And everything was going well until I found that my published site won't send any mails..Instead it returns an error saying Message was not sent.PHPMailer Error: SMTP connect() failed...
Few info regarding my code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Username = "something#gmail.com";
$mail->Password = "PWofsomething";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = "something#gmail.com";
$mail->FromName = "something";
$mail->addAddress(validemail,validfirstname);
$mail->Subject = "bla bla bla";
$mail->Body = html body
if(!$mail->Send()){
echo "Message was not sent."<br />PHPMailer Error: " . $mail->ErrorInfo."<br />";
}
else{
echo 'success';
$mail->clearAddresses();
`
PS:I could add few more .conf or .ini files if you want
I'm trying to send an email from my hotmail account using PHPMailer. It's working fine from my PC but when I try it on another PC I get this error message:
2015-04-23 17:31:18 CLIENT -> SERVER: EHLO localhost
2015-04-23 17:31:18 CLIENT -> SERVER: QUIT
2015-04-23 17:31:18 SMTP connect() failed. Mailer Error
Here's my code:
<?php
require "C:\wamp\www\PHPMailer-master\PHPMailerAutoload.php";
$mail = new PHPMailer();
$mail->SMTPSecure = 'SSL';
$mail->Username = "b1sakher#hotmail.fr";
$mail->Password = "rerered";
$mail->AddAddress("b1sakher#hotmail.fr");
$mail->FromName = "My Name";
$mail->Subject = "My Subject";
$mail->Body = "My Body";
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
{
echo "Mailer Error";
}
else
{
echo "Message has been sent";
}
?>
Firstly you should add 'Debugging Mode' to your code, this will tell you where you are going wrong.
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPDebug = 2; //Alternative to above constant
I had a similar issue, but found out it was the CFS Firewall in Cpanel/WHM blocking the port.
Login into WHM.
Go to ConfigServer Security & Firewall inside plugins option.
Click on Firewall configuration.
Filter by SMTP Settings.
Look for SMTP_ALLOWUSER option and add the Cpanel account username separated by coma.
Restart the firewall.
If you don't have access to WHM ask your hosting provider.
I used this coding to send mail using gmail, but am getting the below error, here when i removed the ssl part, its working, but i dint receive any emails(checked inside spam also). Any other problems ?
$mail = new phpmailer;
$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"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "un"; // GMAIL username
$mail->Password = "pwd"; // GMAIL password
$mail->SetFrom('test#gmail.com', 'Bharanidharan');
$mail->AddReplyTo("test#gmail.com","Vishal Kumar");
$mail->AddAddress("test#gmail.com","Bharani");
$mail->Subject = "Hey, check out";
$body = "hey, check email";
$mail->MsgHTML($body);
$address = "test#gmail.com";
$mail->AddAddress($address, "test");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
Enable the mod_ssl in Apache (search the config file) and the openssl PHP extension (check php.ini)