入口
 HTML/PHP 切替
 環境変数
 配列
 文字コード
 session
買物かご
 index.php
 sub.phl
 引数受取
制御構造
 if
  文字比較
  空文字列
 foreach
文字取扱
 空白文字
  trim()
mail
 mime mail
PostgreSQL
 PostgreSQL 用の関数
 関数名の変更
 単純な表を作成
 記述例
Books

PHP4 programming Apache + PHP4 + PostgreSQL | 日本語説明書
Last Update: "2005/01/05 01:04:29 makoto"

mail

mime mail

Mime 形式のメールを送るコードを手軽に書くには、 PEAR に含まれる mime.php を使う方法と、mimePart.php を使う方法が あります。その mimePart.php の中を読むと、これの方が、こまかい制御が出来 るという説明があります。それを使って見ます。試験用ですから、 送信内容などは全て固定です。

この例では、 本文と添付書類の中には日本語が書けますが、 From: Subject: については日本語は書けません。 この内容は EUC-JAPAN で保存します。 また php を作る時 と /usr/pkg/etc/php.ini に mbstring の設定 がされているものとしています。 赤字の部分は、受取人の指定ですので、 自分向に送られるように必ず設定(変更)して下さい。逆に言えば、それ以外は そのままでも大丈夫です。

<!-- -*- PHP -*- -->
<!-- $Id: 5500.mimemail.html.ja,v 1.1.1.1 2008/03/12 10:52:30 makoto Exp $ -->
<html>
<head>
<title>メールの送信</title>
</head>
<body>
<?php  //  save this file with EUC-JAPAN ;;;
require_once 'Mail.php';
require_once 'Mail/mimePart.php'; 
// this will read "/usr/pkg/lib/php/Mail/mimePart.php" in my environment

// (1)
$params['content_type'] = 'multipart/mixed';
$email = new Mail_mimePart('', $params);

// (2)
// Here we add a text part to the multipart we have
// already. Assume $body contains plain text.
$params['content_type'] = 'text/plain; charset=ISO-2022-JP';   // default, not really necessay
$params['encoding']     = '7bit';         // default, not really necessay
$body = 'test mail ここに日本語';         // mail body
mb_convert_variables ( 'JIS', 'EUC-JP', $body);  // EUC-JP -> JIS 変換
$text = $email->addSubPart($body, $params);

// (3)
// Now add an attachment. Assume $attach is
// the contents of the attachment
$body = "
test test 1 2 3 5
1 2 3 6 test test 
日本語
";                                       // actually the content of attachment

mb_convert_variables ( 'SJIS', 'EUC-JP', $body);  // EUC-JP -> SJIS 変換
$params['content_type'] = 'application/zip';
$params['encoding']     = 'base64';
$params['disposition']  = 'attachment';
$params['dfilename']    = 'teststuff';
// $params['charset']      = '' ;           // override the value set above
$attach =& $email->addSubPart($body, $params);

// Now build the email. Note that the encode
// function returns an associative array containing two
// elements, body and headers. You will need to add extra
// headers, (eg. Mime-Version) before sending.

// (4)
$email ->_headers['Subject'] = 'お問合せ'; // NG, will not be encoded
$encoded = $email->encode();
$email ->_headers['Mime-Version'] = '1.0';  // Cannot use a scalar value as an array

$SMTP = 'mail.example.com';

$parameter["host"] = $SMTP;
$parameter["port"] = 25;
$parameter["auth"] = FALSE;
$parameter["username"] = NULL;
$parameter["password"] = NULL;
$Mail =& Mail::factory("smtp", $parameter);
if(PEAR::isError($Mail))
	exit("SMTPサーバーに接続できません。");

// (5)
$body    = $encoded['body'];

$recipients         = 'hoge@example.com';
$headers            =  $encoded['headers'];

$headers['From']    = '"PHP Example" <hoge@example.com>';
$headers['To']      = 'Makoto Fujiwara <hoge@example.com>';
$headers['Subject'] = 'test';

if($Mail->send($recipients, $headers, $body))
	echo "メールを送信しました。";
else
	echo "メール送信に失敗しました。";

// Invalid argument supplied for foreach() in /usr/pkg/lib/php/Mail.php on line 130
?>
この画面は Jeedosaquin によって表示しています。
Last Update: Sat, 07 Jun 2014 22:16:17 GMT 1.66 2008/03/08