<?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 :(";
}
?>
9 回答
您可以使用sendmail包从localhost发送邮件,在XAMPP中 Build sendmail包 . 因此,如果您正在使用XAMPP,那么您可以轻松地从localhost发送邮件 .
例如,您可以为gmail配置
C:\xampp\php\php.ini
和c:\xampp\sendmail\sendmail.ini
以发送邮件 .在
C:\xampp\php\php.ini
找到extension=php_openssl.dll
并从该行的开头删除分号,使SSL为ghost for localhost工作 .在php.ini文件中找到
[mail function]
并更改现在打开
C:\xampp\sendmail\sendmail.ini
. 使用以下代码替换sendmail.ini中的所有现有代码现在你做到了!!使用邮件功能创建php文件并从localhost发送邮件 .
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以允许来自安全性较低的应用程序访问 .
在XAMPP v3.2.1中,出于测试目的,您可以看到XAMPP在XAMPP / mailoutput中发送的电子邮件 . 就我而言,在Windows 8上,这不需要任何其他配置,而且是测试电子邮件的简单解决方案
在localhost或本地服务器上发送电子邮件非常简单
注意:我在安装了Xampp的Windows 7 64位上使用测试邮件服务器软件
只需下载测试邮件服务器工具并按照其网站上的说明进行安装Test Mail Server Tool
现在,您只需要在
php.ini
文件下更改两行查找
[mail function]
并删除;smtp = localhost
之前的半冒号在
sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
之前放置半结肠您不需要更改任何其他内容,但如果您仍然没有收到电子邮件而不是检查
SMTP port
,则端口号必须相同 .上述方法适用于Xampp软件提供的默认设置 .
您必须在服务器上配置SMTP . 您可以免费使用Google的G Suite SMTP:
了解更多关于
PHPMailer
here .您可以在没有Internet的PC中测试发送邮件
您应该使用Papercut这个简单的应用程序来测试发送邮件 . 而且您不需要配置任何东西 .
只需运行它并尝试测试发送邮件:
test_sendmail.php
你会看到这个:
我希望你有一个美好的一天 . 你可以在 Youtube 找到我更多教程Piseth Sok
欢呼!
对于Windows 8上的人,如果你想这样做,我真的建议你阅读这个教程我发现:http://yogeshchaugule.com/blog/2013/configure-sendmail-wamp
它不是由我写的,而是在将我的头撞到混凝土墙上2.5小时之后,没有让它与最奇怪的错误一起工作:
连接超时 .
连接正常关闭 .
我终于发现教程安装了https://www.stunnel.org/downloads.html和Stunnel的配置 . 它终于奏效了 .
您必须为此定义
SMTP
服务器和端口 . 除了从直播主机发送邮件之外的所有内容 .This is a useful link regarding this .
只花了一个多小时试图让这项工作 . 对于所有发布不起作用的建议都遇到同样问题的人:你必须在你的XAMPP inrerface中重启Apache!只是重新启动XAMPP不会工作!
如果您已安装xampp最新副本的副本,请查看此链接以获取通过xampp发送电子邮件的完整文档 . 在尝试访问以下链接之前,必须启用apache
http://localhost/dashboard/docs/send-mail.html