Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send email on heroku using PHP?

Tags:

php

heroku

I have created PHP app and deployed using heroku. In the app, I have contact form to send mails to my gmail account. To implement this, I have written the following PHP code

<?php
   $to = "[email protected]";
   $subject = "This is subject";
   $message = "This is simple text message.";
   $header = "From:[email protected] \r\n";
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true ){
      echo "Message sent successfully...";
   }else{
      echo "Message could not be sent...";
   }
?>

My php script is executing. But it is not sending mail instead it is showing up Message could not be sent. Can any one help to implement contact form using PHP?

like image 376
suneetha Avatar asked Sep 10 '13 09:09

suneetha


2 Answers

Heroku doesnt allow send e-mails, you must use external SMTP server.

"To send emails from applications deployed to Heroku, use an external SMTP service." https://devcenter.heroku.com/articles/smtp

How to send e-mails throw SMTP is described here: Sending email with PHP from an SMTP server

like image 146
Honza Avatar answered Oct 21 '22 14:10

Honza


Its not easy to send email through smtp on heroku without an add-on.

I'll suggest you checkout the Sendgrid Heroku Add-on Sendgrid is a Flexible Web and SMTP APIs, plus a simple SMTP Relay set-up, allow you to decide which integration method is right for your environment.

Check it out, it should be the solution to the problem, as it solved mine.

UPDATE

For starters, try out sendgrid, it'll be straight forward to use. But note that Sendgrid is just one of the many options that one could use. You can send using any SMTP server of your choice, so far you have the required credentials like username, password, host, port, driver, and/or encryption.

PHPMailer provides the functionality you'll need. So just integrate PHPMailer, or any similar package with you app, and provide the required credentials provided by your SMTP server.

like image 37
pamekar Avatar answered Oct 21 '22 16:10

pamekar