Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail(): setting from header

Status
Not open for further replies.

chessbot

Programmer
Mar 14, 2004
1,524
US
I just wrote an emailing page and the From: part of the header is not being evaluated, meaning that the from line of the email is the name of the website. Can anyone tell me why this isn't working?
Relevant code:
Code:
<?php

function onload()
{

$addrs = $_POST['addrs'];
$names = $_POST['names'];

$fromaddr = $_POST['fromaddr'];
$fromname = $_POST['fromname'];

$subject = $_POST['subject'];
$body = $_POST['body'];

if (!(isset($addrs) && isset($names) && isset($fromaddr) && isset($fromname) && isset($subject) && isset($body)))
  return;

$toheaders = "To: ";
for ($i=0; $i<count($addrs); $i++)
{
  if ($addrs[$i] == "")
    break;
  $toheaders .= $names[$i] . " <" .$addrs[$i] . ">";
  if ($addrs[$i+1] != "")
    $toheaders .= ", ";
}

$toheader .= "\r\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= $toheaders . "\r\n"; 
$headers .= "From: $fromname <{$fromaddr}>\r\n";

if (!mail($tostring, $subject, $body, $headers))
  echo "There was an error in the email.";
else
  echo "Message sent!";
}


onload();

?>

Incidentally, the reason I put everything in a function was so I could exit without having the page elements fail to render. Also, the From: header was working before I tweaked some things (not sure what though...).

Thanks in advance.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
On what OS are you running this script?

If it's a unix-like OS, what mail server is that OS running?


My first guess is that it's sendmail on a unix-like OS. If so, check out the additional parameters documentation on the online manual page for mail(). Search for "sendmail" on the page.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Finding out...

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
I'm using a hosting site that says that they're running it on Linux (running Apache servers).

It doesn't explicitly give the mail server, but their are references to a "path to sendmail", which leads me to believe you are correct.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Thanks! Unfortunately, it doesn't work.
The code I am using:
Code:
// ...
$additional = "-f{$fromaddr}";
// ...
mail($to, $subject, $body, $headers, $additional);

Yet again, the name does not register.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Well, I see someone else has run across the same problem I did. In my case I was half asleep setting up the mail service.

I would:

1. Email your ISP to find out what *exactly* is being used for their mail extensions to php. For example, ssmtp emulates sendmail and by default in its configuration, From: header overrides are OFF. If it is some sort of sendmail emulation they may not have allowed overrides for some reason or other.

2. Find out if it is emulated, if they can enable the From override.

As I run my own server it was easy enough to fix. Hope this helps.
 
I emailed the hosting service. They will respond, in theory, within one business day. I will be back to post results or more questions.

Thanks, everyone!

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top