...

View Full Version : Email Sending Script.



jonnybinthemix
11-23-2007, 07:12 PM
Hi Guys,

I have written (or tried to!) a simple email sending form for a site i am finishing.

My HTML code for the form is:



<form action="email.php" method="post" enctype="text/plain">

<div align="left" style="padding-left: 25px;"><b>Name:&nbsp;&nbsp;</b><input type="text" name="name" size="40" /></div>
<br />
<div align="left" style="padding-left: 25px;"><b>Email:&nbsp;&nbsp;</b><input type="text" name="email" size="40" /></div>
<br />
<div align="left" style="padding-left: 25px;"><b>Comment:</b><textarea name="comment" cols="40" wrap="virtual" rows="10"></textarea>
</div>
<br /><br />

<div alignt="left" style="padding-left: 20px;">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="reset" value="Reset">
</div>


</form>


And the email.php script is:


<html>
<head><title>Sending Email...</title></head>
<body>
<?php

$to = "someone@something.com";
$email = $HTTP_POST_VARS['email'];
$name = $HTTP_POST_VARS['name'];
$message = $HTTP_POST_VARS['message'];


if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($to,$email,$name,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>


The problem is, it doesent seem to work. It just comes back and says invalid email everytime i try to send one.

Does anyone have any idea?

Many Thanks.

NB: someone@something.com, i put this in to replace the other persons email address.

Jon

timgolding
11-23-2007, 07:52 PM
I would seriously consider looking to PEAR for email validation, actually any validation. Although getting it installed can be a nightmare most of them work as separate classes in the document ROOT.

jonnybinthemix
11-23-2007, 07:55 PM
Now that was confusing!! hehe

Ive used this PHP method before and it seemed to work okay, this is not going to be a website where millions of people will me mailing, its not going to be used that often...

To keep things simple, for now, I woul probably prefer to get the PHP version working.

Thanks for you advice though :D

Jon

timgolding
11-23-2007, 08:27 PM
Hehe, sorry let me explain! PEAR is a PHP extension its all in PHP
download this. extract it and Insert it in your document root as a folder.



<PHP
include($_SERVER['DOCUMENT_ROOT'].'/Validate-0.7.0/validate.php');
$valid=__emailRFC822($email,$options);
?>

best thing about PEAR is it is securer for validation ;)

jonnybinthemix
11-23-2007, 08:30 PM
Im not sure i understand what you mean :(

If i add that to my current script, surely it still wont work because it doesent work now. If all PEAR does is validation will that fix the problem of it not working at the moment?

Thanks again.

Jon

Inigoesdr
11-23-2007, 08:35 PM
Your script is failing because something is wrong with the regular expression used to check the e-mail address. tim's suggestion was to use a PEAR module to check the e-mail instead. However, you don't seem to know enough about PHP/PEAR to handle this, so I would suggest you do a search of this forum, and the internet for an alternative e-mail validation regex.

timgolding
11-23-2007, 09:11 PM
OK sorry try this :) PS loving your quotes :)


<html>
<head><title>Sending Email...</title></head>
<body>
<?php

$to = "someone@something.com";
$email = $HTTP_POST_VARS['email'];
$name = $HTTP_POST_VARS['name'];
$message = $HTTP_POST_VARS['message'];


if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*
(\.(com|net|org|edu|biz|gov|info|aero|cat|coop|int|jobs|mil|mobi|museum|name|pro|tel|travel|[a-z]{2})$", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($to,$email,$name,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>

CFMaBiSmAd
11-23-2007, 09:20 PM
$HTTP_POST_VARS are depreciated and are turned off by default in all current versions of PHP5.

Check your web server log for errors and/or turn on full php error reporting to get php to help you.

If you are getting any notice/warning/error messages, find and fix them.

Start by replacing any $HTTP_POST_VARS with $_POST



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum