php 如何配置 XAMPP 从本地主机发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15965376/
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
How to configure XAMPP to send mail from localhost?
提问by farid shaikh
I am trying to send mail from localhost. but i am unable to send the mail from localhost so can anybody tell me that how to reconfigure my xampp to send mail from localhost
我正在尝试从本地主机发送邮件。但我无法从本地主机发送邮件,所以有人可以告诉我如何重新配置我的 xampp 以从本地主机发送邮件
回答by Vikas Dwivedi
You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
您可以使用 sendmail 包从本地主机发送邮件,XAMPP 中内置了 sendmail 包。因此,如果您使用 XAMPP,那么您可以轻松地从本地主机发送邮件。
for example you can configure C:\xampp\php\php.ini
and c:\xampp\sendmail\sendmail.ini
for gmail to send mail.
例如,您可以配置C:\xampp\php\php.ini
和c:\xampp\sendmail\sendmail.ini
让 gmail 发送邮件。
in C:\xampp\php\php.ini
find extension=php_openssl.dll
and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
在C:\xampp\php\php.ini
查找extension=php_openssl.dll
并删除该行开头的分号以使 SSL 可用于 localhost 的 gmail。
in php.ini file find [mail function]
and change
在 php.ini 文件中查找[mail function]
并更改
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini
. Replace all the existing code in sendmail.ini with following code
现在打开C:\xampp\sendmail\sendmail.ini
。用以下代码替换 sendmail.ini 中的所有现有代码
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=my-gmail-password
[email protected]
Now you have done!! create php file with mail function and send mail from localhost.
现在你已经完成了!!使用邮件功能创建php文件并从本地主机发送邮件。
PS: don't forgot to replace my-gmail-idand my-gmail-passwordin above code.
Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
in the php.ini file
PS:不要忘记替换上面代码中的my-gmail-id和 my-gmail-password。另外,如果您从上面复制设置,请不要忘记删除重复的键。例如注释以下行,如果有另一个sendmail_path:sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
在php.ini文件
Also remember to restart the server using the XAMMP control panel so the changes take effect.
还要记住使用 XAMMP 控制面板重新启动服务器,以使更改生效。
For gmail please check https://support.google.com/accounts/answer/6010255to allow access from less secure apps.
对于 Gmail,请检查https://support.google.com/accounts/answer/6010255以允许从不太安全的应用程序访问。
To send email on Linux (with sendmail package) through Gmail from localhost please check PHP+Ubuntu Send email using gmail form localhost.
要从本地主机通过 Gmail 在 Linux 上发送电子邮件(使用 sendmail 包),请检查PHP+Ubuntu Send email using gmail form localhost。
回答by Paul Trotter
In XAMPP v3.2.1 for testing purposes you can see the emails that the XAMPP sends in XAMPP/mailoutput. In my case on Windows 8 this did not require any additional configuration and was a simple solution to testing email
在用于测试目的的 XAMPP v3.2.1 中,您可以在 XAMPP/mailoutput 中看到 XAMPP 发送的电子邮件。就我在 Windows 8 上的情况而言,这不需要任何额外的配置,是测试电子邮件的简单解决方案
回答by Vikas Khunteta
Its very simple to send emails on localhost or local server
在本地主机或本地服务器上发送电子邮件非常简单
Note: I am using the test mail server software on Windows 7 64bit with Xampp installed
注意:我在安装了 Xampp 的 Windows 7 64 位上使用测试邮件服务器软件
Just download test mail server tool and install according to the instruction given on its website Test Mail Server Tool
只需下载测试邮件服务器工具并根据其网站上提供的说明进行安装测试邮件服务器工具
Now you need to change only two lines under php.ini
file
现在您只需要更改php.ini
文件下的两行
- Find
[mail function]
and remove semi colon which is before;smtp = localhost
- Put the semi colon before
sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
- 查找
[mail function]
并删除之前的分号;smtp = localhost
- 把分号放在前面
sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
You don't need to change anything else, but if you still not getting emails than check for the SMTP port
, the port number must be same.
您不需要更改任何其他内容,但是如果您仍然没有收到电子邮件而不是检查SMTP port
,则端口号必须相同。
The above method is for default settings provided by the Xampp software.
以上方法是针对Xampp软件提供的默认设置。
回答by Piseth Sok
You can test send mail in Your PC without Internet
您可以在没有 Internet 的情况下在您的 PC 中测试发送邮件
you should use Papercutthis simple application to test send mail. and you don't need to configure anything.
您应该使用Papercut这个简单的应用程序来测试发送邮件。你不需要配置任何东西。
Just run it and try test send mail:
只需运行它并尝试测试发送邮件:
test_sendmail.php
test_sendmail.php
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
and you will see this:
你会看到这个:
I hope you will have a good day. you can find me on Youtubefor more tutorial Piseth Sok
我希望你会有美好的一天。你可以在Youtube 上找到我获取更多教程Piseth Sok
Cheer!
欢呼!
回答by Thirumalai murugan
You have to configure SMTPon your server. You can use G Suite SMTPby Google for free:
您必须在服务器上配置SMTP。您可以免费使用Google 提供的G Suite SMTP:
<?php
$mail = new PHPMailer(true);
// Send mail using Gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "[email protected]"; // GMAIL username
$mail->Password = "your-gmail-password"; // GMAIL password
}
// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
// Something went bad
echo "Fail :(";
}
?>
Read more about PHPMailer
here.
阅读更多关于PHPMailer
这里。
回答by M at
As in my personal experience I found that very similar thing to Vikas Dwivedi answer will work just fine.
根据我的个人经验,我发现与 Vikas Dwivedi 答案非常相似的东西会很好用。
Step 1 (php.ini file)
第 1 步(php.ini 文件)
In php.ini file located in xampp\php\php.ini
. Change settings to the following:
在位于xampp\php\php.ini
. 将设置更改为以下内容:
extension=php_openssl.dll
[mail function]
sendmail_path =":\xampp7\sendmail\sendmail.exe -t"
mail.add_x_header=On
Turn off other variables under mail funciton
by putting ;
before them. e.g ;smtp_port=25
将其他变量mail funciton
放在;
它们之前关闭它们。例如;smtp_port=25
Step 2 (sendmail.ini file)
第 2 步(sendmail.ini 文件)
In sendmail.ini located in xampp\sendmail\semdmail.ini change to the following:
在位于 xampp\sendmail\semdmail.ini 的 sendmail.ini 中更改为以下内容:
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto
[email protected]
auth_password=YourPassword
Step 3 (code)
第 3 步(代码)
Create a php file and use the following:
创建一个 php 文件并使用以下内容:
<?php
mail($to, "subject", "body", "From: ".$from);
?>
Notice
注意
- You need to restart apache in order for php.ini to reload.
- you need to activate Google Less secure app access in https://myaccount.google.com/u/1/security
- It might help to run Xampp with Admin permission.
- 您需要重新启动 apache 才能重新加载 php.ini。
- 您需要在https://myaccount.google.com/u/1/security 中激活 Google 不太安全的应用程序访问
- 以管理员权限运行 Xampp 可能会有所帮助。
回答by Kishan Sorathiya
This code is used for the mail from your localhost XAMPP and your Gmail account. This code is very easy and working for me try your self.
此代码用于来自您的本地主机 XAMPP 和您的 Gmail 帐户的邮件。这段代码非常简单,对我有用,试试你自己。
Below Change In php.ini File
在 php.ini 文件中的以下更改
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
extension=php_openssl.dll
Below Change In sendmail.ini File
下面更改 sendmail.ini 文件
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=your-gmail-password
[email protected]
Please write the belove code in your PHP file to send email
请在您的 PHP 文件中写入 belove 代码以发送电子邮件
<?php
$to = "[email protected]";
$subject = "Test Mail";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "CC: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<img src="//css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>Details</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>Details</td></tr>";
$message .= "<tr><td><strong>Type of Change:</strong> </td><td>Details</td></tr>";
$message .= "<tr><td><strong>Urgency:</strong> </td><td>Details</td></tr>";
$message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>Details</td></tr>";
$addURLS = 'google.com';
if (($addURLS) != '') {
$message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . $addURLS . "</td></tr>";
}
$curText = 'dummy text';
if (($curText) != '') {
$message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
}
$message .= "<tr><td><strong>NEW Content:</strong> </td><td>New Text</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if(mail($to,$subject,$message,$headers))
{
echo "Mail Send Sucuceed";
}
else{
echo "Mail Send Failed";
}
?>
回答by uyghurbeg
I tried many ways to send a mail from XAMPP Localhost, but since XAMPP hasn't SSL Certificate, my email request blocked by Gmail or similar SMTP Service providers.
我尝试了很多方法从 XAMPP Localhost 发送邮件,但由于 XAMPP 没有 SSL 证书,我的电子邮件请求被 Gmail 或类似的 SMTP 服务提供商阻止。
Then I used MailHog for local smtp server, what you need to do is just run it. localhost:1025 is for smtp server, localhost:8025 is for mail server, where you can check the emails you sent.
然后我使用MailHog作为本地smtp服务器,你需要做的就是运行它。localhost:1025 为 smtp 服务器,localhost:8025 为邮件服务器,可以查看发送的邮件。
here is my code:
这是我的代码:
require_once "src/PHPMailer.php";
require_once "src/SMTP.php";
require_once "src/Exception.php";
$mail = new PHPMailer\PHPMailer\PHPMailer();
//Server settings
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'localhost'; // Set the SMTP server to send through
$mail->Port = 1025; // TCP port to connect to
// $mail->Username = ''; // SMTP username
// $mail->Password = ''; // SMTP password
// $mail->SMTPAuth = true; // Enable SMTP authentication
// $mail->SMTPSecure = 'tls'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
回答by ashkan nasirzadeh
in addition to all answers please note that in sendmail.ini
file:
除了所有答案外,请注意sendmail.ini
文件中的:
auth_password=this-is-Not-your-Gmail-password
auth_password=this- is-不是-your-Gmail-password
due to new google security concern, you should follow these steps to make an application password for this purpose:
由于新的谷歌安全问题,您应该按照以下步骤为此目的设置应用程序密码:
- go to https://accounts.google.com/in security tab
- turn two-step-verification on
- go back to the security tab and make an App-password (in select-app drop-down menu you can choose 'other')
- 在安全选项卡中转到https://accounts.google.com/
- 开启两步验证
- 返回安全选项卡并设置应用程序密码(在选择应用程序下拉菜单中,您可以选择“其他”)
回答by Vishnu R
You have to define an SMTP
server and a port for this. All except like sending mails from live hosts.
您必须为此定义SMTP
服务器和端口。除了像从实时主机发送邮件。
This is a useful link regarding this.
NB: The port should be unused. Please take care that, Some applications like
Skype
uses the default ports and there by prevents sending mail.
注意:该端口应未使用。请注意,某些应用程序(例如
Skype
使用默认端口)会阻止发送邮件。