0

Recently i have uploaded a small php web on Google Cloud Platform, but after uploading site i see that PHP Mailer function does not send mail.

The same code in the localhost runs perfectly, with same SMTP credentials.

Is there need to change my code?

Please, any one helps me out to resolve this issue.

2 Answers2

8

I would just like to add something for anyone that arrives on this question in 2019. I don't have the reputation level to leave a comment in response to Krzysztof's comment, so I am posting this as an answer. His comment was left in 2017 and was valid then. However, in 2019, all ports EXCEPT 25 are open on Google cloud - including 587 (TLS) and 465 (SSL).

If you are using compute engine and sending mail, you MAY need to configure a firewall rule allowing outbound traffic for the port YOU are using! This is not readily apparent. Check this if you try to send on any port other than 25 if things aren't working. 25, as I stated, is blocked. Here is a link to show you how to do this on an instance. It is toward the bottom of the page as of June 2019. It is simple to do. https://cloud.google.com/compute/docs/tutorials/sending-mail/

AS OF JUNE 2019 HERE ARE THE STEPS TO SET UP A FIREWALL RULE FOR OUTGOING EMAIL IF YOU HAVE A COMPUTE ENGINE VM:

  1. Go to the Create a Firewall Rule page. (Sign into your Google Cloud account where you ALREADY have your compute engine VM and put this into the browser after you do - https://console.cloud.google.com/networking/firewalls/ )
  2. Choose a name for the firewall rule.
  3. Under Network, select the network hosting the VM instance that you intend to send emails from.
  4. Under Direction of traffic, select Egress.
  5. Under Targets, choose the appropriate target for this rule. For example, you might choose Specified target tags if you want the rule to apply to instances that have a specific tag.
  6. Under Destination filter, set 0.0.0.0/0 if you want to allow egress traffic from the VM instance to any destination. If you want to limit the destination, enter another IP range here. THIS IS IMPORTANT IF YOUR EMAIL IS HOSTED SOMEWHERE ELSE.
  7. Under Protocols and ports, select Specified protocols and ports > tcp and enter 2525.
  8. Save your changes.

Here is a link to how to set up things to send emails (compute engine) - https://cloud.google.com/compute/docs/tutorials/sending-mail/

Here is a tutorial for setting up the FROM email accounts. All email accounts must be from Google Hosted accounts. For example, I host a website on Google Cloud so I would need to set up the email accounts my mail will come from with Google, but I can use their service to send emails as long as I do (app engine)- https://cloud.google.com/appengine/docs/standard/python/mail/#who_can_send_mail

Here is a tutorial for sending email using php on Google Cloud in 2019 (App Engine)- https://cloud.google.com/appengine/docs/standard/php/mail/sending-receiving-with-mail-api

Here is another tutorial for sending email from Google in 2019 (compute engine)- https://cloud.google.com/compute/docs/tutorials/sending-mail/

As Johan pointed out in a comment below my answer, I didn't include the text of the documents I linked to. However, I didn't do that because the document text can change, as we have seen many times with Google Cloud, and I personally hate when I come to Stack Overflow and go down a path based on instructions here only to find that the process changed months ago. So I think, in this narrow case and for now, the links to the documents may be best.

A M
  • 101
  • 1
  • 5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Johan Jun 18 '19 at 10:53
  • 1
    Hi Johan! Normally, I feel you are absolutely right, however, in this narrow case, the documentation has changed from what it used to be and it might be harmful to people who need the information if I include a step by step here and the process has changed. This isn't a trivial thing and I think it might be better, in this narrow instance, to provide the links so people can read before they do anything AND see if Google changed something. I know I hate doing something I see on Stack Overflow only to find out that the process is now different. I have had that happen with a few AWS items. – A M Jun 18 '19 at 15:32
3

Google Compute Engine does not allow outbound connections on ports 25, 465, and 587. By default, these outbound SMTP ports are blocked because of the large amount of abuse these ports are susceptible to. In addition, having a trusted third-party provider such as SendGrid, Mailgun, or Mailjet relieves Compute Engine and you from maintaining IP reputation with your receivers.

You can install postfix on your VPS server and configure it to send mails by MailGun or other service.

More information about this problem you can find in official documentation: https://cloud.google.com/compute/docs/tutorials/sending-mail/

Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42
  • Thanks, @Krzysztof Raciniewski , So I just want to know that the code is written inside php file uess the PHP Mailer function. So now onwards i have to install **SendGrid Api** on the server and use its function to send the email. right ? – omprakash sharma Aug 10 '17 at 09:14
  • Yes, or you can configure postfix to send all emails by SendGrid API: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid This is one way to send email from Google Cloud Platform VPS instances... unfortunately – Krzysztof Raciniewski Aug 10 '17 at 09:21
  • This answer is partially wrong. Port 25 is blocked to destinations outside the VPC except for certain cases. Port 25 is allowed to destinations within your VPC, a VPC connected using VPC Peering, and to on-premises using VPN tunnels or VLANs. **Ports 465 and 587 are not blocked**. – John Hanley Aug 31 '21 at 03:16