sean
sean

Reputation: 5

How can I add a From e-mail address to a PHP form?

My e-mail processor is working... but how do I get the From e-mail address to show up instead of "myhost"? I'm getting [email protected]. So, when someone fills in the form, I want his e-mail to be the reply address.

<?php
if(!$_POST) exit;

$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','telephone','company','message');
    $required = array('name','email','telephone','message');

    $your_email = "myemail@somehost";
    $email_subject = "New Message: ".$_POST['subject'];
    $email_content = "new message:\n";

    foreach($values as $key => $value){
        if(in_array($value,$required)){
            if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
    }
}

if(@mail($your_email,$email_subject,$email_content)) {
    echo 'Message sent!'; 
} else {
    echo 'ERROR!';
}
}
?>

Upvotes: 0

Views: 278

Answers (4)

david.wosnitza
david.wosnitza

Reputation: 763

okay... i'd say strip the whole $values = array.... line and replace is with

$values = array();
foreach($_POST as $k => $v) if(strtolower($k) != 'submit') $values[$k] = htmlspecialchars($v);

that add's ALL your POST data to the $values array and your content generator should look like this

foreach($values as $key => $value){
    if(in_array($key,$required) && trim($value)==''){
        echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; 
    }
    $email_content .= $key.': '.$value."\n"; // makes more sense
}

hope i got your question right oO

Upvotes: 1

Newton Calegari
Newton Calegari

Reputation: 41

By default, the email sent by PHP [ mail() function ] uses as sender your server. To change the sender, you must modify the header of email. You can do it this way:

$emailHeader = "From: [email protected]" . "\r\n" . "Reply-To: [email protected]" . "\r\n";
// add the header argument to mail function
mail($your_email,$email_subject,$email_content,$emailHeader);

Note that we added a fourth argument to the mail function.

Upvotes: 1

david.wosnitza
david.wosnitza

Reputation: 763

Hey. Just by the way: from what i see here,

        if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }

does not make sense. your key's will always be: 0 to count($values)... you don't have an associative array. or is this $values array just for testing purposes?

Upvotes: 0

captbrogers
captbrogers

Reputation: 494

Add this to your file:

$headers = 'From: ' . $your_email . "\r\n" .
'Reply-To: ' . $your_email . "\r\n" .  
'X-Mailer: PHP/' . phpversion();

Then when you call the mail command use this:

mail($your_email,$email_subject,$email_content,$headers);

Upvotes: 1

Related Questions