Help us understand the problem. What is going on with this article?

PHP開発環境用 メール送信内容をログに残す

More than 3 years have passed since last update.

開発環境用にメールの送信内容をログに残すようにしたので、備忘録を兼ねて

環境

OS:centOS6.7 (vagrant)
Apache:2.4
PHP5.5(php-mbstringもインストール)

メール送信用のスクリプトを用意

ここを参考に一部変更
/usr/local/bin/sendMaiDevに保存

#!/usr/bin/php

<?php
$logfile = '/var/tmp/mail/mail.log-'.date('Y-m-d'); //ログファイル名 1日ごとにファイルを分ける
shell_exec('touch '.$logfile);                      //ファイルが存在しないときのために、touchしておく
//* Get the email content
$log_output = "****" . date('Y-m-d H:i:s') . "****" . PHP_EOL;
$handle = fopen('php://stdin', 'r');

while(!feof($handle))
{
    $buffer = trim(fgets($handle));
    $log_output .= $buffer . PHP_EOL;
}
//* Write the log
//そのまま出力すると文字化けがひどいのでコンバート
//私の環境ではISO-2022-JPになってましたがご自身の環境に合わせて変えてください
file_put_contents($logfile, mb_convert_encoding($log_output, 'UTF-8', 'ISO-2022-JP'), FILE_APPEND);
?>

忘れずに実行権限を付与しましょう

sudo chmod a+x /usr/local/bin/sendMaiDev

php.ini修正

php.iniの747行め付近、sendmail_pathを先ほど作成したスクリプトのパスにします

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/local/bin/sendMaiDev

; 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 =

修正したらApache再起動

sudo service httpd restart

以上でPHPから送信したメールがテキストファイルに出力されます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした