Setting SMTP details for php mail () function(为 php mail() 函数设置 SMTP 详细信息)
问题描述
我一直在寻找答案并尝试了很多方法来解决这个问题.
I have been looking for an answer and tried many things to this problem.
我的脚本在我的虚拟主机上运行良好,但是当将其移动到其他专用服务器时,邮件永远不会送达.现在我需要设置 SMTP 服务器,但不正确.
My script works fine on my webhost but when moving it to an other dedicated server the mail never gets delivered. Now i need to set the SMTP server but don't get it right.
顺便说一句,使用 Gmail 应用程序.这就是代码的样子.
Using Gmail apps btw. This is how the code looks like.
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("@",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','telephone','message');
$required = array('name','email','telephone','message');
$your_email = "xxx@example.com";
$email_subject = "New Messag: ".$_POST['subject'];
$email_content = "New message:
";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'telephone') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."
";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "user@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
?>
那么我该如何正确设置 SMTP 设置呢?
So how do i set the SMTP settings right?
推荐答案
仅适用于 Windows: 您可以尝试使用 ini_set() 函数Docs 用于 SMTPDocs 和 smtp_port文档设置:
Under Windows only: You may try to use ini_set() functionDocs for the SMTPDocs and smtp_portDocs settings:
ini_set('SMTP', 'mysmtphost');
ini_set('smtp_port', 25);
这篇关于为 php mail() 函数设置 SMTP 详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 php mail() 函数设置 SMTP 详细信息
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
