8

May be it's a dumb question, but I can't find the reason why php mail function doesn't work I have a nginx server on debian squeeze, I moved to it recently. I tried simple mail execution but it return false.

if(mail('test@email.com', 'test-subject', 'test-text-blablabla'))
   echo 'ok';
else
   echo 'bad';

What can i do with it?

Thanks.

my mail section of php.ini:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com 

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
Dima Deplov
  • 3,688
  • 7
  • 45
  • 77
  • 6
    Have you checked error log? – zerkms Feb 14 '13 at 01:11
  • 1
    can you send command line email? –  Feb 14 '13 at 01:12
  • @Dagon how can i do it? – Dima Deplov Feb 14 '13 at 13:46
  • @zerkms, I have a few virtual hostings for my sites, and I check a log file of site, where I tried to use mail function, and I didn't found any error about mail. May be you ask about another log file? – Dima Deplov Feb 14 '13 at 13:48
  • @flinth: what `mail` section of `phpunit` says? – zerkms Feb 14 '13 at 19:16
  • @zerkms if you ask about php.ini I add info to question, if not, please explain what you mean `phpunit`. – Dima Deplov Feb 14 '13 at 22:18
  • @flinth: oops, typo: `mail` section of `phpinfo()` – zerkms Feb 14 '13 at 22:55
  • @zerkms so, you can see it in the question body:) – Dima Deplov Feb 14 '13 at 22:56
  • @flinth: have you checked what you really have in `phpinfo()`? If so - do you have SMTP server set up at `localhost:25`? And, uhm, `For Win32 only.` :-S Debian isn't windows :-S You better comment them and set up using sendmail – zerkms Feb 14 '13 at 22:59
  • @zerkms I install sendmail to server, but hmm how I must use it? I must configure it somehow or mm this is the fog road for me. – Dima Deplov Feb 14 '13 at 23:03
  • @zerkms I add sendmail_path to that directive in php.ini, I take this path from phpinfo(), from the field with title Path to sendmail. `/usr/sbin/sendmail -t -i` The result is that page where the simplest mail function is work is http error 504 gateway time-out – Dima Deplov Feb 14 '13 at 23:12
  • @zerkms yes smtp is localhost and smtp_port is 25 (Are this directives for Windows only?). I'm sorry that bothers you. – Dima Deplov Feb 14 '13 at 23:19
  • I hope this comment will help somehow : https://github.com/docker-library/php/issues/135#issuecomment-277199026 – Charaf Feb 03 '17 at 09:13

1 Answers1

24

Okay, I made it. How I made it for debian squeeze with nginx server: (all commands I execute from root user)

First of all you need to install sendmail

apt-get install sendmail

next, you must configure this file that was easier than I thought

sendmailconfig 

okay, next step that I make was a php.ini configuration (I'm not a great admin, I'm a beginner, so I don't know is it necessary or not.)

I set

sendmail_path= /usr/sbin/sendmail -t -i

Okay, from this moment, theoretically, you can send email, but for my case it led to 504 http error gateway time-out. But as I found much later the email already came to email box. So, my test php file is:

<?php 
   mail('myWorkingEmail@example.com', 'test', 'you done that');
   echo 'ok'; // I use this to check that script is end the execution 
?>

That's pretty clear.

Next problem is 504 error. I go to the log files

nano /var/log/mail.log

and here i find this error (that not the only one error, but that one is responsible for 504 error):

sm-msp-queue[***]: My unqualified host name (myhostname) unknown; sleeping for retry

Then, to find how I can solve this trouble: http://forums.fedoraforum.org/archive/index.php/t-85365.html last comment on that page.

Or another words I made this:

nano /etc/hosts

and in that file I change the order of the hosts

127.0.0.1 my_ip localhost myhostname 

save, done. open your test php file, there is no any 504 error and emails is income to email you mention in mail function. As I say, I'm a novice, and that may not work for you, but it work for me anyhow. This is not the end configuration, of course. Hope you find it helpful.

Dima Deplov
  • 3,688
  • 7
  • 45
  • 77
  • if there is a man who well versed in this, I'll be very grateful if you can add some advice here. – Dima Deplov Feb 15 '13 at 12:14
  • 3
    I had to run `apt-get install sendmail-bin` and then, `apt-get install sendmail`. Then, tried to send an e-mail and it worked. I've made no change to php.ini. – Almino Melo Nov 25 '14 at 16:30
  • @AlminoMelo a bit late response, but I think those php.ini changes could affect spam filters of mail services in a good way. (Not sure). I know definitely that sometimes my emails comes to spam folder, and this because of imperfect email configuration plus DNS email settings troubles. – Dima Deplov Feb 18 '15 at 22:26