{"id":597,"date":"2009-09-17T16:51:02","date_gmt":"2009-09-17T09:51:02","guid":{"rendered":"http:\/\/adityo.blog.binusian.org\/?p=597"},"modified":"2009-09-17T16:51:02","modified_gmt":"2009-09-17T09:51:02","slug":"how-to-log-emails-that-was-sent-with-phps-mail-function","status":"publish","type":"post","link":"https:\/\/adityo.blog.binusian.org\/?p=597","title":{"rendered":"How To Log Emails That Was Sent With PHP&#8217;s mail() Function"},"content":{"rendered":"<p>url referer : &#8211;\u00a0<a href=\"http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam\">http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam<\/a><\/p>\n<p>After sometimes it occur to me that me how to get log of mail that was sent from formmail on my web because i always wonderring who sent the email is it spam or not, then i found this artickel\u00a0<a href=\"http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam\">http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam<\/a> \ud83d\ude42<\/p>\n<p>Okay let&#8217;s start the installation<\/p>\n<h3>1. Installing the Log \u00a0script<\/h3>\n<p>Create new file\u00a0<span class=\"system\">\/usr\/local\/bin\/phpsendmail<\/span>&#8230;<\/p>\n<p class=\"command\">vi \/usr\/local\/bin\/phpsendmail<\/p>\n<p class=\"command\">\n<p class=\"command\">Then paste this<\/p>\n<p class=\"command\">#!\/usr\/bin\/php<\/p>\n<p class=\"command\">&lt;?php<\/p>\n<p class=\"command\">\n<p class=\"command\">$sendmail_bin = &#8216;\/usr\/sbin\/sendmail.postfix&#8217;; \/\/ i use postfix and postfix sendmail bin was located on\u00a0\/usr\/sbin\/sendmail.postfix , it depend on your mail system<\/p>\n<p class=\"command\">$logfile = &#8216;\/var\/log\/mail.form&#8217;;<\/p>\n<p class=\"command\">\n<p class=\"command\">\/\/* Get the email content<\/p>\n<p class=\"command\">$logline = &#8221;;<\/p>\n<p class=\"command\">while ($line = fgets(STDIN)) {<\/p>\n<p class=\"command\">if(stristr($line,&#8217;to:&#8217;) || stristr($line,&#8217;from:&#8217;)) $logline .= trim($line).&#8217; &#8216;;<\/p>\n<p class=\"command\">$mail .= $line;<\/p>\n<p class=\"command\">}<\/p>\n<p class=\"command\">\n<p class=\"command\">\/\/* compose the sendmail command<\/p>\n<p class=\"command\">$command = &#8216;echo &#8220;&#8216;.$mail.'&#8221; | &#8216;.$sendmail_bin.&#8217; -t&#8217;;<\/p>\n<p class=\"command\">for ($i = 1; $i &lt; $_SERVER[&#8216;argc&#8217;]; $i++) {<\/p>\n<p class=\"command\">$command .= $_SERVER[&#8216;argv&#8217;][$i].&#8217; &#8216;;<\/p>\n<p class=\"command\">}<\/p>\n<p class=\"command\">\n<p class=\"command\">\/\/* rotate log if it gets too big<\/p>\n<p class=\"command\">if(is_file($logfile) &amp;&amp; filesize($logfile) &gt; 10000000) {<\/p>\n<p class=\"command\">if(is_file($logfile.&#8217;.old&#8217;)) unlink($logfile.&#8217;.old&#8217;);<\/p>\n<p class=\"command\">exec(&#8216;cp -pf &#8216;.$logfile.&#8217; &#8216;.$logfile.&#8217;.old&#8217;);<\/p>\n<p class=\"command\">exec(&#8216;cat \/dev\/null &gt; &#8216;.$logfile);<\/p>\n<p class=\"command\">}<\/p>\n<p class=\"command\">\n<p class=\"command\">\/\/* Write the log<\/p>\n<p class=\"command\">system(&#8216;echo &#8220;&#8216;.date(&#8220;Y-m-d H:i:s&#8221;).&#8217; &#8216;.$_ENV[&#8216;PWD&#8217;].&#8217; &#8216;.$logline.'&#8221; &gt;&gt; &#8216;.$logfile);<\/p>\n<p class=\"command\">\n<p class=\"command\">\/\/* Execute the command<\/p>\n<p class=\"command\">return shell_exec($command);<\/p>\n<p class=\"command\">?&gt;<\/p>\n<p class=\"command\">Now make the script executable&#8230;<\/p>\n<p class=\"command\">chmod +x \/usr\/local\/bin\/phpsendmail<\/p>\n<p>&#8230; and create the logfile and make it writable:<\/p>\n<p class=\"command\">touch \/var\/log\/mail.form<br \/>\nchmod 777 \/var\/log\/mail.form<\/p>\n<p class=\"command\">\n<h3>2. Modifying the main.cf<\/h3>\n<p>Again it depend on your mail system , on my ssystem i use postfix to send the email from my formmail, in other system usually the email send from sendmail or from the php itself in that case you need to edit php.ini. In my system i need to modify postfix main.cf<\/p>\n<p>go to your main.cf on \/etc\/postfix\/main.cf and changes this<\/p>\n<p>vi\u00a0\/etc\/postfix\/main.cf<\/p>\n<p>sendmail_path = \/usr\/local\/bin\/phpsendmail<\/p>\n<p>then relaod your postfix<\/p>\n<p># \/etc\/init.d\/postfix reload<\/p>\n<h3>3 Test the setup<\/h3>\n<p>To test this setup, create a new php file with the name\u00a0<span class=\"system\">mailtest.php<\/span> in one of your websites with the content:<\/p>\n<pre>&lt;?php\r\nmail('yourname@yourdomain.com','This is a test message subject','This is a test message body');\r\necho 'Mail sent.'; \r\n?&gt;<\/pre>\n<pre>to see the log  you can use<\/pre>\n<pre># tail \/var\/log\/mail.form -f<\/pre>\n<pre><span><span>2009-09-17 15:49:30 \/home\/test\/public_html To: test@yahoo.com From: test &lt;test@yahoo.com&gt; \r\n2009-09-17 16:12:01 \/home\/test\/public_html To: test2@bi.edu From: test &lt;test2@bi.edu&gt; <\/span><\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>url referer : &#8211;\u00a0http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam After sometimes it occur to me that me how to get log of mail that was sent from formmail on my web because i always wonderring who sent the email is it spam or not, then i found this artickel\u00a0http:\/\/www.howtoforge.com\/how-to-log-emails-sent-with-phps-mail-function-to-detect-form-spam \ud83d\ude42 Okay let&#8217;s start the installation 1. Installing the Log \u00a0script [&hellip;]<\/p>\n","protected":false},"author":386,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-597","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/users\/386"}],"replies":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=597"}],"version-history":[{"count":3,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/597\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}