使用 Gmail 的 PHP 邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mail using Gmail
提问by Dinah
In my PHP web app, I want to be notified via email whenever certain errors occur. I'd like to use my Gmail account for sending these. How could this be done?
在我的 PHP Web 应用程序中,我希望在发生某些错误时通过电子邮件收到通知。我想使用我的 Gmail 帐户发送这些邮件。这怎么可能?
采纳答案by Javache
Gmail's SMTP-server requires a very specific configuration.
Gmail 的 SMTP 服务器需要非常具体的配置。
From Gmail help:
从Gmail 帮助:
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 ([email protected])
Password: your Gmail password
You can probably set these settings up in Pear::Mailor PHPMailer. Check out their documentation for more details.
您可以在Pear::Mail或PHPMailer 中设置这些设置。查看他们的文档以获取更多详细信息。
回答by maxsilver
You could use PEAR's mail function with Gmail's SMTP Server
您可以在 Gmail 的 SMTP 服务器上使用 PEAR 的邮件功能
Note that when sending e-mail using Gmail's SMTP server, it will look like it came from your Gmail address, despite what you value is for $from.
请注意,当使用 Gmail 的 SMTP 服务器发送电子邮件时,它看起来像是来自您的 Gmail 地址,尽管 $from 的值是多少。
(following code taken from About.com Programming Tips)
(以下代码取自About.com 编程技巧)
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$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>");
}
?>

