为 php mail() 函数设置 SMTP 详细信息
我一直在寻找答案并尝试了很多方法来解决这个问题。
我的脚本在我的虚拟主机上运行良好,但是当将其移动到其他专用服务器时,邮件永远不会被送达。现在我需要设置 SMTP 服务器,但没有设置正确。
顺便说一句,使用 Gmail 应用程序。这就是代码的样子。
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/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 = "[email protected]";
$email_subject = "New Messag: ".$_POST['subject'];
$email_content = "New message:\n";
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]."\n";
}
}
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 = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
?>
那么如何正确设置 SMTP 设置呢?
I have been looking for an answer and tried many things to this problem.
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.
Using Gmail apps btw. This is how the code looks like.
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/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 = "[email protected]";
$email_subject = "New Messag: ".$_POST['subject'];
$email_content = "New message:\n";
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]."\n";
}
}
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 = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
?>
So how do i set the SMTP settings right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅在Windows下:您可以尝试使用
ini_set()
函数SMTP
文档文档和smtp_port
文档设置:Under Windows only: You may try to use
ini_set()
functionDocs for theSMTP
Docs andsmtp_port
Docs settings:检查你的 php.ini,你可以在那里设置这些值。
php手册中的描述如下:
http://php.net/manual/en/mail.configuration.php
如果您想在应用程序中使用多个不同的 SMTP 服务器,我建议使用“更大”的邮件框架,例如 Swiftmailer
Check out your php.ini, you can set these values there.
Here's the description in the php manual:
http://php.net/manual/en/mail.configuration.php
If you want to use several different SMTP servers in your application, I recommend using a "bigger" mailing framework, p.e. Swiftmailer
尝试从您的专用服务器通过端口 465 telnet 到 smtp.gmail.com。它可能被您的互联网提供商阻止
Try from your dedicated server to telnet to smtp.gmail.com on port 465. It might be blocked by your internet provider