PHPMailer Using Gmail Not Sending - php

I am using PHP Mailer (https://github.com/PHPMailer/PHPMailer) and for some reason it is not working with gmail. Does anyone know if there are any other settings I should be using?
function send_email($mail_content,$subject,$email,$name)
{
include_once('third-party/mail/phpmailer-master/PHPMailerAutoload.php');
$php_mailer_mail = new PHPMailer;
$php_mailer_mail->isSMTP();
$php_mailer_mail->Host = 'smtp.gmail.com';
$php_mailer_mail->SMTPAuth = true;
$php_mailer_mail->Username = 'email#gmail.com';
$php_mailer_mail->Password = 'password';
$php_mailer_mail->SMTPSecure = 'ssl';
$php_mailer_mail->Port = 465;
$php_mailer_mail->From = 'email#gmail.com';
$php_mailer_mail->FromName = 'Me';
$php_mailer_mail->addAddress($email, $name);
$php_mailer_mail->isHTML(true);
$php_mailer_mail->Subject = $subject;
$php_mailer_mail->Body = $mail_content;
$php_mailer_mail->send();
}

Make sure you enable IMAP in your gmail settings. smtp is not IMAP, but Google will discard smtp requests for your account unless that is enabled in your account settings.
You may also want to try port 587 with TLS encryption.

Do you get any errors?
if $php_mailer_mail->send(); returns false you may inspect $php_mailer_mail->ErrorInfo
Anyway you should use port 587 and tls instead of ssl.
$php_mailer_mail->Port = 587;
$php_mailer_mail->SMTPSecure = 'tls';
You may take a look at
https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Furthermore (from the example at the link above)
SMTP needs accurate times, and the PHP time zone MUST be set
This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
This line must be at the beginning of the script.
Of course double-check username and password.
Hope it helps...

Related

PHPMailer sets the From same as the smtp username

I am using PHPMailer library to send SMTP email for booking enquiry. (Note: The same problem I was facing in PHP Pear Mail library as well).
I set the email from as below
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'gmail.owner#gmail.com'; // SMTP username
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true);
$mail->Password = 'xxxx';
$mail->setFrom(mark.antony#example.com, Mark Antony);
$mail->replyTo(mark.antony#example.com, Mark Antony);
$mail->addAddress(gmail.owner#gmail.com, Gmail Owner);
$mail->Subject = $whatever subject;
$mail->Body = $whatever html;
The problem is after sending email in the received mailbox I see the From: is the same as To: (Or same as the gmail/smtp username).
I have fixed the problem on reply to by setting replyTo value.
Is there anyway I can fix this? Or is that how it suppose to be?
Google does not allow you to set arbitrary from addresses, because doing so is usually forgery. If you try to, gmail will ignore it and substitute your Username address, which is what you're seeing.
There is one exception to this: you can set up additional aliases in your gmail settings (which you need to verify), and once you've done that you can use those addresses as from addresses.
If you're using this for a contact form (as it sounds like you are), setting to and from to your own address, and the submitter as a reply-to is the correct way to go, as the PHPMailer contact form example demonstrates.

phpmailer does not respect encryption "none"

I am trying to send an email using phpmailer. I have set the encryption to "none" to send using plaintext, however, it starts TLS. I am able to telnet manually to the server and successfully send the email in plaintext, meaning that the server supports plaintext and is not enforcing TLS. Here are the settings and traffic capture:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = MAILSRV;
$mail->Port = "25";
$mail->SMTPSecure = "none";
$mail->SMTPAuth = "1";
$mail->Username = MAILUSER;
$mail->Password = MAILPASS;
$mail->IsHTML(true);
$mail->setFrom(MAILFROM, "Test");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->addAddress(MAILTO);
$mail->Subject = SUBJECT;
$mail->msgHTML(BODY);
Server sends RST after client hello. How can I configure phpmailer to send the email with no encryption?
Don't make up random stuff and expect it to work; pay attention to the correct types and values of PHPMailer properties, ideally by reading the API documentation.
$mail->Port = "25";
Port is an integer, not a string.
$mail->SMTPSecure = "none";
If you're trying to turn it off, set it to an empty string (as the source code says), not some random value, though it is off by default so you don't have to do this anyway.
$mail->SMTPAuth = "1";
This is a boolean value, and it already defaults to false.
$mail->Encoding = "base64";
This is generally a bad idea, and makes your messages more likely to be classified as spam.
But to get to the point of your question, PHPMailer uses opportunistic encryption, meaning that if your server advertises it, PHPMailer will automatically attempt to use it. This can fail in some circumstances, usually when the target mail server is misconfigured in some way, but the thing you are looking for is:
$mail->SMTPAutoTLS = false;
and the documentation on it is here.

why does "Email failed to send: SMTP connect() failed." still appear after having enabled less secure apps

My PHPMailer will not connect to my gmail account properly although I have enabled less secure apps and have checked my username and password several times.
I have enabled less secure apps to access my gmail account "tokens.tts#gmail.com". I have also tried to use both ssl and tls but neither seem to work. I have looked at several videos online and they all seem to mention enabling the less secure apps option but nothing else.
<?php
use PHPMailer\PHPMailer\PHPMailer;
$name = "Michiel Olieslagers";
$email = "tokens.tts#gmail.com";
$subject = "This is just a regular email";
$body = "I am writing this email to see whether this actually function propperly.";
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
// $mail->isSMTP();
// $mail->Host = "smtp.gmail.com";
// $mail->SMTPAuth = true;
// $mail->SMTPSecure = "tls";
// $mail->Username = "tokens.tts#gmail.com";
// $mail->Password = "hqyzZYax";
// $mail->Port = "587";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "tokens.tts#gmail.com";
$mail->Password = "********";
$mail->Port = "465";
$mail->isHTML(TRUE);
$mail->setFrom($email, $name);
$mail->addAddress("micholieslagers#gmail.com");
$mail->Subject = $subject;
$mail->Body = $body;
if ($mail->send()){
echo "Email is sent";
}
else { echo "Email failed to send: ". $mail ->ErrorInfo; }
?>
I should be receiving a test email at my second account "micholieslagers#tts.edu.sg" however this does not happen.
Looks like the new process for GMTP SMTP has a compulsion.
Please follow the below steps to achieve the same.
Make sure you enable 2-step authentication process with your mobile number before proceeding below steps.
Go to google account from this link.
You will see settings page with tabs on left panel. Click on security tab.
Click on app passwords link. Make sure here you enabled 2-step verification.
You will see select the app and device you want to generate the app password for.
Select app as mail and select your relevant device or you can choose other at last.
Click on the generate button, you will get the generated password. Copy and save it somewhere as it's one-time password otherwise you have to generate it again by creating new app password.
Now STMP configuration,
Host - smtp.gmail.com
Username - your email address for which you generated the app.
Password - Your generated password
SMTPSecure - TLS or SSL whichever work
Port - 465 for SSL and 587 for TLS
This should work as I am still using this configuration.

PHPMailer Not sending Mails To Some Gmail Accounts

I'm using PHPMailer (Version 5) for user registration. (When user registers to my site the Profile activation code is sent to user to activate it).
PHPMailer works, I tested it many times (I registered Myself with other mails and with gmail too for testing purposes, I always got the activation code), but many users complain that they not getting the activation codes and then I have to send them manually...
I can't understand what is the problem (When I checked my users database there are many users that got activation codes, but also that couldn't received).. I debug PHPMailer, but there is not any error or problem...
I'm Using PHPMailer With Gmail SMTP:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "mymail#gmail.com";
$mail->Password = "MyPassword";
$mail->From = "mymail#gmail.com";
$mail->FromName = 'www.mysite.com';
$mail->AddAddress($email);
$mail->WordWrap = 80;
$mail->IsHTML(true);
$mail->Subject = 'Registration';
$mail->Body = $message;
$mail->AltBody = $message;
$mail->Send();
I also tried to use SSL-465, but the result is the same..
Please Help!
Thank you very much in advanced...
I Solved this problem from server (host). I checked mail functions / configs and found "MAX_EMAIL_PER_HOUR" was set to 30 (Really strange, by default It is 100) So I changed it to 500 and It fixed all PHPMailer Issues...
In addition:
Enabling "Allow less secure apps" will usually solve many problem for PHPMailer, and it does not really make your app significantly less secure. (When enabling Google warns you that the App is insecure).
If you Need More security, PHPMailer added support for XOAUTH2 for google, you can use it...
I haven't used this technology before, but I'll play with it as soon as possible ;)
With Best Wishes!

Can I use gmail as smtp server for my website

Hello I am trying to get a website up and running. It is currently hosted on AWS, so I do not have my own smtp server running at this moment. So after reading a few articles, I have understood that we could used gmail as a smtp server.
I wanted to double check if what I read was right, I am going to use smart job board software, can I plug in the values provided by gmail and use that as an smtp server??
Yes, Google allows connections through their SMTP and allows you to send emails from your GMail account.
There are a lot of PHP mail scripts that you can use. Some of the most popular SMTP senders are: PHPMailer (with an useful tutorial) and SWIFTMailer (and their tutorial).
The data you need to connect and send emails from their servers are your GMail account, your password, their SMTP server (in this case smtp.gmail.com) and port (in this case 465) also you have to make sure that emails are being sent over SSL.
A quick example of sending an email like that with PHPMailer:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "john.doe#gmail.com"; // SMTP account username
$mail->Password = "your.password"; // SMTP account password
$mail->SetFrom('john.doe#gmail.com', 'John Doe'); // FROM
$mail->AddReplyTo('john.doe#gmail.com', 'John Doe'); // Reply TO
$mail->AddAddress('jane.doe#gmail.com', 'Jane Doe'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I successfully use the GMail SMTP server.
We do have a corporate GMail account, though I don't think that matters. A personal GMail account should suffice.
I do not have a PHP sample however the following configuration for ASP.Net should provide adequate guidance:
<mailSettings>
<smtp from="me#gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="me#gmail.com" password="mypassword" />
</smtp>
</mailSettings>
If someone has a suitable PHP sample, feel free to edit my answer or post your own.
Authentication is required I believe, but I don't see why not. I won't do the research for you, but there are a couple things to look into:
Their SMTP server requires TLS encryption and is hosted on a non-standard port (995). You will need to ensure that AWS supports both of these options for SMTP outbound.
There is probably a cap on emails you can send before being marked as spam. You should research this and ensure it is not beyond your requirements.
You can user PHPMailer class for this job. And you can easily configure the smtp.
An example of configuration
if (class_exists(#PHPMailer)) {
$smtp_mail = new PHPMailer();
$smtp_mail->isSMTP();
$smtp_mail->SMTPAuth = true; // enable SMTP authentication
$smtp_mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$smtp_mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$smtp_mail->Port = 465; // set the SMTP port
$smtp_mail->Username = "info#example.com"; // GMAIL username
$smtp_mail->Password = "password"; // GMAIL password
$smtp_mail->From = "info#example.com";
$smtp_mail->FromName = "Name";
$smtp_mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$smtp_mail->WordWrap = 50; // set word wrap
$smtp_mail->AddReplyTo("info#example.com","Name");
$smtp_mail->isHTML(true); // send as HTML
}
Although you can technically use Gmail as SMTP server, it is not recommended for larger websites. By time you may receive issues like "421 4.7.0 Temporary System Problem" or similar, is it was not designed to be used by an application, rather a single person.
Related issue for the error message above:
Gmail SMTP error - Temporary block?

Categories