为 php mail() 函数设置 SMTP 详细信息

发布于 2024-11-09 02:30:19 字数 1711 浏览 3 评论 0原文

我一直在寻找答案并尝试了很多方法来解决这个问题。

我的脚本在我的虚拟主机上运行良好,但是当将其移动到其他专用服务器时,邮件永远不会被送达。现在我需要设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-11-16 02:30:19

仅在Windows下:您可以尝试使用ini_set()函数SMTP文档文档smtp_port文档设置:

ini_set('SMTP', 'mysmtphost'); 
ini_set('smtp_port', 25); 

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); 
晨敛清荷 2024-11-16 02:30:19

检查你的 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

唔猫 2024-11-16 02:30:19

尝试从您的专用服务器通过端口 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文