我正在尝试从本地主机发送邮件。 但是我无法从本地主机发送邮件, 所以有人可以告诉我如何重新配置我的xampp以从本地主机发送邮件吗?
C:\ xampp \ php \ php.ini
和c:\ xampp \ sendmail \ sendmail.ini
用于gmail发送邮件。C:\ xampp \ php \ php.ini
中查找extension = php_openssl.dll
并从该行开头删除分号,以使SSL对于本地主机上的gmail工作。[mail function]
并更改SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
现在打开 C:\xampp\sendmail\sendmail.ini
文件。用以下代码替换 sendmail.ini 文件中的所有现有代码。
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com
sendmail_path =“C:\ xampp \ mailtodisk \ mailtodisk.exe”
,请注释以下行。c:\xampp\php\php.ini
文件,找到 [mail function]
部分,并取消注释提到 mailtodisk.exe
的那一行。除了 mail.add_x_header
这一行之外,请保持该部分中的其他所有行都被注释掉。 - Tim Malonesendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
。 - Tim Malone您可以使用Papercut这个简单的应用程序来测试发送邮件,而且您无需配置任何内容。
只需运行它并尝试测试发送邮件:
test_sendmail.php
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
然后你会看到这个:
希望你有美好的一天。 你可以在Youtube上找到我,获取更多教程:Piseth Sok
加油!
在本地主机或本地服务器上发送电子邮件非常简单。
注意:我正在使用安装了Xampp的Windows 7 64位操作系统上的测试邮件服务器软件。
只需下载测试邮件服务器工具,并根据其网站上给出的说明进行安装Test Mail Server Tool
现在,您只需要在php.ini
文件下更改两行内容即可。
[mail function]
并删除分号,该分号位于;smtp = localhost
之前sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
之前放置分号您不需要更改其他任何内容,但如果您仍然无法接收电子邮件,请检查SMTP端口
,端口号必须相同。
以上方法适用于Xampp软件提供的默认设置。
您需要在您的服务器上配置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 = "your-gmail-account@gmail.com"; // 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 :(";
}
?>
了解有关PHPMailer
的更多信息,请在此处阅读。
根据我的个人经验,我发现与Vikas Dwivedi的答案非常相似的方法也可以很好地解决问题。
步骤1(php.ini文件)
在位于xampp\php\php.ini
的php.ini文件中。将设置更改为以下内容:
extension=php_openssl.dll
[mail function]
sendmail_path =":\xampp7\sendmail\sendmail.exe -t"
mail.add_x_header=On
通过在其他邮件mail funciton
前加上;
来关闭它们,例如:;smtp_port=25
步骤2(sendmail.ini文件)
在位于xampp\sendmail\sendmail.ini的文件中,将如下更改:
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto
auth_username=address@gmail.com
auth_password=YourPassword
第三步(代码)
创建一个php文件,并使用以下代码:
<?php
mail($to, "subject", "body", "From: ".$from);
?>
注意
sendmail.ini
文件中:
由于谷歌的新安全问题,您需要按照以下步骤为此目的创建一个应用程序密码:auth_password=this-is-Not-your-Gmail-password
这段代码用于从您的本地 XAMPP 和 Gmail 账户发送邮件。
以下内容更改 php.ini 文件
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = your@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
extension=php_openssl.dll
修改sendmail.ini文件如下
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=yourmail@gmail.com
auth_password=your-gmail-password
force_sender=yourmail@gmail.com
<?php
$to = "tomail@gmail.com";
$subject = "Test Mail";
$headers = "From: from_mail@gmail.com\r\n";
$headers .= "Reply-To: replytomail@gmail.com\r\n";
$headers .= "CC: theassassin.edu@gmail.com\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";
}
?>
刚刚花了一个多小时尝试让它工作。对于所有遇到相同问题的人,所有已发布的建议都不起作用:您必须在XAMPP界面中重新启动Apache!仅重新启动XAMPP是不起作用的!!
您需要定义一个SMTP
服务器和端口。除了像从活动主机发送邮件之外的所有内容。
NB:端口应该是未使用的。请注意,一些应用程序(如
Skype
)使用默认端口,从而阻止发送邮件。