8

I installed sendmail with PHP and apache on Ubuntu. When I try the following command-line

php -r "mail('test@gmail.com', 'test', 'test')"'

it successfully sends the email.

However, running the file "test_send_mail.php" with:

<?php
mail('test@gmail.com', 'test', 'test')
?>

doesn't send an email.

The unsuccessful attempt with the .php file generates the log entry:

`Jul  5 21:24:47 www sendmail[25603]: p661OlL7025603: from=www-data, size=106, class=0,

nrcpts=0, msgid=<201107060124.p661OlL7025603@www.server.com>, relay=www-data@localhost

The successful attempt with the command line generates the log entry:

    Jul  5 21:22:40 www sm-mta[25533]: p661MevV025533: from=<root@www.server.com>, 
size=352, class=0, nrcpts=1, msgid=<201107060122.p661Mecm025532@www.server.com>, 
proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

Does anyone have any idea what might be happening? Thank you for your help!

Tim
  • 81
  • 1
  • 1
  • 2
  • 1
    The PHP `mail()` function is actually fairly limited and quirky. If you're intending on sending email to or for users of your website, you should use a proper library, such as PHPMailer. This gives you things like attachment handling and much better headers support. – staticsan Jul 06 '11 at 03:11
  • Come on, mail can send anything, including attachments and so on. There is more about transport for sending email. In case of PHPMailer mostly it uses SMTP while mail function uses linux sendmail programm. And it seems like it's configured for root user, but not for www-data – Alex Kapustin Oct 27 '17 at 07:04

2 Answers2

1

PHP Has separate ini files depending on the environment:

  • cli/php.ini
  • cgi/php.ini
  • php.ini

Make sure you have made all the appropriate changes in all the files, cgi is usually used for Nginx and Lighttpd, but replicate the settings in all 3 to be sure.

Also you can run the phpinfo(); function to see what settings are actually being used.

also this may be the direct fix: PHP mail issue with www-data

Community
  • 1
  • 1
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
0

Your unsuccessful attempt is sending email as the web server's user. You probably don't want to do that. The key is to pass more parameters to mail() so as to override these sorts of defaults as it hands the email to your injector.

Look in the comments on the man page for mail() and there will be plenty of help to do that.

staticsan
  • 29,935
  • 4
  • 60
  • 73