使用Gmail的PHP邮件

时间:2008-08-30 16:18:35

标签: php email gmail

在我的PHP网络应用程序中,我希望在发生某些错误时通过电子邮件收到通知。我想使用我的Gmail帐户发送这些内容。怎么可以这样做?

2 个答案:

答案 0 :(得分:9)

Gmail的SMTP服务器需要非常具体的配置。

来自Gmail help

Outgoing Mail (SMTP) Server (requires TLS)
 - smtp.gmail.com
 - Use Authentication: Yes
 - Use STARTTLS: Yes (some clients call this SSL)
 - Port: 465 or 587
Account Name:   your full email address (including @gmail.com)
Email Address:  your email address (username@gmail.com)
Password:     your Gmail password 

您可以在Pear::MailPHPMailer中设置这些设置。查看他们的文档以获取更多详细信息。

答案 1 :(得分:4)

您可以将PEAR的邮件功能与Gmail的SMTP服务器一起使用

请注意,使用Gmail的SMTP服务器发送电子邮件时,它看起来就像是来自您的Gmail地址,不过您的价值是来自$。

(以下代码来自About.com Programming Tips

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

// stick your GMAIL SMTP info here! ------------------------------
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_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>");
 }
?>