「PHPの基礎 - メール」の版間の差分
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
<code>mail</code>関数または<code>mb_send_mail</code>関数を使用して、メールを送信する手順を記載する。<br> | <code>mail</code>関数または<code>mb_send_mail</code>関数を使用して、メールを送信する手順を記載する。<br> | ||
<br><br> | |||
== php.iniの設定 == | |||
<code>mail</code>関数または<code>mb_send_mail</code>関数では、php.iniファイルで設定したSMTPサーバ名とポート番号の値を読み込んで使用する。<br> | |||
そのため、php.iniファイルの該当箇所を適切な値に設定する必要がある。<br> | |||
<br> | |||
php.iniファイルの[mail function]において、以下の設定を記述する。<br> | |||
* SMTP | |||
*: SMTPサーバのホスト名またはIPアドレスを指定する。 | |||
*: もし、ローカル環境のメール送信用サーバを使用する場合、<code>localhost</code>でもよい。 | |||
* smtp_port | |||
*: メールの送信時に使用するポート番号を指定する。 | |||
*: 標準のポート番号は<code>25</code>番であるが、迷惑メール対策で<code>Outbound Port25 Blocking</code>が実施されている場合は、<code>587</code>番等を使用する。 | |||
*: 使用するメールサーバに合わせて設定すること。 | |||
* sendmail_from | |||
*: Windowsで使用する場合、<code>sendmail_from</code>にメール送信元のメールアドレスを設定できる。 | |||
*: 設定する場合は、先頭のセミコロン(;)を外してメールアドレスを設定する。 | |||
[mail function] | |||
; For Windows only. | |||
SMTP = <SMTPサーバのホスト名またはIPアドレス> | |||
smtp_port = <ポート番号> | |||
; For Windows only. | |||
;sendmail_from = me@example.com | |||
; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). | |||
;sendmail_path = | |||
; Force the addition of the specified parameters to be passed as extra parameters | |||
; to the sendmail binary. These parameters will always replace the value of | |||
; the 5th parameter to mail(), even in safe mode. | |||
;mail.force_extra_parameters = | |||
<br> | |||
以下に、設定例を示す。<br> | |||
[mail function] | |||
; For Win32 only. | |||
SMTP = smtp.example.jp | |||
smtp_port = 25 | |||
; For Win32 only. | |||
sendmail_from = peke@example.jp | |||
<br> | |||
設定変更後は、Apache等のWebサーバを再起動する。<br> | |||
<br><br> | <br><br> | ||
2021年3月2日 (火) 16:58時点における版
概要
mail関数またはmb_send_mail関数を使用して、メールを送信する手順を記載する。
php.iniの設定
mail関数またはmb_send_mail関数では、php.iniファイルで設定したSMTPサーバ名とポート番号の値を読み込んで使用する。
そのため、php.iniファイルの該当箇所を適切な値に設定する必要がある。
php.iniファイルの[mail function]において、以下の設定を記述する。
- SMTP
- SMTPサーバのホスト名またはIPアドレスを指定する。
- もし、ローカル環境のメール送信用サーバを使用する場合、
localhostでもよい。
- smtp_port
- メールの送信時に使用するポート番号を指定する。
- 標準のポート番号は
25番であるが、迷惑メール対策でOutbound Port25 Blockingが実施されている場合は、587番等を使用する。 - 使用するメールサーバに合わせて設定すること。
- sendmail_from
- Windowsで使用する場合、
sendmail_fromにメール送信元のメールアドレスを設定できる。 - 設定する場合は、先頭のセミコロン(;)を外してメールアドレスを設定する。
- Windowsで使用する場合、
[mail function] ; For Windows only. SMTP = <SMTPサーバのホスト名またはIPアドレス> smtp_port = <ポート番号> ; For Windows only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters =
以下に、設定例を示す。
[mail function] ; For Win32 only. SMTP = smtp.example.jp smtp_port = 25 ; For Win32 only. sendmail_from = peke@example.jp
設定変更後は、Apache等のWebサーバを再起動する。
ASCIIメールの送信 : mail関数
ASCIIメールの送信を行うには、mail関数を使用する。
bool mail ( string to,
string subject,
string message
[, string additional_headers
[, string additional_parameters]] )
パラメータ: to メールの宛先 subject メールのタイトル message メールの本文 additional_headers 追加ヘッダ additional_parameters 追加パラメータ 戻り値: メール送信が受け入れられた場合はTRUE、それ以外の場合はFALSE
メールの宛先、タイトル、本文を指定してメールを送信する。
以下に、メールの宛先(メールアドレス)の書式を示す。
ただし、Windowsで使用する場合は、User <user@example.com>の形式は使用できない。
user@example.com user@example.com, anotheruser@example.com User <user@example.com> User <user@example.com>, Another User <anotheruser@example.com>
メールのタイトルに指定する文字列は、改行できない。
メールの本文は、複数行の文字列を指定できる。
改行する場合は¥n(LF)を使用する。
なお、各行の長さは70文字を超えてはいけないため、wordwrap関数を使用して70文字以上含まれる行は改行する。
$message = wordwrap($message, 70, "¥n");
追加ヘッダは、From、Cc、Reply-To等のヘッダとして、記述する内容を指定する。
複数のヘッダを追加する場合は、¥r¥n(CRLF)で区切る。
ヘッダの中で、Fromヘッダのみ必須となっている。
引数のadditional_headersは省略可能であるが、php.iniファイルでsendmail_fromを設定していない場合は、Fromヘッダの記述が必要である。
追加パラメータは、sendmail等へパラメータを渡す場合に使用する。
ただし、Windowsの場合は使用しない。
例えば、以下のような記述となる。
$to = 'you@example.com';
$subject = 'Test Title';
$message = "This is Test mail¥nMulti Line";
$message = wordwrap($message, 70, "¥n");
$headers = 'From: my@example.com';
mail($to, $subject, $message, $headers);
以下の例において、index.phpというファイル名で、/<Webサーバのドキュメントルート>/mailディレクトリに保存する。
送信元および送信先のメールアドレスに変更して、実際に使用できるメールアドレスに変更する。
次に、Webブラウザを起動して、以下のWebサイトにアクセスする。
http://localhost/mail/index.php
SMTPサーバがメールの送信を受け入れた場合、Webブラウザに成功と表示される。
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>PHP TEST</title>
</head>
<body>
<h1>メール送信</h1>
<?php
$to = 'you@example.com';
$subject = 'test mail';
$message = "This is Test mail¥nMulti Line";
$message = wordwrap($message, 70, "¥n");
$headers = 'From: my@example.com'."¥r¥n".
'To: you@example.com'."¥r¥n".
'X-Mailer: PHP/Mail';
if(mail($to, $subject, $message, $headers))
{
print('成功');
}
else
{
print('エラー');
}
?>
</body>
</html>