问题 使用Gmail的PHP邮件


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


6927
2017-08-30 16:18


起源



答案:


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

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 (username@gmail.com)
Password:     your Gmail password 

您可以在其中设置这些设置 PEAR ::邮件 要么 PHPMailer的。查看他们的文档了解更多详情。


8
2017-08-30 19:22





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

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

(以下代码取自 关于编程技巧 )

<?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>");
 }
?>

4
2017-08-30 16:21