Build a PHP Script to Send Email – Dreamweaver Tutorial

Build a PHP Script to Send Email – Dreamweaver Tutorial

In the following tutorial we will start with a few html files – which are available for download here on the site – and we will build an Email Handling PHP script that sends info from your web form to your email! It is pretty easy PHP so enjoy learning and Have fun! I am using Dreamweaver CS3, but you can use pretty much any version of Dreamweaver, just follow closely and try not to make mistakes.

More from Nathaniel Dodson
The Extract Filter – Photoshop CS2
The Extract Filter In Photoshop CS2 Learn how to make and use...
Read More
Join the Conversation

114 Comments

  1. says: Bene_amin

    Note: most of these scripts use the mail() function – That does not work on our shared hosting (because we don’t have access to the php.ini file). Therefore they gave us their own function called :
    function authSendEmail($toaddress, $subject, $message) – We used that and it was dead easy.

    1. says: Maluguimpel

      is this function specific to your shared hosting? should i ask my hosting for the correct one i should use?
      thankyou

  2. says: SYAWEDIS

    Hey man, I don’t use dreamweaver I strictly use a text editor and when you talk about linking the html to the php file, you go into Design view and do it from there, leaving me clueless as to what I have to write into the Code.

    Can you explain how I would target the php file in Code format?

    Great tutorial btw!

  3. says: Tammy

    Hey there Nathaniel!  Thanks for the instructions…I went over them several times and am still having trouble with the content not showing up in the email…the fields show up but the answers that the client enter do not…could you tell me what I might be missing?  Thanks!!

  4. says: MikeMcC

    I too am having trouble with the content.  It is not showing up in my e-mail.  I am using Dreamweaver CS4.

  5. says: Spaceboy

    Hi Nathaniel, Is there a fix for the no content showing up? Im using godaddy server. Do I need to link the php.ini?

  6. says: LuisRod

    guys. I emailed Nathaniel about this issue. And he said.
    “Hey   Luis   ,
    No problem at all, man! Glad you tweeted over to me so I saw it :PFor some servers in the EOD part it shouldn’t be: Email: $email , but:Email: $emailField etc etc.And in the headers part you have to change $headers = “From: $email rn”,to: $headers = “From: $emailFieldrn”Note: There are also issues with Yahoo! and GoDaddy servers. If you areusing their hosting try contacting them and see if they have any answers foryou.”
    hope you guys can get it to work 🙂

    1. says: Steven

      I’m on an Apache/Linux server, and I made the change of Email: $email to:Email: $emailField etc. etc., and also $headers = “From: $emailFieldrn and it is all working perfectly.  Don’t know why it worked for you in the tutorial.  Thanks for the fix.  Steven

    2. Hay would you have a look at my code, something is wrong, when i hit send i get a lot of code come up

      ::

      Email: $emailField

      Name: $nameField

      Phone Number: $phoneField

      Budget: $budgetField

      Number of Travelers: $travelersField

      Comments: $commentsField

      Newsletter: $newsletterField

      EOD;

      $headers = “From: $emailFieldrn”;
      $headers = “Content-type: text/htmlrn”;
      $success = mail($webMaster, $emailSubject, $body, $headers);

      /* Results rendered as HTML */

      $theResults = <

      ::

      im guessing i have miss something out but i just can’t work it out.

      hope you can help

      Tom

      1. says: Jacoba

        Hi Tomasz,
        did you include the following part:
        $Headers = “From: $emailrn”;
            $Headers .= “Content-type: text/htmlrn”;
            $success = mail ($webMaster, $emailSubject, $body, $Headers);

        In the video, Nathaniel specifically mentiones the second line, which makes sure the code got ‘translated’ into text (instead of code)

        Hope this solves your problem!

  7. says: Anonymous

    I tried this PHP code, and when I receive the email, there is no content and the email appears to come from an “unknow sender”.  I have a shared server with GoDaddy, and they indicated that I need to include “relay-hosting.secureserver.net” somewhere in the PHP code, but I’m a rookie with PHP and not sure how to do this.  I’m also not sure if this is the problem that is causing the labels to be sent without the content or data??  Please help as I’d love to get this working.  Thanks!!

  8. says: Xmalunda

    i tried the code designed my template using FW cs4 but when i put the file and click submit it shows me the code and NO email is sent please help 

  9. says: Jacoba

    Hi Nathaniel,
    I really like your tutorials and builded all my websites based upon them 😉
    But now I have the same ‘problem’ as mentioned below, I cannot find the document htmltophp. Can you help me with that?
    Thank you!!

  10. says: kamil

    hello i dont know why mine is not working here the code
    K1K!<?php/* Subject and Email Variables */ $emailSubject = 'Contact ME!'; $webMaster = '[email protected]'; /* Gathering Data Variables */ $nameField = $_POST['name']; $emailField = $_POST['email']; $subjectField = $_POST['subject']; $messageField = $_POST['message']; $body = <<<EODName: $name Email: $email Subject: $subject Message: $message EOD; $headers = “From: $emailrn”; $headers  .= “Content-type: text/htmlrn”; $success = mail($webMaster, $emailSubject, $body,    $headers); /* Result rendere as HTML */ $theResults = <<< EOD  K1K!body,td,th { font-family: “Droid Serif”, serif;}h1,h2,h3,h4,h5,h6 { font-family: Michroma, sans-serif;}function MM_validateForm() { //v4.0  if (document.getElementById){    var i,p,q,nm,test,num,min,max,errors=”,args=MM_validateForm.arguments;    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);      if (val) { nm=val.name; if ((val=val.value)!="") {        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';        } else if (test!='R') { num = parseFloat(val);          if (isNaN(val)) errors+='- '+nm+' must contain a number.n';          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');            min=test.substring(8,p); max=test.substring(p+1);            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }    } if (errors) alert('The following error(s) occurred:n'+errors);    document.MM_returnValue = (errors == '');} }         Time(GMT):obj=new Object;obj.clockfile=”free002-black.swf”;obj.TimeZone=”WET”;obj.width=150;obj.height=150;obj.wmode=”transparent”;showClock(obj);Latest Videos:        AC_AX_RunContent( ‘width’,’292′,’height’,’250′,’src’,’http://www.youtube.com/v/XsrRRklznj4′,’type’,’application/x-shockwave-flash’,’wmode’,’transparent’,’movie’,’http://www.youtube.com/v/XsrRRklznj4′ ); //end AC code  Recent Tweets:new TWTR.Widget({  version: 2,  type: ‘profile’,  rpp: 2,  interval: 30000,  width: 250,  height: 300,  theme: {    shell: {      background: ‘#333’,      color: ‘#fff’    },    tweets: {      background: ‘#333’,      color: ‘#fff’,      links: ‘#4aed05’    }  },  features: {    scrollbar: false,    loop: false,    live: false,    behavior: ‘all’  }}).render().setUser(‘K1KXD’).start(); Home Images Videos Servers Staff Donate Contact Us                          Thank you for contacting me, i shall get back to you as soon as possible.Copyright © 2010-2012 Kamil Pietrecki(K1K), LLC. All Rights Reserved.EOD;echo “$theResults”;?>

    it send the email but it doesn’t show the information or the sender email

  11. says: Kaamil331

    i was reading some suggestion my new code
    K1K!<?php/* Subject and Email Variables */ $emailSubject = 'Contact ME!'; $webMaster = '[email protected]'; /* Gathering Data Variables */ $nameField = $_POST['name']; $emailField = $_POST['email']; $subjectField = $_POST['subject']; $messageField = $_POST['message']; $body = <<<EODName: $nameField Email: $emailField Subject: $subjectField Message: $messageField EOD; $Headers = “From: $emailFieldrn”; $Headers  .= “Content-type: text/htmlrn”; $success = mail($webMaster, $emailSubject, $body, $Headers); /* Result rendere as HTML */

    still doesn’t work it send the email but the information doesn’t appear

  12. says: Javier3446

    Hey guys a lil help would be nice am getting an error:

    Parse error: syntax error, unexpected $end in /home/content/35/7697835/html/sendemail.php on line 270

    and all my code ends at line 269 i don’t have aline 270

    266 EOD;        
    267
    268 echo “$Result”;
    269 ?>

  13. says: Croger

    Hey Nathaniel, 
    How do I fix this error :   Fatal error: Function name must be a string in /home/a3757721/public_html/contactformprocess.php on line 10

    in line 10  I have this:
     Gathering Data Variables */ $emailField = $_POST [’email’];  (line10) $nameField = $_POST [‘name’]; $phoneField = $_POST [‘phone’];     $budgetField = $_POST [‘budget’]; $travelersField = $_POST [‘travelers’]; $commentsField = $_POST [‘comments’]; $newsletterField = $_POST [‘newsletter’];

  14. says: Mabiom92

    Hi 🙂

    I saw your tutorial and i like it!
    When I receive the email there is no information inside – exept:Tilbagemelding på email:

    Tilbagemeldning på telefon:

    Navn:

    Firma:

    Adresse:

    Post nr.:

    By:

    Email:

    Telefon:

    Pølsebar:

    Københavnervogn:

    Gasvogn:

    Elvogn:

    Fadølsanlæg:

    Slushice maskine:

    Popcorn maskine:

    Leje dato fra:

    Leje dato til:

    Forventet antal gæster:

    Beskrivelse af arrangement:

    Hope you can help me! <?php/* Subject and Email Variables */    $emailSubject = 'Forespørgsel på arrangement!';    $webMaster = '[email protected]';    /* Gathering Data Variables */    $tilbagemelding_emailField = $_POST['tilbagemelding_email'];    $tilbagemelding_telefonField = $_POST['tilbagemelding_telefon'];    $navnField = $_POST['navn'];    $firmaField = $_POST['firma'];    $adresseField = $_POST['adresse'];    $postnrField = $_POST['postnr'];    $byField = $_POST['by'];    $emailField = $_POST['email'];    $telefonField = $_POST['telefon'];    $poelsebarField = $_POST['poelsebar'];    $koebenhavnervognField = $_POST['koebenhavnervogn'];    $gasvognField = $_POST['gasvogn'];    $elvognField = $_POST['elvogn'];    $fadoelField = $_POST['fadoel'];    $slushiceField = $_POST['slushice'];    $popcornField = $_POST['popcorn'];    $lejefraField = $_POST['lejefra'];    $lejetilField = $_POST['lejetil'];    $gaestField = $_POST['gaest'];    $beskrivelseField = $_POST['beskrivelse'];        $body = <<<EODTilbagemelding på email: $tilbagemelding_email Tilbagemeldning på telefon: $tilbagemelding_telefon Navn: $navnField Firma: $firmaField Adresse: $adresseField Post nr.: $postnrField By: $byField Email: $emailField Telefon: $telefonField Pølsebar: $poelsebarField Københavnervogn: $koebenhavnervognField Gasvogn: $gasvognField Elvogn: $elvognField Fadølsanlæg: $fadoelField Slushice maskine: $slushiceField Popcorn maskine: $popcornField Leje dato fra: $lejefraField Leje dato til: $lejetilField Forventet antal gæster: $gaestField Beskrivelse af arrangement: $beskrivelseField EOD;    $headers = “From: $emailFieldrn”;    $headers .= “Content-type: text/htmlrn”;    $success = mail($webMaster, $emailSubject, $body, $headers);    /* Results rendered as HTML */    $theResults = <<<EOD        Velkommen til Bjarnes Pølser                #container {                width:1000px;                height:auto;                margin:auto;            }            #header {                width:800px;                height:150px;                margin-left:42;                margin-right:auto;            }            #sidebar {                float:right;                width:200px;                height:500px;                margin-left:auto;                margin-right:auto;            }            #main {                width:800px;                height:500px;                background-image:url(images/Main.png);                background-repeat:repeat-y;                margin-left:42;                margin-right:auto;                    }            #footer {                width:800px;                height:60px;                margin-left:42;                margin-right:auto;            }                                                                                                                    Vi takker for din forespørgsel. Din forespørgsel vil blive besvaret hurtigst mulig!                                                             EOD;echo “$theResults”;?>

  15. says: Ediesweedie

    does network solutions use different setting for this.  i have used your php form for several websites, but for some reason, gathering the data variables isn’t working.  the html email comes through with no info and no email address?  HELP!  [email protected]..

  16. says: Anne

    As stated in a previous comment from another user, I also needed to take out the word field for my form to work properly but in general I found your tutorials really easy to follow and I just wanted to say a massive thank you!

  17. says: Ronb

    I did this tutorial and successfully ONCE got the form to work. Now I’m trying it again, using the code that WORKED (this is not yelling, only putting emphasis on the word), and following all the same steps. But when I get down to the EOD; close before echo, it doesn’t show code coloring, which I understand that it means it’s NOT working.

    Any suggestions? Also, how do you get the checkboxes to download in the email results?

  18. says: Abelpede

    Sweet tutorial brother!…. it worked like a charm!….do you mind creating a second part of this tutorial but with a upload file field on this form?….I had been searching all around for this …and is all useless!!….i kinda like the way you explained things!!!…. i don’t know nothing about php and i just code my first page thanks to you man!  but i kinda lost  on how to make this field work with the form….and received a file(image) attached to the email that this form is producing.

    thanks

    man!!!
    Abel

  19. says: TlFord

    I enjoyed the tutorial, but I do have one question. The form is sending the email, but the info in the form does not appear. How do I fix that?

  20. Hey, i had some problems with my script if one of you could help me it would be greatly appericated.
    ————————————————————————————————————————————-
    <?php
    $emailSubject = 'Inquiry From Website';
    $webMaster = '[email protected]';

    $nameField = $_POST['name'];
    $emailField = $_POST['email'];
    $phoneField = $_POST['phone'];
    $coqField = $_POST['coq'];
    $sendField = $_POST['send'];

    $body = <<<EOD

    Name: $name
    Email: $email
    Phone: $phone
    Subscription: $send
    Inquiry: $coq
    EOD;
    $headers = “From: $emailrn”;
    $headers .= “Content=type: text/htmlrn”;
    $success = mail($webMaster, $emailSubject, $body, $headers);

    $theResults = <<
    ————————————————————————————————————
    Thanks guys!

  21. says: MO

    Nathan:
    Great PHP script! After correcting the EOD and $headers lines, everything worked fine; however, I’m getting two messages sent for each completed form: One from the originating email account and one from my hosting service. The email from sent from my hosting service has blank fields (i.e., it doesn’t capture the information). I’m attempting to troubleshoot that and is probably an issue with my hosting service. Thanks again. Your site is a God send and a tremendous resource to the uninitiated.

  22. says: Hunky Dory

    Thank you Nathaniel, your tutorials are always the best! Everything works with this script for me, except that I cannot reply directly but instead need to open new mail and copy paste the address of sender. How do I fix that?

  23. says: Tdog

    Nothing comes back in the email. No data? How do you fix this? I did my own PHP script based on my fields but did it the same way as this tutorial. It should have data. HELP!

  24. says: Katrina

    I have tried everything and I can’t get the email address field to work. It comes up as Email: when it sends me the form in my email address. It also doesn’t show the email address of who it is from. Everything else in the form and submission part worked. If anyone can help me with the Email address field I would greatly appreciate it. Please email me at [email protected].

  25. says: HTTR

    This was a very effective tutorial and worked great. The only problem I have is that I would like the form fields to clear after submit/send button is pressed. When hitting the back button from the Thank you page, the entered info is still present, very annoying. Is there a way to have the info clear automatically after submission other than hitting the refresh button at the top of the page? Any help would be appreciated.

      1. says: Emma

        out of interest, how long did the message take to send? Also, what local server were you using and did you copy the tutorial directly? I am trying to do the same thing on a local server (XAMPP) and it doesn’t seem to be working – so far…thanks!

  26. says: csmith

    I hope this is still monitored…
    I haven’t received the email that I have sent from the form submission.
    I received the html window showing the “thank you” message… but no email ever arrives.
    Any help would be appreciated – I can send or past the code if requested.

    1. says: csmith

      Strike the above, I have now received the email (changed email address… must be a server issue with the other one) HOWEVER, now I have the same issue as others seem to have. The content that was entered into the form was not received … exception is the comment area… strange! Any help out there?
      thanks

  27. says: Sasi spenzer

    hey guys this is my script..can you find any error in these code?? when i click my submit button it displaying the $body variable data..ohhh i m going crazy 😀 🙁

    <?php
    /*my variables*/
    $webSubject = 'myscripting';
    $webMaster = '[email protected]';
    /* getting data to the form*/

    $nameField = $_POST['name'];
    $emailField = $_POST['email'];
    $phoneField = $_POST['phone'];

    $body = <<<EOD

    Name: $email
    Email: $email
    Phone: $phone
    EOD;

    $Headers = “From: $emailrn”;
    $Headers .= “Content-type: text/htmlrn”;
    $success = mail ($webMaster, $emailSubject, $body, $Headers);

    ?>

  28. says: lisa

    Hi Nathaniel, thanks for this tutorial, my first php experience! You explain things very well and I got my form to work. I have one little problem and am wondering if you know how to fix it….

    since I have the form up on my website I am getting emails from [email protected] (hostpapa is my server) and all the answers to the email fields are the same; 1. So I get:

    name: 1
    email: 1
    message: 1
    What does this mean? Is this spam or did I do something wrong?
    I have tried the form and get the right answers that I filled out, so it works, but these hostpapa emails are annoying. I have contacted them and they don’t know what it is and think it might be my form…
    I hope you can help me

    1. says: Shealtiel

      Hello Lisa. Do you have the php code that Nathaniel wrote? Because I looked for the download page that was mentioned in the video, but I wasn’t able to find it. I think the file is no longer available or has been moved. Could you help me out on this?
      Thanks.

  29. says: Julie

    It seems to work on the website but I do not get the email. I’ve gone over it and over it many times and I think it is perfect.

  30. says: CarlM

    Hi guys, I followed thru with the tutorial. I have my own form, I didn’t use the downloadable one. So, when I click my ‘submit’ button, the Mail icon hops on my dock. Is the ‘submit’ button on the form suppose to link to the php? Or is there any missing code I have to write in the php file?

  31. says: Trevor

    I was able to tweek the PHP code with everyone’s comments and got it to work. My email came through finally with all the content.

    Here is my problem. My form has 3 fields for individuals to attach images before they send it to me. After someone submits their content, my email shows all the content and the ONLY file names of the attach images instead of the images themselves. What do I need to do to fix this problem?

    Thanks,
    Trevor

  32. says: Emma

    The code works fine except the email does not send. I have used a ymail account and a gmail account, neither of which have worked. Any help would be much appreciated as I am going crazy!! Thanks!

  33. Pingback: Divx Download
  34. Pingback: теилим
  35. Pingback: Directory
  36. Pingback: satta matka
  37. Pingback: Agen Bandarq
  38. Pingback: yandex ankara
  39. Pingback: economics tuition
  40. Pingback: ICQ Chat
  41. Pingback: London or UK
  42. Pingback: Akantor Webdesign
  43. Pingback: auto transport
  44. Pingback: pizza dog
  45. Pingback: para kazanmak
  46. Pingback: ankara escort
  47. Pingback: para para dinle
  48. Pingback: rhodium
  49. Pingback: joe de saram
  50. Pingback: rhodium
  51. Pingback: openhaard
  52. Pingback: kimsin sen
  53. Pingback: cul
  54. says: Linda

    I need help. I am getting the email body text but the variables are not showing up in my email? anyone able to help me see what is wrong. I see I am not the only one with this problem.

  55. Pingback: Cheap
Leave a comment
Leave a comment

Your email address will not be published. Required fields are marked *