MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
PHPの基礎 - メールのソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
PHPの基礎 - メール
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == <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> == ASCIIメールの送信 : mail関数 == ASCIIメールの送信を行うには、<code>mail</code>関数を使用する。<br> bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] ) <br> パラメータ: to メールの宛先 subject メールのタイトル message メールの本文 additional_headers 追加ヘッダ additional_parameters 追加パラメータ 戻り値: メール送信が受け入れられた場合はTRUE、それ以外の場合はFALSE <br> メールの宛先、タイトル、本文を指定してメールを送信する。<br> <br> 以下に、メールの宛先(メールアドレス)の書式を示す。<br> <u>ただし、Windowsで使用する場合は、User <user@example.com>の形式は使用できない。</u><br> user@example.com user@example.com, anotheruser@example.com User <user@example.com> User <user@example.com>, Another User <anotheruser@example.com> <br> メールのタイトルに指定する文字列は、改行できない。<br> <br> メールの本文は、複数行の文字列を指定できる。<br> 改行する場合は<code>¥n</code>(LF)を使用する。<br> なお、各行の長さは70文字を超えてはいけないため、<code>wordwrap</code>関数を使用して70文字以上含まれる行は改行する。<br> <syntaxhighlight lang="php"> $message = wordwrap($message, 70, "¥n"); </syntaxhighlight> <br> 追加ヘッダは、From、Cc、Reply-To等のヘッダとして、記述する内容を指定する。<br> 複数のヘッダを追加する場合は、<code>¥r¥n</code>(CRLF)で区切る。<br> ヘッダの中で、Fromヘッダのみ必須となっている。<br> 引数の<code>additional_headers</code>は省略可能であるが、php.iniファイルで<code>sendmail_from</code>を設定していない場合は、Fromヘッダの記述が必要である。<br> <br> 追加パラメータは、<code>sendmail</code>等へパラメータを渡す場合に使用する。<br> <u>ただし、Windowsの場合は使用しない。</u><br> <br> 例えば、以下のような記述となる。<br> <syntaxhighlight lang="php"> $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); </syntaxhighlight> <br> 以下の例において、index.phpというファイル名で、/<Webサーバのドキュメントルート>/mailディレクトリに保存する。<br> 送信元および送信先のメールアドレスに変更して、実際に使用できるメールアドレスに変更する。<br> <br> 次に、Webブラウザを起動して、以下のWebサイトにアクセスする。<br> http://localhost/mail/index.php<br> <br> SMTPサーバがメールの送信を受け入れた場合、Webブラウザに成功と表示される。<br> <syntaxhighlight lang="php"> <!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> </syntaxhighlight> <br><br> __FORCETOC__ [[カテゴリ:Web]]
PHPの基礎 - メール
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse