mrbios Posted June 30, 2010 Posted June 30, 2010 I'm using dreamweaver CS5 and i've tried to adapt a helpful online tutorial to my needs, this is what i have come up with thus far: //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the name field is not empty if(trim($_POST['contactname']) == '') { $hasError = true; } else { $name = trim($_POST['contactname']); } //If there is no error, send the email if(!isset($hasError)) { $emailTo = '******************'; //Put your own email address here $body = "Name: $name \n\n"; $headers = 'C3 Referral Form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $body, $headers); $emailSent = true; } } ?> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> C3 Referral Form <br /> $(document).ready(function(){<br /> $("#contactform").validate();<br /> });<br /> <br /> body {<br /> font-family:Arial, Tahoma, sans-serif;<br /> }<br /> #contact-wrapper {<br /> width:880px;<br /> border:1px solid #e2e2e2;<br /> background:#f1f1f1;<br /> padding:20px;<br /> }<br /> #contact-wrapper div {<br /> clear:both;<br /> margin:1em 0;<br /> }<br /> #contact-wrapper label {<br /> display:block;<br /> float:none;<br /> font-size:16px;<br /> width:auto;<br /> }<br /> form#contactform input {<br /> border-color:#B7B7B7 #E8E8E8 #E8E8E8 #B7B7B7;<br /> border-style:solid;<br /> border-width:1px;<br /> padding:5px;<br /> font-size:16px;<br /> color:#333;<br /> }<br /> form#contactform textarea {<br /> font-family:Arial, Tahoma, Helvetica, sans-serif;<br /> font-size:100%;<br /> padding:0.6em 0.5em 0.7em;<br /> border-color:#B7B7B7 #E8E8E8 #E8E8E8 #B7B7B7;<br /> border-style:solid;<br /> border-width:1px;<br /> }<br /> Name of Student: Tutor Group: Staff Member: Date of Incident: Period: Subject: Rule Broken Explanation of Incident C1 C2 C3 Exact time sent to BfL: Signed: My problem is that it won't send any emails, i know that the top section of the php code definately needs adapting so that the body includes all my fields....i think (?) but first things first i want to get it to actually send an email so i can see what it looks like in the email, any help muccccccccccccccccchly appreciated
LosOjos Posted June 30, 2010 Posted June 30, 2010 Just to point out the bleeding obvious first, you have changed this line: $emailTo = '******************'; //Put your own email address here so that your email address is there rather than stars haven't you?
LosOjos Posted June 30, 2010 Posted June 30, 2010 Yea, i edited that out on purpose just checking First thing that pops to mind is that you're not giving the email a subject, as it's a web form I'd imagine you want this to be a constant so you can identify them in your inbox, so try this: just before the 'mail()' line, add: $subject="Email subject goes here"; Then alter your mail line like so: mail($emailTo, $subject, $body, $headers); 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Cheers, ive added that in but still no email As it's only going to be used internally is there any way i can do something like specifying the mail server and addresses to use for sending etc?
webman Posted June 30, 2010 Posted June 30, 2010 PHP's mail function depends on its configuration in php.ini. On Windows, you should specify an SMTP server for it to use. On Linux/Unix, you need to specify the path to sendmail (or a sendmail-like program. Which in turn can be configured to send via SMTP or relay it directly). 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Ah ok, in this case im using Winows and it's hosted on IIS7 What do i need to edit in the php.ini?
webman Posted June 30, 2010 Posted June 30, 2010 Do a search for SMTP, and you should find this area: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 Specify your favourite mail server hostname or IP address in there (replacing localhost in the example above). Ensure the SMTP server security/policies will accept and relay messages from your IIS host though. 1
LosOjos Posted June 30, 2010 Posted June 30, 2010 I'm just looking at your $headers variable and I think that might be causing the error, AFAIK you can't have custom information in the header. Try changing the $headers variable to this: $headers = "From: " . $emailTo . "\r\nReply-To: " . $emailTo; 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Do a search for SMTP, and you should find this area: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 Ok i think i've configured that correctly, i've also configured a custom SMTP connector to recieve mail from my web server, is that correct? EDIT: cheers LosOjos i'll do that now
webman Posted June 30, 2010 Posted June 30, 2010 Yes that should be it. Don't forget to restart the web server after making changes to php.ini. 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 wohoo it emails thank you both Now then, give me 5 mins and i'll be back with my next problem
mrbios Posted June 30, 2010 Author Posted June 30, 2010 (edited) Ok next problem i have then, i now want the information entered into each field of the form to go into the email but i want to be able to format the email into a nice layout that when printed out will look somewhat like the form does, is this possible and is it easy enough to achieve? At the moment the email just emails me the word "Name:" and nothing else, i assume this is to do with the contents in the very beginning piece of code? Edited June 30, 2010 by mrbios
webman Posted June 30, 2010 Posted June 30, 2010 The contents of the mail that is sent is stored in the $body variable. Presently, it is set to do very little: $body = "Name: $name \n\n"; Taking that back a step or two, you first look at where $name is. $name = trim($_POST['contactname']); That retrieves the value of the form field named 'contactname', strips any leading/trailing whitespace and assigns it to the $name variable. You just need to add similar lines for your other fields. In your form: I would recommend changing the value of the "name" attribute to something without spaces and uppercase letters. E.g, tutor_group. Same goes for all other fields. Then: $name = trim($_POST['contactname']); $tutor_group = trim($_POST['tutor_group']); ... $body = "Name: $name \n\n"; $body .= "Tutor group: $tutor_group\n\n"; ... So you just carry on adding to the $body variable with the appropriate fields. 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Cheers webman, does that php use the "id" part of the items or the "name" part of the items?
webman Posted June 30, 2010 Posted June 30, 2010 Only the name part is used for forms when passing data. The ID is used to uniquely identify elements for CSS and Javascript.
mrbios Posted June 30, 2010 Author Posted June 30, 2010 And frustratingly i've just realised it's case sensitive, bah So i now have all the fields emailing me to look like the following: Student Name: 1 Tutor Group: 2 Staff Member: 3 Date of Incident: 4 Period: 5 Subject: 6 Is there any way i can setup the code so that it comes out more like this: Student Name: qwerty -------------------- Tutor Group: 8C Staff Member: qwerty --------------------- Date of Incident: 929294 Period: 3 --------------------------------- Subject: English Where the "-"s are just blank space
webman Posted June 30, 2010 Posted June 30, 2010 Yes, in theory Sending it as plaintext allows certain characters such as \n (newline) and \t (tab). For example: $body = "Student name: $name \t\t Tutor Group: $tutor_group\n\n"; This has certain drawbacks that the values for $name etc will be of different and unknown lengths - resulting in some lines longer than others. Without some messy calculations (or generating an HTML email) I think that's the best you are likely to achieve. With HTML email you can format that data nicely in a table. If you want to do this, have a look at Example 4 on the mail() function documentation. 1
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Thank you very much, i'll go and have a play with that in a bit, time to refuel my brain with a sarnie
LosOjos Posted June 30, 2010 Posted June 30, 2010 And frustratingly i've just realised it's case sensitive, bah So i now have all the fields emailing me to look like the following: Is there any way i can setup the code so that it comes out more like this: Student Name: qwerty -------------------- Tutor Group: 8C Staff Member: qwerty --------------------- Date of Incident: 929294 Period: 3 --------------------------------- Subject: English Where the "-"s are just blank space You can add tab spaces in to your PHP code by adding '\t' (no quotes) anywhere in your strings, but it won't neccessarily work out how you want it to if a particularly long name is entered. An alternative is to make the email that gets sent HTML. First you need to add info to the email header to make it HTML. Add the following directly after you set the $headers variable: $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; Now simply write your $body variable as though it were HTML, using a table to get the layout how you want it EDIT: webman beat me to it
webman Posted June 30, 2010 Posted June 30, 2010 mrbios: Great idea about lunch! LosOjos: Great minds think alike
mrbios Posted June 30, 2010 Author Posted June 30, 2010 How does this look: //If the form is submitted if(isset($_POST['submit'])) { $name = trim($_POST['Student']); $tutor = trim($_POST['TutorGroup']); $staff = trim($_POST['StaffMember']); $doi = trim($_POST['DateofIncident']); $period = trim($_POST['period']); $subject1 = trim($_POST['Subject1']); $C1 = trim($_POST['C1']); $C2 = trim($_POST['C2']); $C3 = trim($_POST['C3']); $C1Rule = trim($_POST['C1Rule']); $C2Rule = trim($_POST['C2Rule']); $C3Rule = trim($_POST['C3Rule']); $Signed = trim($_POST['Signed']); $Time = trim($_POST['Time']); $emailTo = '[email protected]'; $subject="C3 Referral Form"; //Plain text email: $body = "Student Name: $name \n\n Tutor Group: $tutor \n\n Staff Member: $staff \n\n Date of Incident: $doi \n\n Period: $period \n\n Subject: $subject1 \n\n C1 Rule broken: $C1Rule \n\n C2 Rule Broken: $C2Rule \n\n C3 Rule Broken: $C3Rule \n\n C1 Explanation: $C1 \n\n C2 Explanation: $C2 \n\n C3 Explanation: $C3 \n\n Time sent to BFL: $Time \n\n Signed: $Signed \n\n"; $headers = "From: " . $emailTo . "\r\nReply-To: " . $emailTo; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $message = ' C3 Referral Form '; mail($emailTo, $subject, $message, $headers); $emailSent = true; } ?> I've left the old body there and just commented it out for now, and changed the bit in mail() from $body to $message, i obviously still need to add in the content to the body, but apart from that is that correct?
webman Posted June 30, 2010 Posted June 30, 2010 Looks OK. When testing, to save on mailing each time, you could just do echo $message; to show it on the page instead, and comment out the mail() line.
mrbios Posted June 30, 2010 Author Posted June 30, 2010 Right i've created a new page, made myself a form with 3 tables, i've copied all the code from that page into the body section of my html email bit, now how do i declare the variables within the email? just put $name etc or is there more to it than that?
webman Posted June 30, 2010 Posted June 30, 2010 (edited) Like you had before - this bit: $message = ' C3 Referral Form '; When using single quotes you do it like this: $message = ' .... </pre><table> Name:' . $name . ' </table><br> <br> <br><p> Edited June 30, 2010 by webman 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now