如果您想以Gmail用戶身份發送郵件,我建議您使用Gmail的SMTP。由於IMAP主要用於從收件箱接收郵件。
以下是如何從Gmail的SMTP發送郵件的方法。
首先
- 確保已安裝了PEAR郵件包。
- 通常情況下,特別是與PHP 4或更高版本,這已經 已經爲你做了。試試吧。 (Mail.php)
然後
從PHP使用SMTP驗證發送郵件 - 實施例
<?php
require_once "Mail.php";
$from = "Sender <[email protected]>";
$to = "Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "[email protected]";
$password = "Gmail Password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
從PHP使用SMTP認證和SSL加密發送郵件 - 實施例
<?php
require_once "Mail.php";
$from = "Sender <[email protected]>";
$to = "Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "[email protected]";
$password = "Gmail Password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
來源about.com
只要注意,谷歌的SMTP服務,在某種程度上頭不與相關的RFP符合篡改:http://lee-phillips.org/gmailRewriting/ –