PHP mail()函數使用及配置方法_php技巧

來源:互聯網
上載者:User

配置

工欲善其事,必先利其器。首先我們以windows下面為例進行說明,如何配置一下本地的mail。

下載附件 sendmail.zip

 -解壓到任意路徑,修改sendmail.ini,根據實際需要修改下面的資訊。

複製代碼 代碼如下:

  [sendmail]
   smtp_server=smtp.qq.com
   smtp_port=25
   error_logfile=error.log
   debug_logfile=debug.log
   auth_username=***@qq.com
   auth_password=***
   force_sender=***@qq.com
 -php.ini
  [mail function]
   SMTP = smtp.qq.com
   smtp_port = 25
   sendmail_from = ***@qq.com
   sendmail_path = "D:/sendmail/sendmail.exe -t -i"
   mail.add_x_header = On

注意:
    目前測試只是qq發送成功,163的不成功可能是他有過濾系統,可以成功發送給gmail。

文法

複製代碼 代碼如下:
mail(to,subject,message,headers,parameters)

定義和用法

mail() 函數允許您從指令碼中直接寄送電子郵件。
如果郵件的投遞被成功地接收,則返回 true,否則返回 false。
說明
在 message 參數規定的訊息中,行之間必須以一個 LF(\n)分隔。每行不能超過 70 個字元。
(Windows 下)當 PHP 直接連接到 SMTP 伺服器時,如果在一行開頭髮現一個句號,則會被刪掉。要避免此問題,將單個句號替換成兩個句號。

複製代碼 代碼如下:

<?php 
$text = str_replace("\n.", "\n..", $text); 
?>

提示和注釋

注釋:您需要緊記,郵件投遞被接受,並不意味著郵件到達了計劃的目的地。
樣本
下面引用一個官方的發送HTML郵件的例子。

複製代碼 代碼如下:

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// 當發送 HTML 電子郵件時,請始終設定 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";

// 更多前序
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.