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

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

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post 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");
 
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post 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. :?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post by Sindarin »

Don't include the from variable in the headers.
I did what you say and "Nobody" appears on the sender field.
Post Reply