Reputation: 81
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
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/
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
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