Phpmail

出自智邦小幫手

跳轉到: 導航, 搜索

【Linux進階主機】mail()寄信程式範例

以下僅提供mail()寄信程式撰寫範例,不另提供smtp發信主機,請將程式碼儲存成php檔案,例如:email.php

範例程式碼:

 <?php
   $_toEmailAddress = "info at somedomain.com";
   $_ccEmailAddress = "";
   //$_bccEmailAddress = "alternate at destination.com";

   $_subject = $_POST[ "subject" ];
   $_comments = $_POST[ "message" ];
   $phone = $_POST[ "phoneNumber" ];
   $name = $_POST[ "name" ];
   $_from = "\"$name\" <" . $_POST[ "email" ] . ">";
	
	if ( $_POST[ "cc_me" ] == "on" ) {
	  $_ccEmailAddress = $_from;
	}

   $message = "\n\nName: " . $name . "\n";
   $message .= "Email: " . $_from . "\n";
   $message .= "Phone Number: " . $phone . "\n";
   $message .= "\nMessage: " . $_comments . "\n\n";

   $_headers = "";
   //$_headers .= "MIME-Version: 1.0\r\n";
   //$_headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
   $_headers .= "From: $_from\r\n";
   //$_headers .= "To: ".$_toEmailAddress."\r\n";
   if (! empty($_bccEmailAddress) ) {
       $_headers .= "BCC: ".$_bccEmailAddress."\r\n";
   }
   if (! empty($_ccEmailAddress) ) {
       $_headers .= "CC: ".$_ccEmailAddress."\r\n";
   }
   $_headers .= "Reply-To: $_from\r\n";
   $_headers .= "X-Priority: 1\r\n";
   $_headers .= "X-MSMail-Priority: High\r\n";
   $_headers .= "X-Mailer: Hero Network Email System";

   $mailresponse = mail("$_toEmailAddress",
                        "$_subject",
                        "$message",
                        $_headers
                        );
 ?>


網頁上的填寫的表格,請將程式碼儲存成HTML檔案,例如:email_form.html

範例程式碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <title>
       Untitled
     </title>
   </head>
   <body>
     <form action="email.php" method="post">
       <table border="0" width="383" cellpadding="2" cellspacing="0" 
align="center">
         <tr>
           <td width="63" style="font-family: arial, san-serif; font-size: 
12px; color: #333366;" align="right">Name:</td>
           <td><input type="text" class="name_text" name="name" size="20"></td>
         </tr>
         <tr>
           <td width="63" style="font-family: arial, san-serif; font-size: 
12px; color: #333366;" align="right">Email:</td>
           <td><input type="text" class="name_text" name="email" size="20"> 
<img src="img/required.gif" alt="Required" width="9" height="8"></td>
         </tr>
         <tr>
           <td colspan="2" style="font-family: arial, san-serif; font-size: 
12px; color: #333366;">Send a copy of this message to me too: <input 
type="checkbox" name="cc_me" checked="" value="on">
           </td>
         </tr>
         <tr>
           <td width="63" style="font-family: arial, san-serif; font-size: 
12px; color: #333366;"><p align="right" style="text-align: right">Phone:</p></td>
           <td><input type="text" class="name_text" name="phoneNumber" 
size="20"></td>
         </tr>
         <tr>
           <td colspan="2">
             <br>
             <p style="font-family: arial, san-serif; font-size: 12px; color: 
#333366;">
               To best route your email, please select the most appropriate 
category:
             </p>
             <select class="greySelect" name="subject">
               <option class="optionRed" value="Request Information" 
selected>Request information</option>
               <option class="optionRed" value="Owner Contact Request">Send 
Message Directly To Owner</option>
               <option class="optionRed" value="Requesting 
Something">Other</option>
             </select><br>
             <br>

             <p style="font-family: arial, san-serif; font-size: 12px; color: 
#333366;">Input message here.</p>
              <textarea class="input_text" rows="20" cols="50" 
style="font-family: arial, san-serif; font-size: 12px; color: navy; 
scrollbar-base-color:pink;scrollbar-arrow-color: #ee0000;" name="message" 
size="15"></textarea>
             <p align="center"><input type="submit" value=" Send Request " 
name="Submit" style="font-family: arial, helvetica, san-serif; font-size: 13px; 
color: #ffffff; background: #006633;"></p>
             <br>
             <br>
             <br>
           </td>
         </tr>
       </table>
     </form>
   </body>
</html>