我试图从本地主机发送邮件。 但是我无法从本地主机发送邮件 所以有人能告诉我如何重新配置我的xampp从本地主机发送邮件
当前回答
你必须在你的服务器上配置SMTP。你可以通过谷歌免费使用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的信息。
其他回答
在XAMPP v3.2.1中,出于测试目的,您可以在XAMPP/mailoutput中看到XAMPP发送的电子邮件。就我在Windows 8上的情况而言,这不需要任何额外的配置,是测试电子邮件的简单解决方案
你可以用sendmail包从本地主机发送邮件,sendmail包是XAMPP内置的。 所以如果你使用XAMPP,那么你可以很容易地从本地主机发送邮件。
例如,您可以配置C:\xampp\php\php.ini和C:\xampp\ sendmail\sendmail.ini用于gmail发送邮件。
在C:\xampp\php\php.ini中找到extension=php_openssl.dll,并从该行开始删除分号,以使SSL适用于gmail的localhost。
在php.ini文件中找到[邮件函数]并更改
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
现在你做到了!!创建带有邮件功能的PHP文件,并从本地主机发送邮件。
PS:不要忘记在上面的代码中替换my-gmail-id和my-gmail-password。 此外,如果你从上面复制了设置,不要忘记删除重复的键。例如,如果php.ini文件中有另一个sendmail_path: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe
还记得使用XAMMP控制面板重新启动服务器,以便更改生效。
对于gmail,请检查https://support.google.com/accounts/answer/6010255以允许从不太安全的应用程序访问。
在Linux上发送电子邮件(使用sendmail包)通过Gmail从 使用gmail表单发送邮件 本地主机。
在本地主机或本地服务器上发送电子邮件非常简单
注意:我使用的测试邮件服务器软件在Windows 7 64bit安装Xampp
只需下载测试邮件服务器工具,并根据其网站上测试邮件服务器工具的说明进行安装
现在您只需要更改php.ini文件下的两行
找到[mail function]并删除前面的分号;smtp = localhost sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
您不需要更改任何其他内容,但如果您仍然没有收到电子邮件,则检查SMTP端口,端口号必须相同。
以上方法是针对Xampp软件提供的默认设置。
我尝试了许多方法从XAMPP Localhost发送邮件,但由于XAMPP没有SSL证书,我的电子邮件请求被Gmail或类似的SMTP服务提供商阻止。
然后我使用MailHog本地smtp服务器,你需要做的就是运行它。 Localhost:1025是SMTP服务器, Localhost:8025是邮件服务器,在那里您可以检查您发送的电子邮件。
这是我的代码:
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('testtoo@testto.com', 'Mailer');
$mail->addAddress('testtoo@webbamail.com', '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!";
}
MailHog Github Repository链接
您可以测试发送邮件在您的PC没有互联网
你应该使用剪纸这个简单的应用程序来测试发送邮件。你不需要配置任何东西。
只需运行它,并尝试测试发送邮件:
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上找到我的更多教程
欢呼!
推荐文章
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符
- 在PHP5中创建单例设计模式
- 阻止人们入侵基于php的Flash游戏高分表的最佳方法是什么
- PHP子字符串提取。获取第一个'/'之前的字符串或整个字符串
- __construct函数的作用是什么?
- PHP中的异步shell执行器
- Laravel 5 -如何访问在视图存储上传的图像?
- XAMPP -端口80被PID 4的“无法打开进程”使用!12
- 为什么在PHP中使用sprintf函数?
- “质量分配”在Laravel中是什么意思?
- 在逗号上爆炸字符串,并修剪每个值的潜在空格
- PHP与MySQL 8.0+错误:服务器请求身份验证方法未知的客户端
- laravel5“LIKE”对等物(雄辩的)