我试图从本地主机发送邮件。 但是我无法从本地主机发送邮件 所以有人能告诉我如何重新配置我的xampp从本地主机发送邮件
当前回答
此代码用于从本地主机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文件中的Change
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文件中写爱的代码来发送电子邮件
<?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";
}
?>
其他回答
你必须在你的服务器上配置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的信息。
您必须为此定义一个SMTP服务器和端口。除了从实时主机发送邮件。
这是一个有用的链接。
注意:该端口应未使用。请小心点,Some 像Skype这样的应用程序使用默认端口和阻止 发送邮件。
此代码用于从本地主机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文件中的Change
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文件中写爱的代码来发送电子邮件
<?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";
}
?>
你可以用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表单发送邮件 本地主机。
我花了一个多小时才搞定。对于每个人都有同样的问题,所有的建议张贴不工作:你必须重新启动Apache在你的XAMPP inrerface!只是重新启动XAMPP行不通!!
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?