2006年11月7日火曜日

[PHP] PHPでHTMLメールを送信する

Perl なら MIME::Entity で作れるけど、PHPの場合どう書くんだろう?
と思い、まっちゃんに教えてもらったのでメモ。

メール関数(Mail)
@set_time_limit(0);
require_once 'smtp_mail.php';
$to = "expertphp@yahoo.com";
$from = "from@myaccount.com";
$subject = "Subject here";
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=iso-8859-1\r\n".
"From: \"My Name\" <".$from.">\r\n".
"To: \"Client\" <".$to.">\r\n".
"Date: ".date("r")."\r\n".
"Subject: ".$subject."\r\n";

$message = "ここにHTMLを書く";
$response = smtp_mail($to, $subject, $message, $from, $headers);
if($response[0]) echo "The message has been sent !
\n".$response[1];
else echo "The message can not been sent !
\n".$response[1];
など。
PHPMailer
このクラス使うと
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "jswan"; // SMTP username
$mail->Password = "secret"; // SMTP password
$mail->From = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com"); // optional name
$mail->AddReplyTo("info@site.com","Information");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Here is the subject";
$mail->Body = "ここにHTMLを書く";
$mail->AltBody = "This is the text-only body";
if(!$mail->Send())
{
echo "Message was not sent

";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";


と書けるらしい。

簡単だー☆

0 件のコメント: