0

I have correctly set the postfix server according to PHP+Ubuntu Send email using gmail form localhost, so that this msmtp sends the email via:

$ echo -e "Subject: Test Mail\r\n\r\nThis is my first test email." |msmtp --debug --from=default -t skrifix@seznam.cz

/etc/msmtprc

account default
host smtp.gmail.com
port 587
auth on
user autak987@gmail.com
password *********
from autak987@gmail.com
logfile /var/log/msmtp.log

`$ls -l /etc/msmtprc

-rw-rw-r-- 1 www-data www-data 377 Aug 30 12:54 /etc/msmtprc

which works correctly (the defaul account is set in /etc/msmtprc. Then I have set the sendmail_path in /etc/php/7.3/apache2/php.ini:

sendmail_path = /usr/bin/msmtp -t

So I tried to execute an example from php manual - mail :

<?php
$to = 'skrifix@seznam.cz';
$sub = 'the subject';
$mes = 'hello';
$header = 'From: autak987@gmail.com\r\n'; //not even needed - the default account is set

mail($to, $sub, $mes, $header);

But I can't find the email in the address skrifix@seznam.cz. What I did wrong? it should be set according to that answer.

Edit: tried to trigger the execution with root according to this Sending mail through terminal using msmtp works fine, but doesn't work with php mail(), yet no success.

Edit2: It got even more stranger. From 15 test sending (15*execution of php script), 2 mails really arived. But they were from "unknown-sender", where the sender should be default account specified in /etc/msmtprc and even specified in the php in header From:.... I am really confused, what is going on here.

milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • you are sending email in VPS or shared hosting? Some hosting providers intentionally block usage of the mail() function for security – Fatih Şennik Aug 30 '20 at 11:37
  • Using apache on debian machine and sending from localhost (that's why I tried to configure it according to the first link) – milanHrabos Aug 30 '20 at 11:42
  • 1) Gmail requires TLS. Add `tls on` to `/etc/msmtprc`. 2) Your first example will not work. The command line option `--from=default` sets the from address to `default` and does not select the profile `default`. Use the option `--account=default`. 3) Review the PHP log files for error messages from the `send()` call. 4) You need to figure out if Gmail is accepting your emails (review the logs) or rejecting them. – John Hanley Aug 31 '20 at 03:57

0 Answers0