在這邊配合使用 PHPMailer 進行 Email 發送程式撰寫,PHPMailer 目前提供 PHP 4 與

PHP5/6 版本可以使用,可以依照自己所使用的 PHP 版本,進行選擇。

 

可至 PHPMailer 網站 http://phpmailer.worxware.com/ ,點選 Download 選擇自己需要的版

本進行下載。

2010-08-01_170340  

PHP 程式範例:(使用的 PHP 版本為 PHP5 )

2010-08-01_173150

HTML : 註 form 中需要加上 enctype="multipart/form-data"

<form id="form1" name="form1" method="post" action="mail.php" 
enctype
="multipart/form-data" >
  <table width="333" border="1">
    <tr>
      <td>姓名</td>
      <td><input name="name" type="text" id="name" /></td>
    </tr>
    <tr>
      <td width="92">Email</td>
      <td width="225"><input name="email" type="text" id="email" /></td>
    </tr>
    <tr>
      <td valign="top">內容</td>
      <td><textarea name="contents" rows="10" id="contents"></textarea></td>
    </tr>
    <tr>
      <td valign="top">附件</td>
      <td><input type="file" name="file" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="Submit" value="送出" />
        <input type="reset" value="重設" />
      </div></td>
    </tr>
  </table>
</form>

 

PHP 程式:


<?php
    
    require_once('class.phpmailer.php');
    
    //檔案上傳
    $filename=$_FILES['file']['name'];
    $tmpname=$_FILES['file']['tmp_name'];
    $filetype=$_FILES['file']['type'];
    $filesize=$_FILES['file']['size'];    
    
    
    $mail = new PHPMailer(); 
                
    mb_internal_encoding('UTF-8');
                            
    $mail->SetFrom($_POST['email'],$_POST['name']);                                
    
    //你要寄送到的信箱
    $mail->AddAddress("輸入你要寄送到的信箱", "輸入你要寄送到的信箱名稱");
 
    $mail->Subject =  mb_encode_mimeheader("測試", "UTF-8");
    $mail->CharSet="UTF-8";                
    
    $body=nl2br($_POST['contents']);
                         
    $mail->MsgHTML($body);
    $mail->IsHTML(true);
    
    //夾帶附件檔
    $mail->AddAttachment($tmpname, $filename);
                
    if($mail->Send()){
        echo "寄件成功";
    }
    
?>

 

範例檔:mail.rar

arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()