Page 1 of 1

Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 12:04 pm
by benrud
Hello Everyone,

This is my first post on this board and I'm still very new to PHP.

I need a little help setting the "From" when sending a form via php.

Below is what I've done to send the form.

Code: Select all

<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$comment = $_POST['comment'];
 
 
$to = "ghswebdesign@gmail.com";
$subject = $reason;
$body = "Contact Form \n $last_name \n $phone \n $reason \n $email \n $reason \n $comment \n";
$headers = "Contact Form";
 
 
mail($to, $subject, $body, $headers);
?>
The problem I'm having is when I receive the form via email the email is "From" World Wide Web Server<www@localhost.localhost>. I want to set it so the e-mail is coming from the $email received in the form.

Any ideas?

Thanks for you help!
Todd

High School Web Design Teacher
http://www.mrbenrud.com

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 12:12 pm
by blue-printf
you have to set more headers.

Code: Select all

 
$headers .= "From: <example@example.com>n";
$headers .= "X-Sender: <example@example.com>n";
$headers .= "X-Mailer: PHPn";
$headers .= "X-Priority: 3n"; //1 = Spoed bericht, 3 = Normaal bericht
$headers .= "Return-Path: <example@example.com>n";
 
change the strings to use the given email:

Code: Select all

 
$headers .= "From: <$emall>n";
$headers .= "X-Sender: <$emall>n";
$headers .= "X-Mailer: PHPn";
$headers .= "X-Priority: 3n"; //1 = Spoed bericht, 3 = Normaal bericht
$headers .= "Return-Path: <$emall>n";
 

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 12:31 pm
by Apollo
benrud wrote:$headers = "Contact Form";
This is wrong, you cannot just put random stuff in email headers.


Just a From: header is enough, like this: (ReturnPath etc will be the same as From by default, and X-mailer and X-priority are pretty much useless)

Code: Select all

$headers = "From: $email\n";
But most importantly, add the -f switch to instruct your mail server to use the right address:

Code: Select all

mail($to, $subject, $body, $headers, "-f$email");

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 12:38 pm
by benrud
Apollo,

Thank you for your help! I made the corrections and everything is working now. I do have one question for you though.

What is "-f$email" in the mail() function?

Thanks,
Todd

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 2:05 pm
by blue-printf
http://nl3.php.net/manual/en/function.mail.php

additional_parameters (optional)

The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 2:37 pm
by benrud
Over my head but thanks for your help!

have a great day and thanks again.

Todd

Re: Help with setting the "From" when e-mailing a form

Posted: Tue Jan 13, 2009 2:47 pm
by pickle
Please make sure you're starting threads in the correct forum.

Moving to PHP Code.