UDT mini Mailer

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
mw
Forum Members
Forum Members
Posts: 159
Joined: Mon Aug 25, 2008 8:38 pm

UDT mini Mailer

Post by mw »

a little mailer that uses php to send email to designated recipient.
because it uses  "PHP_SELF" the echo commands are placed on the same page as the form resides.
useful for grabbing variables from other modules and using them in the form.

create a UDT called "simple_mailer" and paste in the code below:

Code: Select all

if(!empty($_POST['name'])
    || !empty($_POST['email'])
    || !empty($_POST['message']))
	{    
$to = "youremail@somewhere.com"; 
$subject = "Form submission";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$option = $_POST['radio'];
$dropdown = $_POST['drop_down'];
$message = $_POST['message'];
$header = "From: ". $name_field . " <" . $email_field . ">\r\n"; //optional headerfields
$header .= "Reply-To: $name_field < $email_field >\n";
$header .= "X-Mailer: PHP/" . phpversion() . "\n";
$header .= "X-Priority: 1";

foreach($_POST['check'] as $value) {
		$check_msg .= "Checked: $value\n";
	}

        $body = stripslashes($_POST['message']);    
	$body .= "\n\n---------------------------\n";    
	$body = "From: $name_field\n E-Mail: $email_field\n $check_msg Option: $option\n Drop-Down: $dropdown\n Message:\n $message\n";

if(mail($to,$subject,$body,$header)) {
echo "<h3> Your Submission has been sent! </h3>";
echo "<p>Thank you<strong> $name_field </strong><p />";
echo "<p>You may wish to print this page as a reference.</p>";
echo "<h3>Contact Details</h3><br />";
echo "<br />";
echo "<strong>Name</strong>: {$name_field}<br />";
echo "<strong>Email</strong>: {$email_field}<br />";
echo "<br />";
echo "<h3>Submission Details</h3>";
echo "<p><p />";
echo "<strong>Subject</strong>: {$subject}<br />";
echo "<strong>Options</strong>: {$option} <br />";
echo "<strong> Language</strong>: {$dropdown}<br />";
echo "<strong>Favorite color</strong>: {$check_msg} <br />";
echo "<strong>Message</strong>:{$message}<br />";
echo "<div id=\"errorbox\" style=\"display: none\">";

} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}}

echo '<form action="http://' . $_SERVER['SERVER_NAME']  . $_SERVER['PHP_SELF'] ."?". $_SERVER['QUERY_STRING'] . '" method="post" name="form1" onsubmit="return validate_form(this)" name="[object]">';

create a new page and in text view paste the below :

Code: Select all

{simple_mailer} 
<div style="font-weight: bold; color: red" id="errorbox"></div>
Name: <input name="name" size="19" type="text" /><br />   <br /> 
E-Mail: <input name="email" size="19" type="text" /><br />   <br />
<input name="check[]" value="blue_color" type="checkbox" /> Blue<br />
<input name="check[]" value="green_color" type="checkbox" /> Green<br />
<input name="check[]" value="orange_color" type="checkbox" /> Orange<br /> <br />
<input value="yes" name="radio" type="radio" /> YES<br />   
<input value="no" name="radio" type="radio" /> NO <br />   <br />  
 <select name="drop_down"> 
<option value="php">php</option>
 <option value="xml">xml</option> 
<option value="asp">asp</option>
 <option value="jsp">jsp</option>
 </select>   <br />   <br /> 
Message:<br />   
<textarea rows="9" name="message" cols="30"></textarea><br />   <br />   
<input value="Submit" name="submit" type="submit" /> 
</form>
place this javascript in your "head" section to validate the form fields:

Code: Select all

<__script__ type="text/javascript">
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 

  { 
document.getElementById("errorbox").innerHTML="";
document.getElementById('errorbox').innerHTML +=  alerttxt ;return false;}
else {return true}
}
}

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
 { 
document.getElementById("errorbox").innerHTML="";
document.getElementById('errorbox').innerHTML +=  alerttxt ;return false;}
else {return true}
}
}

function validate_form(form1)
{
with (form1)
{

if (validate_email(email,"Not a valid e-mail address!")==false)
  {email.focus();return false;}
if (validate_required(name,"Name must be filled out...")==false)
  {name.focus();return false;}
if (validate_required(email,"Email must be filled out...")==false)
  {email.focus();return false;}

}
}
</__script>

Now you have a simple contact form that has all the elements, drop down,radio,text input and text area.
it has error checking using client side javascript to make sure certain fields are filled in and will echo errors above the form.
if form is filled in correctly the results are echo'd on the same page and emailed...
Last edited by mw on Wed Feb 11, 2009 6:34 pm, edited 1 time in total.
pedes
Power Poster
Power Poster
Posts: 840
Joined: Tue Jan 27, 2009 11:47 am

Re: UDT mini Mailer

Post by pedes »

when putting the code in UDT it's giving an error "incorrect code"

Peter
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: UDT mini Mailer

Post by Nullig »

I think the first line of the UDT should be:

if(!empty($_POST['name'])

Nullig
pedes
Power Poster
Power Poster
Posts: 840
Joined: Tue Jan 27, 2009 11:47 am

Re: UDT mini Mailer

Post by pedes »

I think the first line of the UDT should be:

if(!empty($_POST['name'])
this is correct my best Nullig

and even there should be a closing tag for the form, otherwise the layout get messed up

so behind the formfields i used a UDT with just this in it:
echo '';
now it is working ok

Thx for your help
Peter
mw
Forum Members
Forum Members
Posts: 159
Joined: Mon Aug 25, 2008 8:38 pm

Re: UDT mini Mailer

Post by mw »

sorry, the copy/paste missed those two items as noted above.
I fixed up the code to include it.
Post Reply

Return to “Tips and Tricks”