I noticed a thread on the forums that discussed exactly how to implement a working contact form into a site template. So I thought to myself, “What better way to demonstrate the process than with a screencast!”

The code that you’ll find here is 100% yours to use in any way, shape, or form. Feel free to use it as it is, or or edit the code to your heart’s content. As long as you use it to make your ThemeForest templates better, I’ll be happy.

Click on the “Toggle” to view the video in full screen.

*Note - I’ve added three lines of code to the Javascript file. These lines aren’t referenced in the screencast, but are included in the source files. They allow for the code to work with, or without Javascript turned on.

Download the Source Code

Comments

Leave a Comment
  1. Nice little tutorial video. I really liked how you explained everything in detail such as showing people how to find the phpMailer.

    One thing I noticed was that you didn’t urlencode the name, email, and comments so if someone enters some unexpected values for some of your fields it will break. Just something to consider/fix in your code.

    Also I wanted to mention that your site is truly beautiful; I really like the design you have created. Here’s hoping you can build some themes for Drupal with the same level of design; we non-designer Drupal developers really need some good themes to work with!

  2. Nice tutorial thanks :)

  3. “function (joeThePlumber)” ? LOL!

  4. I was trying to watch your PHP contact form tutorial but when you press play it goes staight to the next one. It skips forward to the photo gallery tutorial.

  5. Why not make a “Tutorials” post category and put this in it. I’m sure there’s many of us that would like to conveniently see all your tutorials at once.

    Thanks for the cool instructions.

  6. Great job! This is fantastic. I can see a lot of additions being made to it as well. Thanks a ton!

  7. @Joseph - Great idea. I’ll take care of that. ..Anything that makes more people subscribe works for me! :)

  8. Jeffrey,
    I like your screencasts they’re amazing :)
    But something that I recommend to do Is to add htmlspecialchars to the name,comments and email.

  9. Why aren’t there more subscribers here? This place is full of awesome info! Keep up the great work guys, I’m sure it will pay off.

  10. Hi Jeffrey,

    Great job on the screencast. Just a quick note, you said that you used -tags to be able to set the label names to “block” using CSS. There is actually a tag created to do just that! It’s called , and one uses it like this:

    Name:

    This greatly increases the accessibility and usability of a site. It help people with screenreaders, etc. select the text field, and us “normal” web users can actually just click anywhere on the label and go straight to the text field to start typing right away!

    Keep the good work up!

    Mads

  11. Awesome job. Thank you!

  12. Pretty cool, but I am curious as to why you didn’t use the label tag in your form?

  13. jeffrey i have read quite a few of your posts and this is by far one of the best… in depth yet easy to follow and a method i would actually use

  14. Nice tutorial Thanks

  15. Nice tutorial!! very usefull!

    I was wondering how to replace the input button for a styleable button like

    code: Send

    When I replace the button, the ajax function breaks and loads the error messages into another blank page.

    thanks

  16. Hi thanks for the tut…

    I have a problem when i hit the botton “send” the elements are opend in another page, exactely in http://localhost:8888/alice22/sendEmail.php

    I cant figure it out by myself!

  17. Hey I downloaded your source it did not work what is going on?

    http://gtacommunity.exofire.net/contact/

  18. Where is the php.ini file to edit? Do I download a generic one from somewhere?

  19. @Jeff

    You said that there is a bug that makes the email in Gmaill show your own address as the sender. Isn’t that actually the address you wrote in the php code, the smpt email address and password? You can see that in the mail, the receiver address is spelled with only lowercase letters, but the sender one has the Forest and Test words with an uppercase letter, just like you wrote it in the php code. Maybe that should actually be the senders email address and password. It also doesn’t make much sense why the sender requires your password to send a mail to you. Also, wouldn’t that be visible to anyone by just taking a look at the php file? I really appreciate your tutorials and I want to use this one but I want to be sure about this first. Thanks for your help!

  20. hello i am trying to work with your php mailer. I is great but i am have a promblem. i am using gmail just like you and this is what I get “Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in MYWEBSITE/stream/phpMailer/class.phpmailer.php on line 45″

    I have examined line 45 and I am by no means a coder programmer. What does this mean and what can i do about it.

  21. @ Fernando

    I have swapped the button code for a link…

    index.php

    Send

    & modified the script.js file

    $(’#main a#submit’).onclick(function() {

    …it works fine with JS switched on but when it is switched off the user is sent to the error/feedback page, but it lists all the form errors regardless of what was entered by the user!

    Any help with this issue would be greatly appreciated :)

  22. …the code for the ’send’ link - previous post

    index.php

    a href=”sendEmail.php” onclick=”return false;” title=”Submit” class=”link_button” name=”submit” id=”submit”>Send

  23. Hey Jeff,
    Fantastic tutorial! The only question I have about it is concerning the GMail code that you added in the sendEmail.php file. As you mentioned, I had the same bug where GMail was not showing me the proper sender nor their emai address. As I was looking the code over, I started to wonder why I even needed to put in my GMail username and password in the first place to send an email from the server.

    What I chose to do was just delete that entire block of code title “GMail Stuff” from my sendEmail.php file up until $mail->Send();. Once I tried sending an email with this modified code, everything worked properly. I have tried viewing the emails in Outlook and GMail, and everything works great :D

    I mean in no way to criticize your tutorial, as I am still learning PHP. I just wasn’t sure why you added the GMail block in the first place. I hope this tip can help others that were having this problem. I look forward to more tutorials from you and can proudly say I am following your JQuery series.

    Thanks,
    -Josh

  24. @josh

    Thanks for clarifying the GMail issue!

    What do you think Jeffery ?

    …Bueller ??

  25. Very COOL tut.Good work .

  26. Very well done contact form, thanks!

    I’m trying this out for a swedish site and the letters “å ä ö” didn’t work out very well… How can I change the charset in the messages being sent?

  27. Nice tutorial, Jeffrey. If I recall, haven’t you already posted something about creating a contact form using PHP and JS?

    In any case, this is helpful for beginners as well as being nice for reviewing and learning how others script. Thanks!

  28. Josh say

    “What I chose to do was just delete that entire block of code title “GMail Stuff” from my sendEmail.php file up until $mail->Send();. Once I tried sending an email with this modified code, everything worked properly. I have tried viewing the emails in Outlook and GMail, and everything works great :D
    —-

    wok for me too, eah!
    on a ftp-web server.

    Thank for great tutorial.

  29. Great tutorial.. I am having an problem though.. I want to have the submit button to be an image.. I tried making the changes but it wont work as an image.. any ideas?

  30. Gravatar

    Hassan Ijaz

    Hey Jeff, Can you please let me know about some book or a very good site which provides how to create site layouts and then code them , make them ready for the web? Please help me out. I really want to learn that stuff.

    Learning a lot from this series Jeff! Awesome!

    -Hassan

  31. Im so greatful to you i could marry you…..so happy i get it now as a beginner.

  32. Anyone else getting this error:

    stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in D:\Domains\zoost.ie\wwwroot\eclicks\phpMailer\class.smtp.php on line 194

    :-S

  33. Has anybody else had trouble using a gmail email address without having the GMAIL STUFF section of sendEmail.php?

    Works with it in there, but doesn’t when I remove it. I would prefer to remove it as I don’t want my gmail login information visible in the php file.

    But overall, thanks for a well produced tutorial.

  34. hi,Jeff thx for gr8 tutorial but i am having one prob.

    When i click submit it just opens send.php in a new tab :(

    Your help would be much appreciated .

  35. Hello,

    I have an error using php mailer the same code as the source you put online.

    The mail is sent but the following error is written:

    Notice: Undefined variable: extra in C:\wamp\www\mess\php\phpMailer\class.smtp.php on line 173

    What should I do?

    Thaks a lot for your help! Awsome tutarial by the way,

    Camille

  36. Jeffrey, u have done a great job on this screencast with all the explaination.

    I am lost iin the sending mail part, hope you can help me on this:

    I am having the same problem with @james (stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\wamp\www\contact_form\phpMailer\class.smtp.php on line 194)
    and also @camille
    (Notice: Undefined variable: extra in C:\wamp\www\contact_form\phpMailer\class.smtp.php on line 173).

    When i removed the GMAIL stuff $mail-> up to $mail->Send();
    Those errors are gone and it sent d messages successfully, but when i go to my gmail to check the mail, no incoming mail. i wonder how to solve this problem.
    Hope i will get help from the experts here.
    Thanks.

  37. hi,Jeff thanks for your tutorial but i am having the same problem as Karu, when i click submit it just opens send.php in a new tab
    Help please !

  38. hello Jeff,

    I’ve looked all over to solve my problem (that siewlee also has…)

    Notice: Undefined variable: extra in C:\wamp\www\contact_form\phpMailer\class.smtp.php on line 173

    I cannot find an answer… It would be awsome for us if you could help us on that…

    thanks a lot in advance, your work is incredible!

    camille

  39. Thanks so much, after trying a lot of different resources, finally I found something which really works. Thank you very much.

  40. Hello Again,
    I dont why but when I used mootools it worked !!!
    Thanks again for your tutorial

  41. Hello; I’m a visually impaired computer user trying to improve my website by adding a contact form for people to use to post new equipment listings to my website. My efforts are further complicated by using an air card as I have to travel quite a bit to make a living while I wait for the website to hit. So, I’d love to view your video tutorials, but it would help greatly if you could tell me which one if any address my specific problem. The form not only needs to have fields for contact info, but I really need for there to be a way for people to submit photos or videos along with their contact as the main purpose of the contact is to list their equipment on my site. I hope you can help with this. Thank you, Max

  42. Gravatar

    William Rouse

    It would be of value to me if you gave some thought to explaining how to modify your code to have it work from the hosting site of a webpage on the internet SMTP mail server rather than going trough Google.
    Thanks for the code. It was helpful to see how this work in an OOP setting.
    WBR

  43. Gravatar

    William Rouse

    Jeff:
    Never mind, I spent most of the day, but I figured out how to send from my development system via my hosting site SMTP server.
    William

  44. This tutorial was very informative and I have bookmarked your site. I’m sure I’ll be back very soon. Thank you.

  45. I have been watching your tuts all day. They are great!

  46. I’ve been looking for at well explained TUT on this subject for so long. Thank you Jeffrey

  47. i dont’t know why but it says “Oops The link appears broken

  48. Thanks for the tutorial, this is exactly what I was looking for. I plan on loading it dynamically using facebox.

  49. I managed to get this program to work when I did the tutorial. However, when I tried to modify it to use it for a different contact form I did, I had trouble this time around and I don’t understand what’s going wrong.

  50. Anyone know how to change the text on the submit button? It says “Contact Me Now”, but html form code says it should be “Contact Me”. I can’t find anything in the files that is causing the text to be overridden.

  51. nice tutorial, I am new to web design, and this is the first time that i see a tutorial for .php that i totally understand. thanks!

    you are amazing.

  52. Uh…yes the tutorial is great but I guess I must be really stupid because I feel I’m still missing tons of info!

    What do I do with the source files now? Where do I upload them? Into the Theme I’m using sure BUT, I already HAVE a index.php file there, surely I’m not going to override it with this one…right?? Why wouldn’t the file be called “contact.php” instead? Also, I already have a “JS” folder in my theme file, if I transfer these source files to my theme, am I supposed to take what’s in the source “JS” and put it in with what’s in my existing theme “JS” folder?

    I’m sorry but this isn’t clear to me, the entire section of “What to do with the Source files once you’ve modified them” is missing for this dummy…please help!!!

  53. I’m using a local host. I tried and tested it but my yahoo account is not receiving anything.

    I changed the email address provided in sendmail.php to my own email but it seems like is not working on my side.

    What should I do?

    Please help.

  54. Hi….
    I just checked your form with yahoo mail & gmail …..& its not working….
    Infact form submitted successfully but i never recieved it in my mailbox…..

    I have some other form which are working perfectly but they are not ajaxed….

    Welll wishing you luck …..tc

  55. Thanks for the tutorial. I am having an issue where I just get the php script after hitting the send e-mail button. Anyone know why that is?

  56. I removed the Gmail portion as well, and it works great, thanks.

    There is one big issue with this form though - it will not submit with javascript disabled. To fix this, use a noscript HTML tag like so:

    <!--
    document.write('');
    -->

    You can find more details about this method in the article at http://www.webaim.org/techniques/javascript/alternatives.php

  57. Sorry, the full code didn’t show up in my previous comment, but you can grab it from that link.

  58. Thank you for this great tutorial!
    I just have a question… I can’t find my php.ini file in my server, I’m using a shared hosting account.
    Can anyone help me to find it???

Add a Comment

Name Email Website

Note: The avatars shown next to comments are Gravatars. You can get a Gravatar account for free and any other site that supports it will show your avatar too!

 

Trackbacks

Leave a Trackback