Page 1 of 1

e-mail is not being sent - Send to friend form

Posted: Thu Jan 31, 2008 8:30 am
by Sindarin
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

Re: e-mail is not being sent - Send to friend form

Posted: Fri Feb 01, 2008 3:17 am
by aceconcepts
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");
 

Re: e-mail is not being sent - Send to friend form

Posted: Fri Feb 01, 2008 3:37 am
by Sindarin
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. :?

Re: e-mail is not being sent - Send to friend form

Posted: Fri Feb 01, 2008 8:09 am
by Sindarin
Don't include the from variable in the headers.
I did what you say and "Nobody" appears on the sender field.