【问题标题】:PHP mail using Gmail使用 Gmail 的 PHP 邮件
【发布时间】:2010-09-07 08:33:31
【问题描述】:

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

【问题讨论】:

    标签: php email gmail


    【解决方案1】:

    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 中设置这些设置。查看他们的文档了解更多详情。

    【讨论】:

      【解决方案2】:

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

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

      (以下代码取自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>");
       }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-16
        • 1970-01-01
        • 2013-02-25
        • 2011-05-30
        • 2016-03-30
        • 2013-07-13
        • 1970-01-01
        相关资源
        最近更新 更多