<form action="feedback.php" method="post" id="myform" name="myform">
<label for="myname">Name</label><input type="text" value="" size="30" maxlength="100" name="myname" id="myname" /><br /><br />
<label for="myemail">Email</label> <input type="text" value="" size="30" maxlength="100" name="myemail" id="myemail" /><br /><br />
<input type="radio" id="rd1" name="myradio[]" value="1" /><label for="rd1">First radio</label>
<input type="radio" id="rd2" name="myradio[]" value="2" checked="checked" /><label for="rd2">Second radio</label>
<br /><br />
<input type="checkbox" id="chk1" name="mycheckbox[]" value="1" /><label for="chk1">First checkbox</label>
<input type="checkbox" id="chk2" name="mycheckbox[]" value="2" checked="checked" /><label for="chk2">Second checkbox</label>
<br /><br />
<label for="myselect">Type of Enquiry</label>
<select name="myselect" id="myselect">
<optgroup label="Sales">
<option value="1" selected="selected">East Coast</option>
<option value="2">South America</option>
<option value="3">Asia Pacific</option>
<option value="4">Africa</option>
</optgroup>
<optgroup label="IT" >
<option value="5">System Issues</option>
<option value="6">Webpage Feedback</option>
</optgroup>
</select><br /><br />
<textarea name="mytextarea" id="" rows="3" cols="30">
Text area
</textarea> <br /><br />
<button id="mysubmit" type="submit">Submit</button>
</form>
<?php
// include the phpmailer class, which can be found here: http://sourceforge.net/projects/phpmailer
// after downloading, put it in one of the included directories specified by "includes" in the php config
require("class.phpmailer.php");
// parse the form and send the email:
// we have the following sample structure from the submitted form:
/*
POST data
Array
(
[myname] => my name
[myemail] => myemail@email.com
[myradio] => Array
(
[0] => 2
)
[mycheckbox] => Array
(
[0] => 2
)
[myselect] => 4
[mytextarea] => Text area
)
NOTE: Be aware that "mycheckbox" will not be present if no boxes are checked
*/
//How you compose the Subject and body is up to you but as an example:
$subject = "Website Enquiry";
$body = "An enquiry has been sent from " . $_POST['myname'] . " (" . $_POST['myemail'] . ")<br/> Their enquiry is below:<br/>" . $_POST['mytextarea'] . "";
$contacts = array("EC","SA","APAC","AF","SI","WEB"); // in this case it is prefixed to "@mydomain.com"
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$my_domain = "mydomain.com";
$my_reply = array("address"=>"noreply" . "@" . $my_domain, "name"=>"no reply");
$smtp_server = "localhost";
$smtp_port = "25";
$smtp_user = "uname"; //username
$smtp_pass = "pword";//password
$mail->Host = "$smtp_server:$smtp_port"; // SMTP servers
$mail->SMTPAuth = false; // turn on SMTP authentication if required by setting to true
// replace <<uname>> and <<pword>>
$mail->Username = "$smtp_user"; // SMTP username
$mail->Password = "$smtp_password"; // SMTP password
// replace the following within the << ... >>
$mail->From = $my_reply["address"];
$mail->FromName = "My Company";
$mail->AddAddress($contacts[$_POST['myselect']] . "@" . "$my_domain");
$mail->AddAddress($_POST['myemail']);
$mail->AddReplyTo($my_reply["address"],$my_reply["name"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "$subject";
$mail->Body = "$body";
$mail->AltBody = $mail->Body;
$mail->Send();
?>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented: