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
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.
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