Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

MSMTP and PHP mail function fails with Apache

the mail() function

Moderators: macek, egami, gesf

MSMTP and PHP mail function fails with Apache

Postby ObiwanKenobi » Tue Jun 04, 2013 7:07 pm

Greetings to you all, this is my first post and my apologies for being a newbie to PHP.
This is the problem:

- running OK Ubuntu Linux ver 12.10 on an Amazon EC2 instance.
- Apache2 works perfectly, I can access web pages placed in /var/www from my remote browser
- PHP5 installed, can run a script such as info.php using info() function in /var/www , showing configuration
- installed, small mail agent msmtp works fine from command line, cron, etc, sends emails through Gmail.
- I wrote a small PHP script named hello.php, placed in same /var/www directory

here's the code for hello.php:

<?php

$to = 'somemail@gmail.com' ;
$subject = 'test PHP';
$body = 'Hello PHP !';

exec('msmtp somemail@gmail.com < ~/test.txt');
echo "Hello! Testing mail" ;

if (mail($to, $subject, $body))
{
echo("<p>Message successfully sent!</p>");
}
else
{
echo("<p>Message delivery failed...</p>");
}
?>

Running from comand line, php hello.php , work perfect, exec() and mail() functions.
Invoked from a remote browser, I can see the echo statement work but exec() and mail() don't work.
Result is : Message delivery failed... seen at the browser's screen.

The /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini contains in line #1055:

sendmail_path = '/usr/bin/msmtp -t -i'

Please, what am I doing wrong ? Help wil be very much appreciated.

Thank you an best wishes !
ObiwanKenobi
New php-forum User
New php-forum User
 
Posts: 4
Joined: Tue Jun 04, 2013 5:52 pm

Re: MSMTP and PHP mail function fails with Apache

Postby johnj » Wed Jun 05, 2013 8:43 pm

use this, $error = error_get_last(); echo $error; before the echo in the else part and see if there is any hidden errors.

Also, switch on error reporting at the top of your script.

And, then print $from, $to, $message and check if they are error free.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: MSMTP and PHP mail function fails with Apache

Postby ObiwanKenobi » Thu Jun 06, 2013 4:42 am

Hello johnj , thank you for your kind reply.

OK, here is the modified script:

Code: Select all
<?php

        error_reporting();
        $to = 'somemail@gmail.com' ;
        $subject = 'test PHP';
        $body = 'Hello PHP !';

        echo $to , " " , $subject , " " , $body , " " , "\n" ;
        exec('msmtp somemail@gmail.com < ~/test.txt');
        echo "Hello testing  function mail()" ;
        echo "\n" ;

        if (mail($to, $subject, $body))
        {
                echo("<p>Message successfully sent!</p>");
        }
        else
        {
                $error = error_get_last();
                echo "error report: " , $error , "\n" ;
                echo("<p>Message delivery failed...</p>");
        }
?>


The script runs OK in command line of my Ubuntu 12.10.
This is what the browser shows:

somemail@gmail.com test PHP Hello PHP ! Hello testing function mail1() error report: Array
Message delivery failed...


I see this Array error ...
ObiwanKenobi
New php-forum User
New php-forum User
 
Posts: 4
Joined: Tue Jun 04, 2013 5:52 pm

Re: MSMTP and PHP mail function fails with Apache

Postby johnj » Tue Jun 25, 2013 9:48 pm

What is this function mail()? Also - do print_r($error);
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: MSMTP and PHP mail function fails with Apache

Postby ObiwanKenobi » Wed Jun 26, 2013 11:00 am

OK, here is the result when running from a browser, with the print_r($error) statement included:

    microlatina@gmail.com test PHP Hello PHP ! Hello testing function mail() error report: Array
    Message delivery failed...

    Array ( [type] => 2 [message] => mail(/etc/php5/apache2/mail.log): failed to open stream: Permission denied [file] => /var/www/hola.php [line] => 17 )


Seems that there is apermission issue with Apache :o , isn't it ?

Code: Select all
<?php

        error_reporting();
        $to = 'microlatina@gmail.com' ;
        $subject = 'test PHP';
        $body = 'Hello PHP !';

        echo $to , " " , $subject , " " , $body , " " , "\n" ;

        //funcionan ambas versiones

        exec('msmtp microlatina@gmail.com < ~/test.txt');

        echo "Hello testing  function mail()" ;
        echo "\n" ;

        if (mail($to, $subject, $body))
        {
                echo("<p>Message successfully sent!</p>");
        }
        else
        {
                $error = error_get_last();
                echo "error report: " , $error , "\n" ;
                echo("<p>Message delivery failed...</p>");
                print_r($error);
        }


?>


The script runs perfectly well when invoked from command line of my Ubuntu Linux, sending 2 email messages OK, one using exec() and the other using mail()
BTW, the mail() function is the one that call my msmtp command in Linux. As I said, it works fine from command prompt.

The file hola.php has full 777 permissions at this time, however the owner is root while the real user is ubuntu .

Willing to hear from you about this issue and thank you very much !
ObiwanKenobi
New php-forum User
New php-forum User
 
Posts: 4
Joined: Tue Jun 04, 2013 5:52 pm

Re: MSMTP and PHP mail function fails with Apache

Postby ObiwanKenobi » Wed Jun 26, 2013 11:19 am

johnj , changing owner and group to 'mail' , the mail.log file now works and contains the following:


Also, now the browser shows the following:

    microlatina@gmail.com test PHP Hello PHP ! Hello testing function mail() error report:
    Message delivery failed...

My experience her is so poor ... sorry for that.
I dont know how to fix it'.
ObiwanKenobi
New php-forum User
New php-forum User
 
Posts: 4
Joined: Tue Jun 04, 2013 5:52 pm


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.