PHPを使用してメールを送信する

このプロジェクトに取り組んでいる間、特定の「申請者へのアンケート」を作成する必要がありました。このアンケートでは、アンケート全体を傷の後ろに示された電子メールアドレスに送信しなければなりませんでした。







bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
      
      





必須パラメーター:



オプションのパラメーター:



戻り値:





最も簡単な例


 <?php mail("E-mail ", "", "  \n 1-  \n 2-  \n 3- "); ?>
      
      







さらに複雑な例に移りましょう。




 <?php $to = "<mail@example.com>, " ; $to .= "mail2@example.com>"; $subject = " "; $message = ' <p> </p> </br> <b>1-  </b> </br><i>2-  </i> </br>'; $headers = "Content-type: text/html; charset=windows-1251 \r\n"; $headers .= "From:    <from@example.com>\r\n"; $headers .= "Reply-To: reply-to@example.com\r\n"; mail($to, $subject, $message, $headers); ?>
      
      







最初に、レターの宛先を決定し、&to変数がこれを担当しますが、複数の受信者がいる場合は、電子メールアドレスをコンマで区切って記述します。 メール。



変数$ subjectと$ messageについては説明しませんが、これはすでに明らかです。



この例では、$ headers変数は3行で構成されています。





そして今、添付ファイル(添付ファイル)付きの最も興味深い送信レター




 $subject = " "; $message =" "; //  ,     , , ,    .. $filename = "file.doc"; //   $filepath = "files/file.doc"; //   //      ,    $boundary = "--".md5(uniqid(time())); //   $mailheaders = "MIME-Version: 1.0;\r\n"; $mailheaders .="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n"; //       boundary $mailheaders .= "From: $user_email <$user_email>\r\n"; $mailheaders .= "Reply-To: $user_email\r\n"; $multipart = "--$boundary\r\n"; $multipart .= "Content-Type: text/html; charset=windows-1251\r\n"; $multipart .= "Content-Transfer-Encoding: base64\r\n"; $multipart .= \r\n; $multipart .= chunk_split(base64_encode(iconv("utf8", "windows-1251", $message))); //     //   $fp = fopen($filepath,"r"); if (!$fp) { print "   22"; exit(); } $file = fread($fp, filesize($filepath)); fclose($fp); //   $message_part = "\r\n--$boundary\r\n"; $message_part .= "Content-Type: application/octet-stream; name=\"$filename\"\r\n"; $message_part .= "Content-Transfer-Encoding: base64\r\n"; $message_part .= "Content-Disposition: attachment; filename=\"$filename\"\r\n"; $message_part .= \r\n; $message_part .= chunk_split(base64_encode($file)); $message_part .= "\r\n--$boundary--\r\n"; //    ,       $multipart .= $message_part; mail($to,$subject,$multipart,$mailheaders); //   //   60 . if (time_nanosleep(5, 0)) { unlink($filepath); } //  
      
      








All Articles