php mail question

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
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

php mail question

Post by nomb »

Hey guys,

First off, this looks like a place I'm definately going to hang out... :D

My question I'm afraid is driving me to the point of no return. Here it is:

I set up postfix to send mail from php pages. I tested postfix like this:

telnet localhost 25
helo nomb
MAIL FROM: nomb@xxxxxxxxx.com
RCPT TO: nomb@xxxxxxxxx.net
DATA
This is a test.
.
quit

This works perfectly. However, when I try to use the mail function in php I get this:

Code: Select all

May 23 15:16:49 localhost postfix/pickup[12021]: B1C8243C15: uid=33 from=<www-data>
May 23 15:16:49 localhost postfix/cleanup[12028]: B1C8243C15: message-id=<20070523191649.B1C8243C15@localhost>
May 23 15:16:49 localhost postfix/qmgr[12022]: B1C8243C15: from=<www-data@localhost>, size=305, nrcpt=1 (queue active)
May 23 15:16:50 localhost postfix/smtp[12030]: B1C8243C15: to=<nomb@xxxxxxxxx.com>, relay=smtp.secureserver.net[64.202.166.12]:25, delay=0.68, delays=0.06/0.0$
May 23 15:16:50 localhost postfix/cleanup[12028]: 6F4F943C18: message-id=<20070523191650.6F4F943C18@localhost>
May 23 15:16:50 localhost postfix/qmgr[12022]: 6F4F943C18: from=<>, size=2100, nrcpt=1 (queue active)
May 23 15:16:50 localhost postfix/bounce[12031]: B1C8243C15: sender non-delivery notification: 6F4F943C18
May 23 15:16:50 localhost postfix/qmgr[12022]: B1C8243C15: removed
May 23 15:16:50 localhost postfix/local[12032]: 6F4F943C18: to=<www-data@localhost>, relay=local, delay=0.06, delays=0.02/0.02/0/0.03, dsn=2.0.0, status=sen$
May 23 15:16:50 localhost postfix/qmgr[12022]: 6F4F943C18: removed
For some reason php is using 'www-data@localhost' which makes anything sent via php not go out.
I have been trying for a few days to fix this and it is about to drive me crazy..

any help would be great.

thanks,

nomb
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mail() is a faff on....

Code: Select all

ini_set("sendmail_from", "your-actual@address.com");
mail($to, $subject, $message, "From: \"Your name\" <your-actual@address.com>", "-oi -fyour-actual@address.com");
Some servers will still ignore that since the MTA can override it.
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

Post by nomb »

I'm not sure what faff one means but does this mean that I will have to change any program which uses php? like my gallery2 or ampache? I have to go in and add that line?

thanks,
nomb

****************

I was actually able to send out a test mail with the following:

Code: Select all

<?php

$to = "nmcbride@xxxxxxx.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "nomb@xxxxxxxx.com";
$headers = "From: $from";

ini_set("sendmail_from", "nomb@xxxxxxxxxx.com");  
mail($to,$subject,$message, "From: \"xxxxxxxxxx.com\" <nomb@xxxxxxxxx.com>", "-oi -f nomb@xxxxxxxx.com");
echo "Mail Sent.";

?>
How do I make this work for every php page? Not just the ones I've scripted?
Last edited by nomb on Fri May 25, 2007 2:26 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'd use Swiftmailer if I were you. All your mail problems will be solved with that one application.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

nomb wrote:Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm not sure what faff one means but does this mean that I will have to change any program which uses php?  like my gallery2 or ampache?  I have to go in and add that line?

thanks,
nomb

****************

I was actually able to send out a test mail with the following:

Code: Select all

<?php

$to = "nmcbride@xxxxxxx.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "nomb@xxxxxxxx.com";
$headers = "From: $from";

ini_set("sendmail_from", "nomb@xxxxxxxxxx.com");  
mail($to,$subject,$message, "From: "xxxxxxxxxx.com" <nomb@xxxxxxxxx.com>", "-oi -f nomb@xxxxxxxx.com");
echo "Mail Sent.";

?>
How do I make this work for every php page? Not just the ones I've scripted?


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color][/quote]

I like Everah's suggstion, but then I'm biased 

