1

I have a Heroku app which has a php script that send out an email. The email sent always goes to spam. I know that this question has been asked many times before, but I haven't been able to find a solution that helped me solve the issue for a Heroku website without a custom domain while using a gmail acount.

Let's say the heroku app is mywebsite.herokuapp.com and my email is mywebsite@gmail.com. Based on my research I understand that the problem might be that the email is being sent from my server which is mywebsite.herokuapp.com but the email address is gmail.com.

If I used $mail->Host = 'smtp.gmail.com'; can this be the problem?

Based on my research other problems might be with the SPF or the DKIM, but it seems like sending it through a gmail account might make this not an issue?

Server Settings:

$mail->isSMTP();                                            
$mail->Host       = 'smtp.gmail.com';                    
$mail->SMTPAuth   = true;                              
$mail->Username   = 'mywebsite@gmail.com';                    
$mail->Password   = 'secretpassword';                              
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;        
$mail->SMTPSecure = 'ssl';
$mail->Port       = 465;  

I spent a ton of hours trying to look it up online but everything I found had to do with custom domains and their DNS settings (for a start) which you can't have with a Heroku app.

This link gave a lot of general (very helpful) information but I can't tell from this whether or not there is a solution for my situation. This was the closet that I got to helpful information but it didn't give me enough information to go on.

Any suggestions?

Thanks

yem
  • 529
  • 1
  • 6
  • 20

1 Answers1

1

Fundamentally, you simply cannot send from a gmail address if you're not sending through gmail's servers. Their SPF, DKIM and DMARC records won't allow it. Furthermore, you can't send from a gmail address that isn't yours, or a predefined alias of yours.

PHPMailer has some docs on avoiding spam filters (essentially by not looking like something they would want to block), but there's not a lot you can do.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • The Gmail account is mine. Does that make a difference? – yem Nov 01 '21 at 21:23
  • Also, I’m assuming that setting the host to smtp.gmail.com doesn’t count as sending it though gmail servers, right? – yem Nov 01 '21 at 21:28
  • You can send through your own gmail address, but only using your own address (or predefined aliases) as the from address. If you try to use anything else, it will be ignored. Setting `Host` to `smtp.gmail.com` *is* sending through gmail servers, but you have to authenticate using your own account. – Synchro Nov 03 '21 at 08:42
  • isn't writing `$mail->Username = 'mywebsite@gmail.com'; $mail->Password = 'secretpassword'; ` essentially authenticating my account? Do you mean that I am supposed to authenticate somewhere in my gmail account instead of through the code? If so where can I do that? – yem Nov 03 '21 at 09:07
  • Yes it is, but when you do that you can only send messages from `mywebsite@gmail.com`, not any other address. – Synchro Nov 03 '21 at 09:08
  • But if I am only sending from `mywebsite@gmail.com` then is there a reason that it still would be going to spam (it doesn't seem to be a filter issue or a blacklist)? – yem Nov 03 '21 at 18:27
  • 1
    Oh, I think I got the wrong impression then! The best thing to do is inspect the headers in gmail. Google's filters are often heavy-handed, but they do at least usually tell you why they didn't like a message. – Synchro Nov 03 '21 at 19:26