php 为php mail()函数设置SMTP详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6093976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting SMTP details for php mail () function
提问by inpbox
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.
我的脚本在我的虚拟主机上运行良好,但是当将其移动到其他专用服务器时,邮件永远不会送达。现在我需要设置 SMTP 服务器,但没有正确设置。
Using Gmail apps btw. This is how the code looks like.
顺便说一句,使用 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
?>
So how do i set the SMTP settings right?
那么如何正确设置 SMTP 设置呢?
回答by heximal
Under Windows only:You may try to use ini_set()
functionDocsfor the SMTP
Docsand smtp_port
Docssettings:
仅在 Windows 下:您可以尝试使用ini_set()
功能文档进行SMTP
文档和smtp_port
文档设置:
ini_set('SMTP', 'mysmtphost');
ini_set('smtp_port', 25);
回答by Bjoern
Check out your php.ini, you can set these values there.
查看您的 php.ini,您可以在那里设置这些值。
Here's the description in the php manual: http://php.net/manual/en/mail.configuration.php
这是php手册中的描述: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
如果你想在你的应用程序中使用几个不同的 SMTP 服务器,我建议使用“更大”的邮件框架,pe Swiftmailer
回答by Tudor Constantin
Try from your dedicated server to telnet to smtp.gmail.com on port 465. It might be blocked by your internet provider
尝试在端口 465 上从您的专用服务器 telnet 到 smtp.gmail.com。它可能被您的互联网提供商阻止