「PHPの基礎 - メール」の版間の差分

提供: MochiuWiki : SUSE, EC, PCB

文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…
139行目: 139行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>
{{#seo:
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux
|image=/resources/assets/MochiuLogo_Single_Blue.png
}}


__FORCETOC__
__FORCETOC__
[[カテゴリ:Web]]
[[カテゴリ:Web]]

2024年11月5日 (火) 00:35時点における版

概要

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にメール送信元のメールアドレスを設定できる。
    設定する場合は、先頭のセミコロン(;)を外してメールアドレスを設定する。
[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>