【问题标题】:Send email from localhost with gmail(windows)使用 gmail(windows)从本地主机发送电子邮件
【发布时间】:2011-02-13 06:37:19
【问题描述】:

我想使用本地主机中的 mail() 函数。我安装了 WAMP 和一个 Gmail 帐户。我知道 Gmail 的 SMTP 是 smtp.gmail.com,端口是 465。我需要在 WAMP 中配置什么才能使用 mail() 函数? 谢谢

【问题讨论】:

    标签: php email


    【解决方案1】:

    Ayush 的回答非常有用,下面是稍微简化的方法
    1) 下载PHPMailer
    2) 解压到您的 php 项目中的文件夹并将其重命名为 phpmailer
    3) 创建 gmail-sample.php 并粘贴以下代码:

        <?php
        require("phpmailer/class.phpmailer.php");
        $mail = new PHPMailer();
    
        // ---------- adjust these lines ---------------------------------------
        $mail->Username = "your.username@gmail.com"; // your GMail user name
        $mail->Password = "your-gmail-password"; 
        $mail->AddAddress("friends.email@domain.com"); // recipients email
        $mail->FromName = "your name"; // readable name
    
        $mail->Subject = "Subject title";
        $mail->Body    = "Here is the message you want to send to your friend."; 
        //-----------------------------------------------------------------------
    
        $mail->Host = "ssl://smtp.gmail.com"; // GMail
        $mail->Port = 465;
        $mail->IsSMTP(); // use SMTP
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->From = $mail->Username;
        if(!$mail->Send())
            echo "Mailer Error: " . $mail->ErrorInfo;
        else
            echo "Message has been sent";
        ?>
    

    4) 从浏览器发送邮件(例如http://localhost/your-project/gmail-sample.php)。

    【讨论】:

      【解决方案2】:

      我曾经收到“SMTP 错误:无法连接到 SMTP 主机”。

      此错误是由于 XAMPP (1.7.7) 及其 Apache 服务器造成的,默认情况下未启用其“SSL”选项。所以我们必须自己启用它。

      怎么办? 在您的 XAMPP 的 PHP.ini 文件中,您必须添加以下扩展名(未编写或注释): extension=php_openssl.dll

      保存 php.ini 文件,重新启动您的 apache 服务器,然后……尽情享受吧!

      个人使用:
      Port = 465
      Host = smtp.gmail.com
      SMTPAuth = true
      SMTPDebug = 1
      SMTPSecure = 'ssl'

      【讨论】:

        【解决方案3】:

        确保已安装 PEAR Mail 包。

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

        或者您可以使用第三方 php 类来发送邮件。像 PHPMailer 这样更容易

        PHPMailer

        【讨论】:

        • 我收到了这个错误。不推荐使用:在第 154 行的 C:\xampp\php\PEAR\Mail.php 中不推荐使用通过引用分配 new 的返回值
        • 然后使用phpmailer 真的很简单!
        猜你喜欢
        • 2015-05-29
        • 1970-01-01
        • 2012-05-25
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 2021-02-12
        • 2016-07-22
        相关资源
        最近更新 更多