mail

Simple Mail PHP Wrapper

ปัญหาใหญ่สุดของ Share Hosting คือ การส่ง email ของ function mail() แล้วไม่รู้ที่ว่า ก่อให้เกิดปัญหา spam ชาวบ้านไปทั่ว ชาวโลกคงเจอปัญหาเดียวกับเรา ตอนแรกคิดว่าจะต้องมี smtp authen ซะแล้ว แต่ความจริงไม่ต้องถึงกับขนาดนั้นก็ได้ เพราะ php มี feature นี้อยู่พอดี (ใช้ได้เฉพาะบน linux เท่านั้น)

Copy มาจาก iezzi.ch ง่ายๆเร็วๆ

สร้าง wrapper script ใน /usr/sbin/sendmail-wrapper-php

#!/bin/sh
logger -p mail.info sendmail-wrapper-php: site=${HTTP_HOST}, 
 client=${REMOTE_ADDR}, script=${SCRIPT_NAME}, 
 pwd=${PWD}, uid=${UID}, user=$(whoami)
/usr/sbin/sendmail -t -i $*

Check Permission

chown root /usr/sbin/sendmail-wrapper-php
chmod 755 /usr/sbin/sendmail-wrapper-php

แก้ php.ini

sendmail_path = /usr/sbin/sendmail-wrapper-php
auto_prepend_file = /var/www/common/php_set_envs.php

ใน php_set_envs.php

<?php
putenv("HTTP_HOST=".@$_SERVER["HTTP_HOST"]);
putenv("SCRIPT_NAME=".@$_SERVER["SCRIPT_NAME"]);
putenv("SCRIPT_FILENAME=".@$_SERVER["SCRIPT_FILENAME"]);
putenv("DOCUMENT_ROOT=".@$_SERVER["DOCUMENT_ROOT"]);
putenv("REMOTE_ADDR=".@$_SERVER["REMOTE_ADDR"]);
?>

ตัวอย่าง log

# grep sendmail-wrapper-php /var/log/mail.log
Dec 29 20:41:35 web logger: sendmail-wrapper-php: site=www.iezzi.ch, client=212.35.7.99, script=/test.php, pwd=/var/www/webXX/includes/wordpress, uid=4002, user=web2
Dec 29 20:42:52 web logger: sendmail-wrapper-php: site=webmail.onlime.ch, client=212.35.7.99, script=/index.php, pwd=/var/www/webYY/includes/roundcube, uid=4001, user=web1

PHPMailer

Sometimes, I would like to send multiple receiver email such as cc, bcc, to. But mail function on php cannot to do them. I can solve this problem by phpmailer.

This are the features of phpmailer

  • Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Redundant SMTP servers
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Support for 8bit, base64, binary, and quoted-printable encoding
  • Uses the same methods as the very popular AspEmail active server (COM) component
  • SMTP authentication
  • Word wrap
  • Address reset functions
  • HTML email
  • Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
  • Works on any platform
  • Flexible debugging
  • Custom mail headers
  • Multiple fs, string, and binary attachments (those from database, string, etc)
  • Embedded image support

It's the good idea that uses sendmail function of unix command to send them.

References : PHPMailer