使用gmail(windows)从localhost发送电子邮件

时间:2011-02-13 06:37:20

标签: php email

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

3 个答案:

答案 0 :(得分:7)

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)。

答案 1 :(得分:4)

我曾经遇到过“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'

答案 2 :(得分:2)

确保已安装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