PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Sindarin
Forum Regular
Posts: 521 Joined: Tue Sep 25, 2007 8:36 am
Location: Greece
Post
by Sindarin » Thu Jan 31, 2008 8:30 am
I am using the below script to send an email but after testing the form the email is not delivered at all. Is something wrong?
Code: Select all
<?
/*
Send to a friend form
*/
//function email_validate() - validates an email address
function email_validate($email_address) {
if (strlen (trim ($email_address)))
return (eregi("^[a-z0-9]([_\\.\\-]?[a-z0-9]+)*@((([a-z0-9]+[a-z0-9\\-]*[a\-z0-9]+)|[a-z0-9])+\\.)+[a-z]{2,10}$", $email_address));
return FALSE;
}
//Get the variables from the form
$form_to=$_POST['form_to'];
$form_sendername=$_POST['form_sendername'];
$form_senderemail=$_POST['form_senderemail'];
$form_targetemail=$_POST['form_targetemail'];
$form_message=$_POST['form_message'];
if (email_validate($form_senderemail)==FALSE)
{
//sender email is not valid
header ('Location: send-error-senderemail.html');
}
if (email_validate($form_targetemail)==FALSE)
{
//target email is not valid
header ('Location: send-error-targetemail.html');
}
//setup the email message
$email_subject="-snip-";
$email_message="-snip-";
//send email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-7\r\n";
$headers .= "To: <$form_to> \r\n";
$headers .= "From: <$form_senderemail>\r\n";
mail($form_to, $email_subject, $email_message, $headers);
//for debugging
echo "email was sent";
?>
EDIT: Now I get a: SMTP server response: 501 5.2.1 Need Rcpt command. in e:\sites\-snip-\sendToFriend\send-to-friend.php on line 58
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Fri Feb 01, 2008 3:17 am
Don't include the from variable in the headers.
Do it like this:
Code: Select all
//REMOVE THE FROM STUFF FROM THE HEADER
$headers .= "From: <$form_senderemail>\r\n"; //DELETE
//CHANGE ABOVE TO:
$from="From: <$form_senderemail>";
//AMEND THE MAIL() FUNCTION SO THAT IT LOOKS LIKE THIS
mail($form_to, $email_subject, $email_message, $headers, "-f $from");
Sindarin
Forum Regular
Posts: 521 Joined: Tue Sep 25, 2007 8:36 am
Location: Greece
Post
by Sindarin » Fri Feb 01, 2008 3:37 am
So that protects against injection?
Btw, I found my problem was that I was passing a wrong variable ($form_to) to the "to" header. I must get some rest.
Sindarin
Forum Regular
Posts: 521 Joined: Tue Sep 25, 2007 8:36 am
Location: Greece
Post
by Sindarin » Fri Feb 01, 2008 8:09 am
Don't include the from variable in the headers.
I did what you say and "Nobody" appears on the sender field.