PHPMailer Not sending Mails To Some Gmail Accounts - php

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!

Related

How to Setup up Gsuite smtp-relay and PHPMailer

Hi I've been struggling with how to setup the SMTP-relay that comes with gsuite. I have asked google support but not even they know. What is happening is when i run this code to send emails no emails show up in my gsuite not even in spam either. I have used this exact same code successfully changing the SMTP host to premium64.web-hosting.com and it works great for every email address other than sending to gsuite. I have tried sending it to a different gsuite as well and it still does not work for both the smtp-relay and for the premium64. Thinking it has to be a setting with google admin because google is really strict on security. I have enabled less secure apps. Here below is my code and the current setting. Please show me what needs to be changed so that it works. I've had a tough enough time working on this for the last 4 days. Any help is appreciated and thank you in advance. Thank you.
Phpmailer code:
$sentfromemail = "sam#email.com";
$mail = new PHPMailer();
//
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp-relay.gmail.com';
$mail->Port = '465';
$mail->isHTML(true);
$mail->Username = $sentfromemail;
$mail->Password = 'correctpassword';
$mail->SetFrom('sam#email.com' , 'Email Name');
$mail->Subject = "test";
$mail-> Body = 'test';
Google Admin Console Settings:

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: Dynamic gmail forwarding

I do not know if what I want to do is possible (but finding out that it isn't would be useful in itself).
I cannot use my company's gmail account "real.business#gmail.com" directly with PHPMailer. I can, however, use an intermediary gmail account "fake.12345.account#gmail.com" which can have "less secure apps" enabled, which permits SMTP verification.
However I do not want to have the emails be sent from this fake.12345.account#gmail.com account (wouldn't look particularly professional) - but rather the company's gmail account.
I can send the emails from the intermediary account to real.business#gmail.com; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from fake.12345.account#gmail.com to the company account.
The problem lies in how real.business#gmail.com can then successfully email the email (or at least appear to be the sender), as originally intended.
The code so far
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'fake.12345.account#gmail.com'; // gmail account username
$Mail->Password = 'a_password'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'testing.num.101#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Testing Again';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress($personEmail); // To: the PERSON WE WANT TO EMAIL
$Mail->isHTML( TRUE );
$Mail->Body = ' Good news '.$personName.'! The email sent correctly!';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
So the issue is: not having the email sent to $personEmail from fake.12345.account#gmail.com (that's trivial) but rather how to send the email from fake.12345.account#gmail.com to real.business#gmail.com such that real.business#gmail.com forwards the message to $personEmail
What you're describing is really relaying, which is usually configured in the mail server config (not the messages), but you don't have access to anything like that in gmail.
You can set allowed aliases in gmail, but I would guess that these are not allowed to overlap with existing gmail account names as that would be a major security hole. Why not enable "less secure apps" on the main account? It's not as if it is actually any less secure - if anything it's better, because the setup to use OAuth2 is so deeply complex and unpleasant...
That said, rather than trying to do all this forgery, you may be interested in this PR and associated docs. It's fairly likely the xoauth branch will get merged into master and released without any further changes as PHPMailer 5.2.11, and it would very helpful if you could give it a try.
PHPMailer is made for sending.
What you want to do is forward an email. This implies receiving the email and then sending it through.
What you need is some kind of IMAP client in php, that will allow you to read the emails on fake.12345.account#gmail.com (and maybe real.business#gmail.com). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with real.business#gmail.com.

phpMailer: Could not instantiate mail function

I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.

Email System in PHP using PHPMailer

Hi i am currently implementing an email system for a customer in php. I'm having a bit of trouble in figuring out something. Here's a sample code:
$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 = "***missing part***";
$mail->Password = "***missing part***";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
My customer already created a business email account on gmail for this website. My question is should i put this business email and password in these missing parts? Anyone could help me please? Thanks.
whose username and password should i put there?
If you want to use GMail, you need to put in the username and password that belong to the GMail account you want to send the message from.
.... which is why sending E-Mail through GMail is a bit of an imperfect solution IMO - you put your personal Google login data, with which you can access everything you do on Google, into a script on a server. It's not great practice security-wise.
It might be more feasible to create a SMTP account on the client's web site, and use that. That has the additional advantage that you can use a email#clients-domain-name.com sender address.
you must post the username and password so we can help you.. LOL, just kidding. You should put there the username and password of the account that would be sending the email. In this case, probably your webpage's gmail account.
Often web hosts don't use authentication for their web servers to send email using their smtp servers, i would suggest contacting the people who host the website and ask them about using their smtp servers.
Using gmail (if you can from a script) might end up with the rather annoying reply thing that you get from gmail where the reply address is a gmail account.
e.g. from mrKoz#gmail.com on behalf of mr#koz.com which doesn't look awesome :)

Categories