php - sendmail

 

 

 


控制 mail from

 

# php-fpm / mod_php

# Default: "/usr/sbin/sendmail -t -i"
php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -fapache2@server"

 * 在 .htaccess 不能使用 php_admin_value

# php code

$returnPath = "-f USER@DOMAIN";      //-f will do the job
mail($to, $subject, $body, $headers, $returnPath)

 


限制某 vhost 出唔到 mail

 

A) 如果不 log 出信情況, 那可以直接在 vhost 的 config file 內加

php_admin_value sendmail_path "/dev/null"

B) log 出信的時間及內容

建立 script /home/dummy/logsendmail.sh:

#!/bin/bash

logFile=/tmp/sendmail_record.txt

# For log send out time
echo "last:" `date` >> $logFile

# For log mail content
while IFS= read -r line
do
    echo "Get stdin: $line" >> $logFile
done

chmod 755 /home/dummy/logsendmail.sh

在 vhost 的 config file 內加

php_admin_value sendmail_path "/home/dummy/logsendmail.sh"

* "php_admin_value disable_functions "mail"" 係唔 work 的

Server-wide disable:

Disable sendmail api:

修改 php.ini:

disabled_functions = mail

 


Testing script

 

code.php

<?php
 $to = "[email protected]";
 $from = "[email protected]";
 $subject = "Hi!";
 $body = "Hi,\n\nThis is test msg.";
 $headers = "From: $from\r\n" .
     "X-Mailer: php-test-mail";
 if (mail($to, $subject, $body, $headers)) {
   echo("<p>Message sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
?>

解釋

#php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -fuser@domain"

(default: "sendmail -t -i")

mail function:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

$message

Each line should be separated with a CRLF (\r\n). Lines should not be larger than 70 characters.

Return Values

    TRUE if the mail was successfully accepted for delivery

... webserver postfix/pickup[32152]: 1AFAB8492C: uid=33 from=<www-data>
... webserver postfix/cleanup[32667]: 1AFAB8492C: message-id=<[email protected]>
... webserver postfix/qmgr[2650]: 1AFAB8492C: from=<[email protected]>, size=336, nrcpt=1 (queue active)

php.ini 的設定

# For Win32 only

sendmail_from    no value    no value

# For Unix only.

sendmail_path    /usr/sbin/sendmail -t -i

 

Creative Commons license icon Creative Commons license icon