Saturday, December 13, 2014

Building A Contact Form - PHP Sendmail and Gmail Tutorial & Tips

Gmail Won't Send PHP Form Mail


For those of you who want to skip right to figuring out how to fix the sendmail or mail function in a php contact form when using a personal gmail account to smtp your mail, click here. For those of you interested in learning to troubleshoot php contact forms and the php mail function, please continue :)

I've developed php based sites for a while now.  I'm sure all of us have been happily coding along when we run into a bug or a setting that takes hours to correct. When that 'AHA!' moment comes and we fix the problem, all we can do is facepalm. I've always found the simplest of settings/bugs end up taking the majority of our time to fix. Such was the case when I was using a gmail SMTP server (smtp.gmail.com) to send my php contact form's mail while using PHP's 'mail' function. Building a contact form using the mail function is super simple. The code:

<?php
mail($to, $subject,$message)
?>

That's it! Nice, clean, and tidy. The problem was when I setup my php.ini file.  For you php uber noobs, the php.ini file is a configuration file that instructs the php engine how to operate at runtime. It's where you go to tell php how to handle things like security, connections to mysql, and in this case, send mail (or as php spells it 'sendmail'.) The php.ini file needs to be configured properly in order for you to troubleshoot php contact form issues.   I use xampp as my server configuration tool, so in my case, my files like in my C:\xampp folder - your file folder locations may differ if you don't use xampp. You need to update the settings in your php.ini file as well as your sendmail.ini file.

(Note: make sure you don't have multiple entries for this data like the 'sendmail_path' function, as this can cause errors)

(Note2: a configuration file has many 'switches' for simple and fast setup. In the php.ini file, you'll notice that many of the lines have a ';' semicolon in front of them.  The php interpreter reads your php.ini file line by line. The semicolon tells php's interpreter to that this line of code is switched off (in practice, to skip that line). To have it process the code, simply remove the ';' in front of the line.)

PHP.ini Mail Function Settings For Gmail

smtp_port = 587
sendmail_from = <your gmail email account>
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" <this is also the location of your senmail.ini file>
mail.log = "C:\xampp\php\logs\php_mail.log"

Sendmail.ini Mail Function Settings For Gmail

error_logfile = error.log
debug_logfile = debug.log
auth_username = <your gmail email account>
auth_password = <your gmail email account password>
force_sender = <your gmail email account>

Your log files are key to determining why your form mail simply won't work.  Make sure you note where your log files are stored so php can tell you if your smtp server is playing nice or if you have some errors. It was in my log files, that I discovered that the php mail function was working all fine and dandy.

Gmail Won't Send My PHP Contact Form Mail

Which lead me to the main culprit.  If my php code was correct, and my ini file was correct, there could be only one villain - gmail itself. Turns out the issue was a simple setting in gmail.  By default, gmail disable's access from 'less secure apps' ::rolleyes:: Enable this setting, and your php form should work just fine. Here is the link (make sure you are logged into your google account first): Enable Less Secure Apps.


How To Fix Gmail Not Sending PHP Contact Form Mail





No comments:

Post a Comment

Feel free to send along any questions, comments, or hacks you'd like to see :)