php5 mail() function sendmail error

2019-02-24 23:08发布

问题:

I'm trying to use php's mail() function but keep getting an error. I've installed sendmail via sudo apt-get install sendmail, edited my /etc/php5/cli/php.ini file adding the following text to these lines:

 sendmail_path = /usr/sbin/sendmail -t
 sendmail_from = uslonsweb003.ALU@no-reply.com

I then restarted my webserver and used this command for test:

 :~$ php -r "mail('sadmicrowave@gmail.com', 'test subject', 'test body message');"

but I get the following error EVERYTIME!!!:

 sh: -t: not found

This is odd because I have tried the sendmail_path with -t and without -t but I keep getting the same error. What am I doing wrong?

UPDATE! this is what my phpinfo() shows: (I added -t back but the command isn't working with or without it).

Another UPDATE -

I commented out the sendmail_path and sendmail_from lines to start from scratch expected the mail() function to complain that php doesn't know what it is but instead I get the EXACT same error as before (even without the two lines entirely!!). This leads me to believe that it doesn't have to do with the sendmail program or mail() function at all...

回答1:

I don't know, but you might try sendmail_path = /usr/sbin/sendmail -t -i. It's set so on webhosting which I use. Otherwise, you might want to check if phpinfo() contains correct settings for sendmail.



回答2:

I take the error to indicate that you are missing the information that -t would be looking for, 'To:' , 'CC:' , or 'BCC:'

Try adding some extra info to your mail command line and see if that works:

php -r "mail('sadmicrowave@gmail.com', 'test subject', 'test body message', 'To: Receiver <receiver@email.com>');"


回答3:

Look at the following sections in your phpinfo() output to make sure you're editing the correct file:

  • Loaded Configuration File
  • Additional .ini files parsed

If the file you edited is not listed in one of those sections, the changes will have no effect.



回答4:

I solved my issue by utilising the SwiftMailer module where I can specify a mailserver to relay through. I used my company mailserver as the server property and continued specifying the options as follows:

require_once('/var/www/global/swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance( 'mailout.usa.mycompany.com', 25 );
$mailer = Swift_Mailer::newInstance( $transport );
$message = Swift_Message::newInstance( 'Suggestion Status Update' )
    ->setFrom( array( 'uslonsweb003@no-reply.com' => 'SuggestionBox' ) )
    ->setTo( array( $pEmail => $username ) )
    ->setBody( $body, 'text/html' )
    ;
$result = $mailer->send( $message );