By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

How to send email with from data in headers

P: 29
The following email code is sending the values but is not sending all the values from the form.
Expand|Select|Wrap|Line Numbers
  1. $subject = $_POST["subject"];
  2. $message = $_POST["message"];
  3. $name = $_POST["name"];
  4. $from = $_POST["from"];
  5. mail('xxxx@hotmail.co.uk', 'My Subject', $message, $from);
I tried to just insert the values ($name $subject) on that line but on the email it will only post two values.
May 20 '10 #1

✓ answered by niheel

The call to the mail function in your code is wrong:

Use:
Expand|Select|Wrap|Line Numbers
  1. $to = "xxxx@hotmail.co.uk";
  2. $subject = $_POST["subject"];
  3. $message = $_POST["message"];
  4. $name = $_POST["name"];
  5. $from = $_POST["from"];
  6. mail($to,$subject,$message);
  7.  
If you want to send FROM you'll have to manipulate the headers and you can use code like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $to = "xxxx@hotmail.co.uk";
  3. $subject = $_POST["subject"];
  4. $message = $_POST["message"];
  5. $name = $_POST["name"];
  6. $from = $_POST["from"];
  7. $headers = "From: $name <$from>" . "\r\n";
  8.  
  9. mail($to,$subject,$message,$headers);
  10. ?>
  11.  
As Atli suggested use http://php.net/manual/en/function.mail.php as a guide to help you create the mail output you want.

Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Look at how the function is used in the manual entry. Study that page; everything is explained there. There are even a number of examples that show exactly how to use the function for various purposes.

If that doesn't get you any closer, you may need to study how email actually works; what exactly makes up an email. That may help you understand the terms used in the manual, and how to properly use the input parameters.

I could explain all this here, but it seems rather redundant. All this information is widely available online, only a Google search away.
May 21 '10 #2

P: 29
I looked at it and it seems that my thick head can't get way around it. With email you need the form, to, subject, and body. I don't understand the extra parameters for the header.
May 21 '10 #3

Expert Mod 100+
P: 2,330
The call to the mail function in your code is wrong:

Use:
Expand|Select|Wrap|Line Numbers
  1. $to = "xxxx@hotmail.co.uk";
  2. $subject = $_POST["subject"];
  3. $message = $_POST["message"];
  4. $name = $_POST["name"];
  5. $from = $_POST["from"];
  6. mail($to,$subject,$message);
  7.  
If you want to send FROM you'll have to manipulate the headers and you can use code like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $to = "xxxx@hotmail.co.uk";
  3. $subject = $_POST["subject"];
  4. $message = $_POST["message"];
  5. $name = $_POST["name"];
  6. $from = $_POST["from"];
  7. $headers = "From: $name <$from>" . "\r\n";
  8.  
  9. mail($to,$subject,$message,$headers);
  10. ?>
  11.  
As Atli suggested use http://php.net/manual/en/function.mail.php as a guide to help you create the mail output you want.
May 21 '10 #4

P: 29
Thank you very much. It works beautifully now I will try my best to understand better the email concept form the php manual.
May 21 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.