Contents
PHPを使用して電子メールの送信
php.iniファイルで、システムが電子メールを送信する方法の詳細をPHPで正しく設定する必要があります。/ etc /ディレクトリにあるphp.iniファイルを開き、[mail function]というセクションを見つけます。
最初に設定するのは、メールサーバーのアドレスを定義するSMTPの設定。2番目はsendmail_fromと呼ばれ、独自の電子メールアドレスを定義します。
Windowsの設定は次のようになります。
[mail function] ; For Win32 only. SMTP = smtp.secureserver.net ; For win32 only sendmail_from = exsample.com
プレーンテキストの電子メールを送信する
PHPはmail()関数を使用して電子メールを送信します。この関数は、受信者の電子メールアドレス、メッセージの件名、および実際のメッセージを指定する3つの必須引数を必要とし、さらに2つのオプションのパラメータが必要です。
mail( to, subject, message, headers, parameters );
ここに各パラメータの説明があります。
Sr.No | パラメータと説明 |
---|---|
1 | to
必須。電子メールの受信者/受信者を指定します。 |
2 | subject
必須。電子メールの件名を指定します。このパラメータには改行文字を含めることはできません。 |
3 | message
必須。送信するメッセージを定義します。各行はLF(\ n)で区切る必要があります。行は70文字を超えてはなりません |
4 | headers
任意。From、Cc、Bccなどの追加のヘッダーを指定します。追加のヘッダーは、CRLF(\ r \ n)で区切る必要があります。 |
5 | parameters
任意。送信メールプログラムの追加パラメータを指定します。 |
メール関数が呼び出されると、PHPはメールを送信しようとすると、成功した場合はtrueを返し、失敗した場合はfalseを返します。
mail()関数の最初の引数として複数の受信者をカンマ区切りのリストで指定することができます。
HTMLメールを送信する
PHPを使用してテキストメッセージを送信すると、すべてのコンテンツは単純なテキストとして扱われます。テキストメッセージにHTMLタグを含める場合でも、HTMLタグは単純なテキストとして表示され、HTMLタグはHTML構文に従ってフォーマットされません。しかし、PHPはHTMLメッセージを実際のHTMLメッセージとして送信するオプションを提供しています。 電子メールメッセージを送信する際に、Mimeバージョン、コンテンツタイプ、および文字セットを指定してHTML電子メールを送信することができます。
例
次の例では、xyz@somedomain.comにHTMLメールを送信し、afgh@somedomain.comにコピーします。このプログラムは、ユーザーからのすべてのコンテンツを受け取るようにコーディングしてから、電子メールを送信する必要があります。
<html> <head> <title>Sending HTML email using PHP</title> </head> <body> <?php $to = "xyz@somedomain.com"; $subject = "This is subject"; $message = "<b>This is HTML message.</b>"; $message .= "<h1>This is headline.</h1>"; $header = "From:abc@somedomain.com \r\n"; $header .= "Cc:afgh@somedomain.com \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?> </body> </html>
電子メールで添付ファイルを送信する
混合コンテンツを含む電子メールを送信するには、Content-typeヘッダーをmultipart / mixedに設定する必要があります。次に、境界内でテキストセクションと添付セクションを指定できます。 境界は、2つのハイフンとそれに続く電子メールのメッセージ部分に表示されない一意の番号で開始されます。PHP関数md5()を使用して、32桁の16進数を作成して一意の番号を作成します。電子メールの最終セクションを示す最後の境界線も2つのハイフンで終わらなければなりません。
<?php // request variables // important $from = $_REQUEST["from"]; $emaila = $_REQUEST["emaila"]; $filea = $_REQUEST["filea"]; if ($filea) { function mail_attachment ($from , $to, $subject, $message, $attachment){ $fileatt = $attachment; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1; $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment $email_from = $from; // Who the email is from $subject = "New Attachment Message"; $email_subject = $subject; // The Subject of the email $email_txt = $message; // Message that the email has in it $email_to = $to; // Who the email is to $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $msg_txt="\n\n You have recieved a new attachment message from $from"; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_txt .= $msg_txt; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_txt . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name = \"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename = \"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "File Sent Successfully."; unlink($attachment); // delete a file after attachment sent. }else { die("Sorry but the email could not be sent. Please go back and try again!"); } } move_uploaded_file($_FILES["filea"]["tmp_name"], 'temp/'.basename($_FILES['filea']['name'])); mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"])); } ?> <html> <head> <script language = "javascript" type = "text/javascript"> function CheckData45() { with(document.filepost) { if(filea.value ! = "") { document.getElementById('one').innerText = "Attaching File ... Please Wait"; } } } </script> </head> <body> <table width = "100%" height = "100%" border = "0" cellpadding = "0" cellspacing = "0"> <tr> <td align = "center"> <form name = "filepost" method = "post" action = "file.php" enctype = "multipart/form-data" id = "file"> <table width = "300" border = "0" cellspacing = "0" cellpadding = "0"> <tr valign = "bottom"> <td height = "20">Your Name:</td> </tr> <tr> <td><input name = "from" type = "text" id = "from" size = "30"></td> </tr> <tr valign = "bottom"> <td height = "20">Your Email Address:</td> </tr> <tr> <td class = "frmtxt2"><input name = "emaila" type = "text" id = "emaila" size = "30"></td> </tr> <tr> <td height = "20" valign = "bottom">Attach File:</td> </tr> <tr valign = "bottom"> <td valign = "bottom"><input name = "filea" type = "file" id = "filea" size = "16"></td> </tr> <tr> <td height = "40" valign = "middle"><input name = "Reset2" type = "reset" id = "Reset2" value = "Reset"> <input name = "Submit2" type = "submit" value = "Submit" onClick = "return CheckData45()"></td> </tr> </table> </form> <center> <table width = "400"> <tr> <td id = "one"> </td> </tr> </table> </center> </td> </tr> </table> </body> </html>
~約8,000名の受講生と80社以上の導入実績~
現役エンジニアのオンライン家庭教師CodeCamp
前のページ⑰PHPのセッション
次のページ⑲