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

有帮助吗?

解决方案

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 

您也许可以在中设置这些设置 梨::邮件 或者 PHP邮件程序. 。查看他们的文档以获取更多详细信息。

其他提示

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

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

(以下代码取自 About.com 编程技巧 )

<?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>");
 }
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top