如果您想以 Gmail 用户的身份发送邮件,我建议您使用 Gmail 的 SMTP。因为 IMAP 主要用于接收来自收件箱的邮件。
以下是从 Gmail 的 SMTP 发送邮件的方法。
第一的
- 确保安装了 PEAR Mail 包。
- 通常,特别是对于 PHP 4 或更高版本,这已经为您完成了。试一试。(邮件.php)
然后
使用 SMTP 身份验证从 PHP 发送邮件 - 示例
<?php
require_once "Mail.php";
$from = "Sender <sender@gmail.com>";
$to = "Recipient <recipient@gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "username@gmail.com";
$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>");
}
?>
使用 SMTP 身份验证和 SSL 加密从 PHP 发送邮件 - 示例
<?php
require_once "Mail.php";
$from = "Sender <sender@gmail.com>";
$to = "Recipient <recipient@gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username@gmail.com";
$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