PHP Contact Form

March 19, 2019 962 views
Email

I have a static index.html running on a nginx server. I have a simple form element that takes in a user’s contact details and sends a POST request to inc/sendEmail.php. I haven’t set up any mail system on my droplet so maybe that’s why it’s not sending any email? Everything code wise looks pretty solid but I’m not sure where to start to get my code to start working for me. I’m running Ubuntu 18.04 and NGINX. When I hit submit I get a ‘200 OK’ response from jquery sending the post to the PHP script but that’s as far as it goes and then it throws my error something went wrong. Any advice is appreciated.

6 comments
  • @mikem255 I have the same requirement. Have done extensive reading on this and found various solutions. Going to try the easiest first, then report back soon.

  • @dawnsky Thank you very much I appreciate your involvement and your time.

  • Resolved for me.

    There was a tag ’<?’ but should have been ’<?php’ in the code. After I made the change, my form rendered correctly and tested fine.

    After looking at your post a second time, do you have any PHP code in the HTML file? If so, does it need to have the .php not .html?

    My form uses the PHP mail() command to send.

    I wanted to know if it was the mail app being used on the droplet, a DNS issue, or PHP.

    Validated PHP as per the DO tutorial.

    Initially had some DNS issues, but resolved them.

    Found a PHP script that tests the SMTP relay at the server side. All tests worked, but the messages went to SPAM, so a side mystery.

    So what else could it be? During my research I came across a thread where one of the posts said to check for the deprecated <? tag. He explained that the mixture of <?php and <? tags would cause issues. And there you have it.

    I am running an Ubuntu-Nginx-LEMP droplet, latest updates, PHP 7.2. Left everything at defaults. Did not edit any files. Did not touch any mail clients, so using the default send only stack. Running UFW, firewall allows 22, 80 and 443 inbound. Using /var/www/html the default Nginx server block.

    Got lucky on this one. Hope this is helpful for you. Cheers and rock on!

  • @dawnsky First of all thank you for your research, time, and thorough comment. I have the <?php tag which seems fine, the form action is linking to the folder with the php script in it. Would you mind sharing the php script you used to test and the steps you took? Thanks.

  • Please visit the following link to get a test script for the email function–

    https://www.arclab.com/en/kb/php/how-to-test-and-fix-php-mail-function.html

    This will let you know if your SMTP relay portion is working. You will have to check the receiving mailbox for the resulting message.

  • Show 1 more comments
1 Answer

Greetings!

Thank you for posting this question here. To wrap up this post with a dedicated answer, it seems that you were able to resolve this by comparing the script at this URL to your code:

https://www.arclab.com/en/kb/php/how-to-test-and-fix-php-mail-function.html

That is a good reference, for anyone experiencing similar. However, it is also worth noting that there are a lot of complexities to sending email that did not exist several years ago. For that reason, one should also check their MTA logs to see if they can find out what happened when they sent an email. For example, if I’m sending an email to “jarland@gmail.com” and using Postfix on the droplet, I might run this to check the logs:

grep "jarland@gmail.com" /var/log/mail.log

Some more information on debugging Postfix logs (as it is the most common MTA, to the best of my knowledge):

https://easyengine.io/tutorials/mail/postfix-debugging

Jarland

Have another answer? Share your knowledge.