How to receive email on Google Compute Engine - php

Google's compute engine blocks all outgoing email ports by default. Quote from Compute Engine Documentation
Compute Engine blocks or restricts traffic through all of the following ports/protocols between the Internet and virtual machines, and between two virtual machines when traffic is addressed to their external IP addresses through these ports (this also includes load-balanced addresses):
> All outgoing traffic to port 25 (SMTP) is blocked.
Most outgoing traffic to port 465 or 587 (SMTP over SSL) is blocked, except for known Google IP addresses.
As a solution to this, Google advices the use of third-party sites like SendGrid to manage email. SendGrid can easily help you send email, but receiving emails is not so easy.
Installing an email client on the server won't help because the ports through which these clients need to communicate with other servers to receive emails remain blocked.
My question is what's the easiest solution to receiving email? SendGrid's solution is for sending emails, and the Compute Engine Documentation specifies this. I've installed postfix but all emails to my server receive timeout errors, and my inbox remains empty.

TL;DR - You're right outgoing email ports are blocked, but incoming email ports are not. If you open them, and run a mail server which listens for incoming emails - you can still receive them. You will need to use 3rd party email sender gateways like SendGrid only to send the emails.
Longer version
All outgoing traffic to port 25/465/587 are blocked so that people do not abuse GCE for sending out spam, phishing, etc. emails.
You can still run a mail server which just receives emails. In other words, there is no mention that incoming traffic to those ports are blocked. By default GCE firewall blocks all incoming traffic. You will have to open up these listening ports using firewall rules.

I'm using Mailgun instead of SendGrid. I forward all the emails to one of my gmail accounts using MailGun's forwarding rules. SendGrid too should have forwarding rules.

Related

Not receiving mails to gmail from aws instance server

I am facing problem in receiving mail to my Gmail from a contact form on my website. Contact form is connected to a small PHP script. My website is uploaded on aws instance server and DNS on route 53.
Everything is updated in DNS (route 53) like MX records, SPF, DKIM, DMARC and everyday I got DMARC report but not receiving any mails for the contact form.
Below are the details
PHP script
<?php
$to = "info#mydomain";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: sender#example.com";
mail($to,$subject,$txt,$headers);
?>
MX records
1 ASPMX.L.GOOGLE.COM
5 ALT1.ASPMX.L.GOOGLE.COM
5 ALT2.ASPMX.L.GOOGLE.COM
10 ALT3.ASPMX.L.GOOGLE.COM
10 ALT4.ASPMX.L.GOOGLE.COM
SPF
"v=spf1 include:_spf.google.com ~all"
DKIM
The key which is provided by google
DMARC
"v=DMARC1; p=none; rua=mailto:info#mydomain"
These are the details updated on amazon DNS route 53 and still, I am not getting any mail.
Please anybody can help me with this.
There could be a number of reasons for this:
By default port 25 is blocked, if you're sending mail using this port you would need to consider using another port.
Many mail providers will also either reject or mark as spam emails that come from the EC2 IP ranges, primarily because it is easy for someone to setup a free tier account and send spoof emails.
The suggestion would be to connect to a mail service over another port. If you do not currently have one setup for sending take at Amazons offering with SES, using port 587 from your server you could send emails via this service with neither of the top 2 conditions to stop traffic being met.
If there is a specific reason for needing to use Port 25 Amazon do have a process for getting it unblocked for you, but you'll need to provide many steps to prove you are not going to abuse it.
By default, AWS instances are blocked from using PHPs native mail() function. Your code is doing just that.

Do I need to use using third party to work php mail function in Google Cloud Instance?

My customer told me to move his website to setup in google cloud instance. I have setup Ubuntu, lamp and domain dns. Everything is working fine except outbound email sending. GCI(Google Cloud Instance) said that they are blocking port 25. I also tested with php.ini file by changing port 465 and 587 but it doesn't work. According to their tutorials, it recommend to use SendGrid, Mailgun and Mailjet.
Do I really need to use third party services to work only php mail function in Google Cloud Instance? I feel it is wasting resources & money. Is there any option without using third party.
I found similar question, but nobody answered on that.
In GCP, email ports like 25, 465, and 587 are blocked by default because of the large amount of abuse these ports are susceptible to. In addition to this, having a trusted third-party provider such as SendGrid, Mailgun, or Mailjet relieves Compute Engine and you from maintaining IP reputation with your receivers. While sending email from blocked ports is not allowed, your instances can still receive email.
Here are the following tutorials to set up SendGrid, Mailgun, or Mailjet.

SMTP Traffic on port 25

What are the dangers of opening port 25 to allow smtp traffic through? I have a windows server 2003 box which has port 25 blocked (Not by ISP), I know ISP's for usually block this port from use but as I need it for some php scripts I'm running that notify users by email, Is there any dangers or precautionary actions I should take?
Any advice would be appreciated,
Thanks Luke.
If you are thinking of unblocking port 25 on outbound traffic then no, not really. If you are wanting unblock port 25 on incomming traffic to receive email from external scripts into your internal mail server then you need to ensure you have the mail server configured to prevent relaying etc.

GAE Sending emails via SMTP

Reading up on GAE as a possible alternative to dedicated hosting (or VPS)...
Seems I would need to re-write a bit of code, if I had modules which relied on SMTP. Does this mean that even if I connected to a remote SMTP server that code would need to change to an HTTP API (assuming one is even provided by an third party SMTP provider such as smtp.com).
Yes - AppEngine/PHP does not yet support sockets and thus you can not connect to external SMTP servers. Also, even with sockets you would not be able to use SMPT on port 25, but authenticated SMTP on port 587 - see socket limitations.
OTOH, you can send email from AppEngine via the Mail API.
If you still want to send email via an external mail provider, you need to choose one that supports HTTP API, for example Amazon SES.

sendmail with a 'different' SMTP host solution : pro and cons

What are the pro and cons of using the SMTP host option of my choice?
Do sending mails from localhost require a specific SMTP host?
Is there any discernible difference on the receiving end between a mail sent via the server where the site resides and a dedicated mail server?
thanks
Luca
This depends if localhost refers to a public server with a proper DNS and reverse DNS entry, or if localhost is a machine you operate from a residential Internet Service Provider.
If localhost is a real server, outbound mails may be delivered a little faster if sent from the same server, and configuration in PHP & others is generally a lot simpler to setup.
If it isn't a proper server though, especially having proper DNS entries, the likelihood of much mail from it actually reaching recipients is low. Most receiving SMTP servers will reject mail if they cannot validate the source via a reverse DNS entry.
If you use a dedicated mail server, you get the benefit of all the inbound & outbound logging being on the same machine, and when receiving SMTP hosts lookup your domain's MX record in DNS, it will already be configured as the mail server rather than the web server. But this just requires a little more configuration on the web server, especially if the mail server requires authentication from the web server rather than treating it as a trusted relay sender.

Categories