TShopping

 找回密碼
 註冊
搜索
查看: 1932|回復: 0

[分享] php mail()函數用方實例

[複製鏈接]
發表於 2017-6-15 23:22:54 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
定義和用法

mail()函數允許您從腳本中直接發送電子郵件。

如果郵件的投遞被成功地接收,則返回true,否則返回false。

語法

mail(to,subject,message,headers,parameters)

參數        描述
至        必需。規定郵件的接收者。
學科        必需。規定郵件的主題。該參數不能包含任何換行字符。
信息        必需。規定要發送的消息。
頭        必需。規定額外的報頭,比如From,Cc以及Bcc。
參數        必需。規定sendmail程序的額外參數。
說明

在信息參數規定的消息中,行之間必須以一個LF(\ n)分隔。每行不能超過70個字符。(Windows下)當PHP直接連接到SMTP服務器時,如果在一行開頭髮現一個句號,則會被刪掉。要避免此問題,將單個句號替換成兩個句號。
  1. <?php
  2. $text = str_replace("\n.", "\n..", $text);
  3. ?>
複製代碼

實例一:發送HTML郵件

  1. <html>   
  2.    <head>
  3.       <title>Sending HTML email using PHP</title>
  4.    </head>   
  5.    <body>
  6.       <?php
  7.          $to = "xyz@somedomain.com";
  8.          $subject = "This is subject";
  9.          $message = "<b>This is HTML message.</b>";
  10.          $message .= "<h1>This is headline.</h1>";
  11.          $header = "From:abc@somedomain.com \r\n";
  12.          $header = "Cc:afgh@somedomain.com \r\n";
  13.          $header .= "MIME-Version: 1.0\r\n";
  14.          $header .= "Content-type: text/html\r\n";
  15.          $retval = mail ($to,$subject,$message,$header);
  16.          if( $retval == true )
  17.          {
  18.             echo "Message sent successfully...";
  19.          }
  20.          else
  21.          {
  22.             echo "Message could not be sent...";
  23.          }
  24.       ?>
  25.    </body>
  26. </html>
複製代碼

實例二:發送帶附件的郵件

  1. <?php
  2.    // request variables // important
  3.    $from=$_REQUEST["from"];
  4.    $emaila=$_REQUEST["emaila"];
  5.    $filea=$_REQUEST["filea"];
  6.    if ($filea)
  7.    {
  8.       function mail_attachment ($from , $to, $subject, $message, $attachment){
  9.          $fileatt = $attachment; // Path to the file
  10.          $fileatt_type = "application/octet-stream"; // File Type        
  11.          $start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
  12.          $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
  13.          $email_from = $from; // Who the email is from
  14.          $subject = "New Attachment Message";
  15.          $email_subject =  $subject; // The Subject of the email
  16.          $email_txt = $message; // Message that the email has in it
  17.          $email_to = $to; // Who the email is to
  18.          $headers = "From: ".$email_from;
  19.          $file = fopen($fileatt,'rb');
  20.          $data = fread($file,filesize($fileatt));
  21.          fclose($file);   
  22.          $msg_txt="\n\n You have recieved a new attachment message from $from";
  23.          $semi_rand = md5(time());
  24.          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  25.          $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
  26.          $email_txt .= $msg_txt;
  27.          $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";
  28.          $data = chunk_split(base64_encode($data));
  29.          $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";
  30.          $ok = mail($email_to, $email_subject, $email_message, $headers);
  31.          if($ok)
  32.          {
  33.             echo "File Sent Successfully.";
  34.             unlink($attachment); // delete a file after attachment sent.
  35.          }         
  36.          else
  37.          {
  38.             die("Sorry but the email could not be sent. Please go back and try again!");
  39.          }
  40.       }
  41.       move_uploaded_file($_FILES["filea"]["tmp_name"],'temp/'.basename($_FILES['filea']['name']));
  42.       mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"]));
  43.    }
  44. ?>
  45. <html>
  46.    <head>   
  47.       <script language="javascript" type="text/javascript">
  48.          function CheckData45()
  49.          {
  50.             with(document.filepost)
  51.             {
  52.                if(filea.value != "")
  53.                {
  54.                   document.getElementById('one').innerText = "Attaching File ... Please Wait";
  55.                }
  56.             }
  57.          }
  58.       </script>
  59.    </head>
  60.    <body>
  61.       <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  62.          <tr>
  63.             <td align="center">
  64.                <form name="filepost" method="post" action="file.php" enctype="multipart/form-data" id="file">
  65.                   <table width="300" border="0" cellspacing="0" cellpadding="0">
  66.                      <tr valign="bottom">
  67.                         <td height="20">Your Name:</td>
  68.                      </tr>
  69.                      <tr>
  70.                         <td><input name="from" type="text" id="from" size="30"></td>
  71.                      </tr>
  72.                      <tr valign="bottom">
  73.                         <td height="20">Your Email Address:</td>
  74.                      </tr>
  75.                      <tr>
  76.                         <td class="frmtxt2"><input name="emaila" type="text" id="emaila" size="30"> </td>
  77.                      </tr>
  78.                      <tr>
  79.                         <td height="20" valign="bottom">Attach File:</td>
  80.                      </tr>
  81.                      <tr valign="bottom">
  82.                         <td valign="bottom"><input name="filea" type="file" id="filea" size="16"></td>
  83.                      </tr>
  84.                      <tr>
  85.                         <td height="40" valign="middle"><input name="Reset2" type="reset" id="Reset2" value="Reset">
  86.                         <input name="Submit2" type="submit" value="Submit" onClick="return CheckData45()"></td>
  87.                      </tr>
  88.                   </table>
  89.                </form>  
  90.                <center>
  91.                   <table width="400">
  92.                      <tr>
  93.                         <td id="one">
  94.                         </td>
  95.                      </tr>
  96.                   </table>
  97.                </center>               
  98.             </td>
  99.          </tr>
  100.       </table>
  101.    </body>
  102. </html>
複製代碼

原文地址:http://www.manongjc.com/article/72.html

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-19 14:06 , Processed in 0.053278 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表