View Full Version : PHP - Mail script
Sammie
June 16th, 2008, 21:31
Hello,
I am currently working n a project that i am doing, i have a contact page which is a form, i have four different boxes for the visitors to place text in, full name, email, subject and message.
Each name to each field is provided below.
Full name: name
Email address: mail
Subject: subject
Message: msg
My question is, how will i go around validating all these fields with PHP and also sending it to my email address, any help is appreciated. :cool2:
Tree
June 16th, 2008, 23:27
<?php
$to['email'] = 'me@mymail.net';
$to['name'] = 'Sammie';
$errors = array();
if (empty($_POST['name'])) # If name is empty throw an error
$errors[] = 'Please provide your full name';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['mail'])) # If email is not a valid email address, throw an error
$errors[] = 'Please provide a valid email address';
if (empty($_POST['subject'])) # If subject is empty, throw an error
$errors[] = 'Please provide a subject';
if (!isset($_POST['msg']{10})) # If message is not at least 10 characters, throw an error
$errors[] = 'Please provide a message';
if (empty($errors)) {
$headers = 'MIME-Version: 1.0' . "\r\n" .
"Content-type: text/html; charset=iso-8859-1\r\n" .
"From: " . $_POST['name'] . " <" . $_POST['mail'] . ">\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $to['name'] . ',<br><br>' . $_POST['name'] . ' has sent you this message:<br><br>' . $msg;
mail($to['name'] . ' <' . $to['email'] . '>',$_POST['subject'],$body,$headers);
} else {
$message = '<b style="color:Red;text-align:center;">';
foreach ($errors as $error) {
$message .= $error.'<br>';
}
$message .= '</b>';
echo $message;
# Show the form
}
?>
Sammie
June 17th, 2008, 00:52
Looks great, thanks, however, how do i get it so a thank you message comes up when it gets sent?
Thanks
Edit: Also, how would you go around adding a go back link?
Tree
June 17th, 2008, 01:51
There's probably better solutions to the form validations, but this should suffice.
<?php
$fields = array('name' => 'Name', 'email' => 'Email Address', 'subject' => 'Message Subject', 'msg' => 'Message'); // Assign an array of fields where the key is a name that can be used in field names, etc while the value is a friendly, nicely capitalized description
$to['email'] = 'me@mymail.net'; # Set to the email address you wish to recieve the mail
$to['name'] = 'Sammie'; # Set to the name that is going to recieve the mail
$thankyoupage = 'thankyou.php'; # Set to the name of a page a client will be shown after submitting the form
$self = basename($_SERVER['PHP_SELF']);
if (isset($_POST['submit'])) {
$errors = array();
if (empty($_POST['name']))
$errors[] = 'Please provide your full name';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email']))
$errors[] = 'Please provide a valid email address';
if (empty($_POST['subject']))
$errors[] = 'Please provide a subject';
if (!isset($_POST['msg']{10}))
$errors[] = 'Please provide a message';
if (empty($errors)) {
$headers = 'MIME-Version: 1.0' . "\r\n" .
"Content-type: text/html; charset=iso-8859-1\r\n" .
"From: " . $_POST['name'] . " <" . $_POST['mail'] . ">\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $to['name'] . ',<br><br>' . $_POST['name'] . ' has sent you this message:<br><br>' . $msg;
mail($to['name'] . ' <' . $to['email'] . '>',$_POST['subject'],$body,$headers);
include('thankyou.php'); # Include a page thanking the client for emailing
} else {
$message = '<b style="color:Red;text-align:center;">';
foreach ($errors as $error) {
$message .= $error.'<br>';
}
$message .= '</b>';
unset($_POST['submit']); # Unset $_POST['submit'] so the script will display the form again
include($self);
}
} else {
if (isset($message)) echo $message;?>
<form action="<?php echo $self; ?>" method="POST">
<table>
<?php foreach ($fields as $key => $value) { ?>
<tr>
<td><?php echo $value; ?></td>
<td>
<?php if ($key == 'msg') echo '<textarea name="msg">' . $_POST['msg'] . '</textarea>';
else echo '<input type="text" name="' . $key . '" value="' . $_POST[$key] . '" />'; ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit" />
</td>
</tr>
</table>
</form>
<?php } ?>
Sammie
June 17th, 2008, 02:13
Just tested it, and no matter what it kept coming up with 'Please provide a valid email address' even though i had the correct format in that field. :S
Plus, i am not using tables for my form, so can we somehow adjust the first one you posted?
Tree
June 17th, 2008, 02:22
Modified the above code, the email problem was just a one-character error. As for the tables, you can insert whatever code you'd like between the <form> tags, it's just HTML.
Sammie
June 17th, 2008, 02:27
Hmm just updated my file and it's still doing the same thing.
Please provide a valid email address.
<?php
$fields = array('name' => 'Name', 'email' => 'Email Address', 'subject' => 'Message Subject', 'msg' => 'Message'); // Assign an array of fields where the key is a name that can be used in field names, etc while the value is a friendly, nicely capitalized description
$to['email'] = 'sam.c.gordon@gmail.com'; # Set to the email address you wish to recieve the mail
$to['name'] = 'Sammie'; # Set to the name that is going to recieve the mail
$thankyoupage = 'thankyou.php'; # Set to the name of a page a client will be shown after submitting the form
$self = basename($_SERVER['PHP_SELF']);
if (isset($_POST['submit'])) {
$errors = array();
if (empty($_POST['name']))
$errors[] = 'Please provide your full name';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['mail']))
$errors[] = 'Please provide a valid email address';
if (empty($_POST['subject']))
$errors[] = 'Please provide a subject';
if (!isset($_POST['msg']{10}))
$errors[] = 'Please provide a message';
if (empty($errors)) {
$headers = 'MIME-Version: 1.0' . "\r\n" .
"Content-type: text/html; charset=iso-8859-1\r\n" .
"From: " . $_POST['name'] . " <" . $_POST['mail'] . ">\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $to['name'] . ',<br><br>' . $_POST['name'] . ' has sent you this message:<br><br>' . $msg;
mail($to['name'] . ' <' . $to['email'] . '>',$_POST['subject'],$body,$headers);
include('thankyou.php'); # Include a page thanking the client for emailing
} else {
$message = '<b style="color:Red;text-align:center;">';
foreach ($errors as $error) {
$message .= $error.'<br>';
}
$message .= '</b>';
unset($_POST['submit']); # Unset $_POST['submit'] so the script will display the form again
include($self);
}
} else {
if (isset($message)) echo $message;?>
<form action="<?php echo $self; ?>" method="POST">
<table>
<?php foreach ($fields as $key => $value) { ?>
<tr>
<td><?php echo $value; ?></td>
<td>
<?php if ($key == 'msg') echo '<textarea name="msg">' . $_POST['msg'] . '</textarea>';
else echo '<input type="text" name="' . $key . '" value="' . $_POST[$key] . '" />'; ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit" />
</td>
</tr>
</table>
</form>
<?php } ?>
Tree
June 17th, 2008, 02:40
Change if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['mail']))
to
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email']))
Sammie
June 17th, 2008, 03:10
Yup that worked, but when having it on my main design, it makes the whole site repeats itself inside of it? i'll provide screenshots and my code if you like.
Any other ways of doing this, will be appreciated.
Um not sure how you could code what i have into it.
<form action='source/contactsend.php' method='post'>
<p>Full name:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='name' value='' />
</p>
<p><br /><br />Contact email:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='mail' value='' />
</p>
<p><br /><br />Subject:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='subj' value='' />
</p>
<p><br /><br />Message:<span class='required'>*</span><br />
<textarea class='textbox1' rows='' cols='' name='msg'>
</textarea>
</p>
<p><br /><br /><br /><br /><br /><br /><br /><br />
<input type='submit' value='Submit' />
</p>
</form>
Tree
June 17th, 2008, 13:39
The site repeated itself because I originally had it including itself on errors. Now it just shows the form again.
<?php
$to['email'] = 'me@mymail.net'; # Set to the email address you wish to recieve the mail
$to['name'] = 'Sammie'; # Set to the name that is going to recieve the mail
$thankyoupage = 'thankyou.php'; # Set to the name of a page a client will be shown after submitting the form
$self = basename($_SERVER['PHP_SELF']);
if (isset($_POST['submit'])) {
$errors = array();
if (empty($_POST['name']))
$errors[] = 'Please provide your full name';
if (!eregi("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $_POST['email']))
$errors[] = 'Please provide a valid email address';
if (empty($_POST['subject']))
$errors[] = 'Please provide a subject';
if (!isset($_POST['msg']{10}))
$errors[] = 'Please provide a message';
if (empty($errors)) {
$headers = 'MIME-Version: 1.0' . "\r\n" .
"Content-type: text/html; charset=iso-8859-1\r\n" .
"From: " . $_POST['name'] . " <" . $_POST['mail'] . ">\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $to['name'] . ',<br><br>' . $_POST['name'] . ' has sent you this message:<br><br>' . $msg;
mail($to['name'] . ' <' . $to['email'] . '>',$_POST['subject'],$body,$headers);
include('thankyou.php'); # Include a page thanking the client for emailing
} else {
$message = '<b style="color:Red;text-align:center;">';
foreach ($errors as $error) {
$message .= $error.'<br>';
}
$message .= '</b>';
unset($_POST['submit']); # Unset $_POST['submit'] so the script will display the form again
}
}
if (!isset($_POST['submit']))
{
if (isset($message)) echo $message;?>
<form action='source/contactsend.php' method='post'>
<p>Full name:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='name' value='<?php echo $_POST['name']; ?>' />
</p>
<p><br /><br />Contact email:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='mail' value='<?php echo $_POST['mail']; ?>' />
</p>
<p><br /><br />Subject:<span class='required'>*</span>
<input type='text' class='fieldbox1' name='subj' value='<?php echo $_POST['subj']; ?>' />
</p>
<p><br /><br />Message:<span class='required'>*</span><br />
<textarea class='textbox1' rows='5' cols='10' name='msg'><?php echo $_POST['msg']; ?></textarea>
</p>
<p><br /><br /><br /><br /><br />
<input type='submit' value='Submit' name='submit' />
</p>
</form>
<?php } ?>
Sammie
June 18th, 2008, 03:15
Nevermind i worked it out.
Sammie
June 18th, 2008, 04:40
Nevermind it's fine. :)
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.