To make that work for every page you're out of luck.  You can set the sendmail_from directly in php.ini, but that will vary from application to application so you inevitably have to set it in the script anyway.  The additional flags, although the can be set in php.ini, will again fall down when you need to use a different email address.

http://www.swiftmailer.org/

You will have to rewrite your mail sending code to use Swift but it's a fairly simple procedure.
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

Post by nomb »

what exactly was the problem? why does the solution work now? the code that you gave me, is someone replacing the 'www-data@localhost'? If that is the case, can't I globally set that?

also, does swift mailer integrate easily into already built applications? For example, if I download and install drupal which uses the php mail function, do I have to go in and recode the drupal sendmail function?
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

Post by nomb »

Does that output show anything?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

nomb wrote:what exactly was the problem? why does the solution work now? the code that you gave me, is someone replacing the 'www-data@localhost'? If that is the case, can't I globally set that?

also, does swift mailer integrate easily into already built applications? For example, if I download and install drupal which uses the php mail function, do I have to go in and recode the drupal sendmail function?
You can edit php.ini to set the sendmail_from address globally. But some MTAs require you to pass the "-f" flag to set the from address (return-path/sender). In that case you need to include code to handle that.

To add Swift Mailer to exisiting apps you would indeed have to recode that part of the app. There's no way around that unfortunately because they would have hard-coded mail() into it no doubt.
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

Post by nomb »

The 'sendmail_from' in php.ini says "for win32 only". Does that mean it wont work for me on linux?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

nomb wrote:The 'sendmail_from' in php.ini says "for win32 only". Does that mean it wont work for me on linux?
Actually, SMTP is for win32 only. sendmail_from is for Linux too.
nomb
Forum Newbie
Posts: 19
Joined: Thu May 24, 2007 3:24 pm

Post by nomb »

Alrighty, I was able to get it to send adding a -f nomb@xxxxxxxx.com to the 'add extra parameters' section in the ini file. I have another question now. It sends saying it is from 'www-data <nomb@xxxxxxxx.com>'. I am assuming it is doing this because www-data is the default user for apache. Without having to change everything from www-data is there a way to specify the user or someother way to change this?

Thanks,

nate
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

nomb wrote:Alrighty, I was able to get it to send adding a -f nomb@xxxxxxxx.com to the 'add extra parameters' section in the ini file. I have another question now. It sends saying it is from 'www-data <nomb@xxxxxxxx.com>'. I am assuming it is doing this because www-data is the default user for apache. Without having to change everything from www-data is there a way to specify the user or someother way to change this?

Thanks,

nate
You need to add a From: header too. Because mail() does not use SMTP on linux systems if causes the MTA (Sendmail, or Exim..) to be run with the userid of apache. Exim will by default, add headers in the format "user name <address>" when run in this mode unless there are already headers there. In case you haven't taken the hint yet, mail() never really works the same from server to server. There are mailing libraries out there which make porting code easier (see signature).
Exim man(8) wrote: -f <address>
This option sets the address of the envelope sender of a
locally-generated message (also known as the return path).
The option can normally be used only by a trusted user, but
untrusted_set_sender can be set to allow untrusted users to
use it.

Processes running as root or the Exim user are always
trusted. Other trusted users are defined by the trusted_users
or trusted_groups options. In the absence of -f, or if the
caller is not trusted, the sender of a local message is set
to the caller's login name at the default qualify domain.

There is one exception to the restriction on the use of -f:
an empty sender can be specified by any user, trusted or not,
to create a message that can never provoke a bounce. An empty
sender can be specified either as an empty string, or as a
pair of angle brackets with nothing between them, as in these
examples of shell commands:

exim -f '<>' user@domain
exim -f "" user@domain

In addition, the use of -f is not restricted when testing a
filter file with -bf or when testing or verifying addresses
using the -bt or -bv options.

Allowing untrusted users to change the sender address does
not of itself make it possible to send anonymous mail. Exim
still checks that the From: header refers to the local user,
and if it does not, it adds a Sender: header, though this can
be overridden by setting no_local_from_check.

White space between -f and the <address> is optional (that
is, they can be given as two arguments or one combined argu-
ment). The sender of a locally-generated message can also be
set (when permitted) by an initial "From " line in the mes-
sage - see the description of -bm above - but if -f is also
present, it overrides "From ".
Post Reply