omprakash sharma
omprakash sharma

Reputation: 81

Php Mailer Function not working on Google Cloud Platform

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.

Upvotes: 0

Views: 5647

Answers (2)

A M
A M

Reputation: 101

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.

Upvotes: 8

Krzysztof Raciniewski
Krzysztof Raciniewski

Reputation: 4924

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/

Upvotes: 3

Related Questions