如何安装和配置 SMTP 服务器?

HaV*_*VoK 6 php email postfix smtp 12.04

如何设置 SMTP 服务器以将电子邮件发送为 *@mydomain.com?

然后,我如何将这个 SMTP 服务器与 PHP 脚本一起使用,该脚本从我的网站的联系表单中获取发布的数据并将其作为电子邮件发送给我?

我使用的是 Ubuntu 13.10 和 webmin,但我可以在服务器本地进行更改。

这是我的 PHP 脚本..

#!/var/www/php
<?php

 if(isset($_POST['email'])) {





$email_to = "havok@havoksecurity.com";

$email_subject = "havok security contact form";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there was an error found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['message'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['message']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

 if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {

$error_message .= 'The Name you entered does not appear to be valid.<br />';

}


if(strlen($message) < 2) {

$error_message .= 'The Message you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "Name: ".clean_string($name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Phone: ".clean_string($phone)."\n";

$email_message .= "Message: ".clean_string($message)."\n";





   // create email headers

   $headers = 'From: '.$email_from."\r\n".

  'Reply-To: '.$email_from."\r\n" .

  'X-Mailer: PHP/' . phpversion();

  @mail($email_to, $email_subject, $email_message, $headers);  

  ?>



    <!-- include your own success html here -->
    <link rel="stylesheet" type="text/css" media="screen" href="css/reset.css">
    <link rel="stylesheet" type="text/css" media="screen" href="css/style.css">
    <script src="js/jquery-1.7.min.js"></script>
    <script src="js/jquery.easing.1.3.js"></script>
    <script src="js/FF-cash.js"></script>
    <p>&nbsp;</p>
    <p><img src="images/security_logo.png" width="596" height="190" alt="security_logo" longdesc="http://havoksecurity.com" /><img src="images/twitter_icon-1.png" width="24" height="24" alt="twitter_icon" /><img src="images/facebook_icon-2.png" width="24" height="24" alt="facebook_icon" /><img src="images/google_icon-3.png" width="24" height="24" alt="google_icon" /></p>
    <p>&nbsp;</p>
    <p>Thank you for contacting us. We will respond within 24 hours. 
    <?php

    }

    ?>
    </p>
Run Code Online (Sandbox Code Playgroud)

我已经在全新安装的 Ubuntu 12.4 上安装了 iRedMail,但它不会发送或接收电子邮件。所以看来我必须先弄清楚这一点,然后才能使用这个 php 脚本。所有帮助将不胜感激。

我只是尝试顶部发送一条消息,所以我可以看到这里的错误是错误日志

Feb  5 00:53:27 havoksecurity postfix/smtpd[8193]: connect from unknown[192.168.1.126]
Feb  5 00:53:53 havoksecurity postfix/smtpd[8193]: lost connection after UNKNOWN from unknown[192.168.1.126]
Feb  5 00:53:53 havoksecurity postfix/smtpd[8193]: disconnect from unknown[192.168.1.126]
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max connection rate 1/60s for (smtp:192.168.1.126) at Feb  5 00:53:27
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max connection count 1 for (smtp:192.168.1.126) at Feb  5 00:53:27
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max cache size 1 at  Feb  5 00:53:27
Feb  5 01:10:45 havoksecurity postfix/pickup[7985]: DDEACD40FD2: uid=0 from=<havok@havoksecurity.com>
Feb  5 01:10:45 havoksecurity postfix/cleanup[9202]: DDEACD40FD2: message-id=<1391580645.9181@havoksecurity.com>
Feb  5 01:10:45 havoksecurity postfix/qmgr[1687]: DDEACD40FD2: from=<havok@havoksecurity.com>, size=641, nrcpt=1 (queue active)
Feb  5 01:10:46 havoksecurity amavis[1412]: (01412-06) NOTICE: reconnecting in response to: err=2006, HY000, DBD::mysql::st execute failed: MySQL server has gone away at (eval 100) line 166, <GEN37> line 1834.
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: connect from localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity postfix/trivial-rewrite[9204]: warning: do not list domain havoksecurity.com in BOTH mydestination and virtual_mailbox_domains
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: 527E6D40F94: client=localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity postfix/cleanup[9202]: 527E6D40F94: message-id=<1391580645.9181@havoksecurity.com>
Feb  5 01:10:47 havoksecurity postfix/qmgr[1687]: 527E6D40F94: from=<havok@havoksecurity.com>, size=1098, nrcpt=1 (queue active)
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: disconnect from localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity amavis[1412]: (01412-06) Passed CLEAN, MYUSERS <havok@havoksecurity.com> -> <user@gmail.com>, Message-ID: <1391580645.9181@havoksecurity.com>, mail_id: tx8Bn4bMXniG, Hits: -10, size: 641, queued_as: 527E6D40F94, 1404 ms
Feb  5 01:10:47 havoksecurity postfix/smtp[9207]: DDEACD40FD2: to=<user@gmail.com>, relay=127.0.0.1[127.0.0.1]:10024, delay=1.6, delays=0.09/0.01/0/1.5, dsn=2.0.0, status=sent (250 2.0.0 from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 527E6D40F94)
Feb  5 01:10:47 havoksecurity postfix/qmgr[1687]: DDEACD40FD2: removed
Feb  5 01:11:17 havoksecurity postfix/smtp[9219]: connect to gmail-smtp-in.l.google.com[74.125.196.27]:25: Connection timed out
Feb  5 01:11:47 havoksecurity postfix/smtp[9219]: connect to alt1.gmail-smtp-in.l.google.com[173.194.75.27]:25: Connection timed out
Feb  5 01:12:17 havoksecurity postfix/smtp[9219]: connect to alt2.gmail-smtp-in.l.google.com[173.194.78.27]:25: Connection timed out
Feb  5 01:12:47 havoksecurity postfix/smtp[9219]: connect to alt3.gmail-smtp-in.l.google.com[74.125.136.27]:25: Connection timed out
Feb  5 01:13:17 havoksecurity postfix/smtp[9219]: connect to alt4.gmail-smtp-in.l.google.com[173.194.70.27]:25: Connection timed out
Feb  5 01:13:17 havoksecurity postfix/smtp[9219]: 527E6D40F94: to=<user@gmail.com>, relay=none, delay=150, delays=0.04/0.02/150/0, dsn=4.4.1, status=deferred (connect to alt4.gmail-smtp-in.l.google.com[173.194.70.27]:25: Connection timed out)
Run Code Online (Sandbox Code Playgroud)

小智 4

我建议您考虑使用Postfix 之类的东西作为您的电子邮件服务器。这将允许您发送电子邮件。

请确保,如果您想要运行完整的电子邮件服务器(具有入站流量),请设置 DNS 区域文件来处理入站邮件流量。

对于您所指的 PHP 脚本...您的意思是您想要从表单之类的东西发送电子邮件,其中表单数据作为电子邮件的一部分,也许还有一些其他默认信息?