Help with setting the "From" when e-mailing a 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
benrud
Forum Newbie
Posts: 7
Joined: Tue Jan 13, 2009 11:57 am

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

Post 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
User avatar
blue-printf
Forum Newbie
Posts: 12
Joined: Mon Jan 12, 2009 9:27 am

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

Post 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";
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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");
benrud
Forum Newbie
Posts: 7
Joined: Tue Jan 13, 2009 11:57 am

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

Post 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
User avatar
blue-printf
Forum Newbie
Posts: 12
Joined: Mon Jan 12, 2009 9:27 am

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

Post 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.
benrud
Forum Newbie
Posts: 7
Joined: Tue Jan 13, 2009 11:57 am

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

Post by benrud »

Over my head but thanks for your help!

have a great day and thanks again.

Todd
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

Please make sure you're starting threads in the correct forum.

Moving to PHP Code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply