php 使用 gmail(windows) 从本地主机发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4982821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:26:17  来源:igfitidea点击:

Send email from localhost with gmail(windows)

phpemail

提问by no_freedom

I want to use the mail() function from my localhost. I have WAMP installed and a Gmail account. I know that the SMTP for Gmail is smtp.gmail.com and the port is 465. What I need to configure in WAMP so I can use the mail() function? Thanks

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

回答by Wolfi

Ayush's answer was very useful, below a slightly simplified approach
1) Download PHPMailer
2) Extract to folder within you php project and rename it to phpmailer
3) Create gmail-sample.php and paste the following code:

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

    <?php
    require("phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    // ---------- adjust these lines ---------------------------------------
    $mail->Username = "[email protected]"; // your GMail user name
    $mail->Password = "your-gmail-password"; 
    $mail->AddAddress("[email protected]"); // 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) Send mail from Browser (e.g. http://localhost/your-project/gmail-sample.php).

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

回答by grou76

I used to get the "SMTP Error: Could not connect to SMTP host".

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

This error is due to XAMPP (1.7.7) and its Apache server, whose "SSL" option is not enabled by default. So we have to enable it ourselves.

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

What to do? Into the PHP.ini file of your XAMPP, you must add the following extension (which is not written nor commented): extension=php_openssl.dll

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

Save the php.ini file, restart your apache server and.... enjoy it!

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

Personally it works with:
Port = 465
Host = smtp.gmail.com
SMTPAuth = true
SMTPDebug = 1
SMTPSecure = 'ssl'

就个人而言,它适用于:
Port = 465
Host = smtp.gmail.com
SMTPAuth = true
SMTPDebug = 1
SMTPSecure = 'ssl'

回答by Sudantha

Make sure the PEAR Mail package is installed.

确保安装了 PEAR 邮件包。

<?php
 require_once "Mail.php";

 $from = "Sandra Sender <[email protected]>";
 $to = "Ramona Recipient <[email protected]>";
 $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>");
  }
 ?>

Or you can use a third party php class for send mails. like PHPMailer which is lot more easy

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

PHPMailer

PHPMailer