Search code examples
phpherokuphpmailer

Integrate PHPMailer in Heroku


I have written a PHP app that sends email for user validation with PHPMailer. Locally everything works like a charm, and I would like to now deploy it in Prod on Heroku and I am looking for guidance on how to integrate PHPMailer there. Unfortunately, I couldn't find very clear material about it. I understand that Heroku uses composer and that I may need to edit composer.json. There is currently no composer.json in my deployed app on Heroku. Should I simply create it from scratch? If so, should I require only PHPMailer or is there anything else that I should include?

Sorry for the dumb questions, I am new to Heroku and I just want to make sure I don't compromise the deployment since it's working fine, and all I need to do is integrate the mail part.


Solution

  • On your own computer, with composer installed, run this in the root directory of your project:

    composer require phpmailer/phpmailer
    

    That will create a composer.json file, a composer.lock file, and a vendor folder containing composer utilities (such as autoload.php) and PHPMailer's files. Normally you'd add the two composer files to your git repo, but not the vendor folder.

    That should mean you can run your project locally. When it comes to transferring it to Heroku, I would expect you to upload everything except the vendor folder, and Heroku will spot the two composer files and use them to install exactly the same files on your server.

    If Heroku doesn't install things for you, you can upload the vendor folder too, but this is not recommended.

    When I say "upload" I expect that you would normally allow Heroku to access a git repository with your code in and have it pull the files from there (and perhaps automatically pull changes when you push to your repo), rather than you manually uploading.