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

no_*_*dom 16 php email

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

Wol*_*lfi 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";
    ?>
Run Code Online (Sandbox Code Playgroud)

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


Sud*_*ha 2

确保 PEAR 邮件包已安装。

<?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>");
  }
 ?>
Run Code Online (Sandbox Code Playgroud)

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

PHP邮件程序