articles

2010 年 8 月 9 日

PHP メールを送る

クラスにするまでもないのですが。
使い回しがききそうなので。

class SendMail{
	
	/*インスタンス作成時の引数は配列
	$option=array(
			"to"=>test@test.com, 
			"subject"=>"タイトル",
			"body"=>"本文",
			"from_name"=>"送信者",
			"from_email"=>"form@test.com"
	);
	*/
	
	function __construct($option){
		$this->encoding="utf-8";
		mb_language("ja");
		mb_internal_encoding($this->encoding);
		$this->option=$option;
	}
	
	function mimeHeader($str){
		return mb_encode_mimeheader (mb_convert_encoding($str,"ISO-2022-JP",$this->encoding));
	}
	
	function send(){
		extract($this->option);
		$f_from_name=$this->mimeHeader($from_name);
		$headers  = "MIME-Version: 1.0 \n From: {$f_from_name}<{$from_email}> \n";
		if($reply=="true") $headers .= "Reply-To: {$f_from_name} <{$from_email}> \n";
		$headers .= "Content-Type: text/plain;charset=ISO-2022-JP \n";
		
		$subject =$this->mimeHeader($subject);
		$body = mb_convert_encoding($body, "ISO-2022-JP",	$this->encoding);
		
		$sendmail_params  = "-f$from_email";
		$result = mail($to, $subject, $body, $headers, $sendmail_params);
		return $result;
	}
}