MAIL "from" is not user's email address...HELP!

Have the feeling that everybody is staring at you in the other forums? Then look here, in this forum nobody does care what you say ;-)

Moderator: Project members

Post Reply
Message
Author
adriac88
500 Command not understood
Posts: 1
Joined: 2007-06-01 22:20

MAIL "from" is not user's email address...HELP!

#1 Post by adriac88 » 2007-06-01 22:27

I have a "contact" page on my site, and when i receive an email from the user, instead of the from address being the user's, it shows up in my email as being from the server. can someone help me?!

the PHP code i have for the function.php code is:

function email_contact($userEmail, $subject, $mailMessage, $var = 0){
//tells PHP what mail server to use
$mail_server ="secureserver.net";
ini_set("SMTP", $mail_server);

//provides a 'from' address so spam filters don't complain
$userEmail = $_POST['useremail'];
$from_address = $userEmail;
ini_set("sendmail_from", $from_address);

//sets up the email recipient: {$_SESSION['username']['email']}
$recipient = "adriac@hawaii.edu";

// Try to send the email
if (mail($recipient, $subject, $mailMessage)) {
if($var == 1)
display_page();
}else
echo "mail failed to send";
}

and the code i have from the contact.php page is:

include "./functions.inc";

function validate_contact_page(){
$userEmail = trim($_POST['useremail']);
$message = trim(strip_tags($_POST['message']));
global $errors;
$errors = array();
if (!preg_match('#^([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})(\\]?)$#', $userEmail)) {
$errors['useremail'] = 'Your E-mail address that you entered is invalid.';
}
if($message == "" or $message == null){
$errors['message'] = 'You must enter something into the text area.';
}
if(@!empty($errors)){
display_page();
}
else{
$errors['none'] = true;
$subject = 'E-mail from D.ONE Website';
email_contact($userEmail, $subject, $message);
display_page();
}
}

cor
426 Connection timed out
Posts: 49
Joined: 2007-10-01 12:52

#2 Post by cor » 2007-10-01 22:56

Firstly, if that's your real email address, I'd definitely edit your post and put in a dummy.

Secondly, that code is just rubbish. I'm assuming you didn't write it. Anyways, setting mail headers with ini_set is bad news. Try hacking it with something cleaner and clearer. Here's a mailer script I put together to test if a server could do mail. I use it every time I setup on a new server..

Code: Select all

<?php
$version = '0.1';
/*
	mail.php - test if the server can send mail
	@ (or + corz.org 2004 ->
*/


// host prefs
$corz_domain = $_SERVER['HTTP_HOST'];
$corz_self = $_SERVER['SCRIPT_NAME'];

// mail prefs..
$to_addy = "your-personal-address@wherever.net";
$now = date("l jS \o\f F Y h:i:s A");
$from_addy = "mailer@$corz_domain";
$email_subject = $corz_domain." mailer!";
$email_body = "this is a test mail from http://$corz_domain/$corz_self\r\nat $now";
$xmail_headers = "From: ".$from_addy."\r\n";
$xmail_headers .= "Reply-To: foobar@foothebar.com\r\n"; // optional
$xmail_headers .= "Organization: $corz_domain\r\n"; // ditto
$xmail_headers .= "\nX-Mailer: $corz_domain mailer! (php v".phpversion().")\r\n";

mail($to_addy, $email_subject, $email_body, $xmail_headers);
?>
Note the use of full "\r\n" DOS linebreaks at the end of each header (just in case!).

Note also the simple use of "From:" and ""Reply-To:" headers; both of which will enable you to hit reply in your mail client, and send straight back to the correct user. On some servers, you can't set the "From:", but you can always set the "Reply-To:", and that's all you need (and it will over-ride "From:" when you hit <reply>, too).

Recommendation: remove the ini_set code and hack in some proper mail headers, instead. If you are looking for a quick fix, you might get off with editing this one line to..

Code: Select all

	// Try to send the email
	if (mail($recipient, $subject, $mailMessage, "Reply-To: $from_address\r\n")) {
l*rz..

;o)
(or
nothing is foolproof to the sufficiently talented fool

Post Reply