PHP mail()関数
完全な PHP Mail リファレンスマニュアル
定義と用法
mail() 関数を使用すると、スクリプトから電子メールを直接送信できます。
電子メールの配達が正常に受け付けられた場合は TRUE を返し、それ以外の場合は FALSE を返します。
構文
mail(to,subject,message,headers,parameters)
| パラメータ | 説明 |
|---|---|
| to | 必須。電子メールの受信者を指定します。 |
| subject | 必須。電子メールの件名を指定します。注釈:このパラメータには改行文字を含めることはできません。 |
| message | 必須。送信するメッセージを定義します。各行を LF(\n)で区切ります。行は70文字を超えないようにしてください。 Windows の注釈:PHP が SMTP サーバーに直接接続している場合、メッセージの行の先頭にピリオドが見つかると、そのピリオドは削除されます。この問題を解決するには、単一のピリオドを二重のピリオドに置き換えます: |
| headers | オプション。From、Cc、Bcc などの追加ヘッダーを指定します。追加ヘッダーは CRLF(\r\n)で区切る必要があります。 注釈:電子メールを送信する際には、From ヘッダーを含める必要があります。このパラメータで設定するか、php.ini ファイルで設定できます。 |
| parameters | オプション。sendmail プログラムの追加パラメータを指定します(sendmail_path 設定で定義)。例:sendmail を -f sendmail オプションと一緒に使用する場合、sendmail を使用して送信者アドレスを設定できます。 |
ヒントと注釈
注釈:電子メールの配達が受け付けられたからといって、電子メールが意図した宛先に届くとは限らないことに注意してください。
例 1
簡単な電子メールを送信する:
<?php
$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("somebody@example.com","My subject",$txt);
?>
$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("somebody@example.com","My subject",$txt);
?>
例 2
追加ヘッダーを含む電子メールを送信する:
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "rn" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "rn" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
例 3
HTML 電子メールを送信する:
<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-バージョン: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
// More headers
$headers .= 'From: <webmaster@example.com>' . "rn";
$headers .= 'Cc: myboss@example.com' . "rn";
mail($to,$subject,$message,$headers);
?>
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-バージョン: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
// More headers
$headers .= 'From: <webmaster@example.com>' . "rn";
$headers .= 'Cc: myboss@example.com' . "rn";
mail($to,$subject,$message,$headers);
?>
完全な PHP Mail リファレンスマニュアル その他の拡張機能