duanchi19820419 2018-09-01 09:43
浏览 328

如何使用已安装的mailutils(postfix)配置AWS EC2服务器Ubuntu 16.04以使用php表单操作从mysite发送电子邮件?

I have Amazon EC2 server with Ubuntu 16.04. I was set up my project site with a free domain by Freenom.com and free SSL by Cloudflare.com - my site works well. On server in inbound rules option I added these protocol types rules: 'http=80', 'https=443', 'ssh=22', 'smtp=25'. I installed 'mailutils', configured 'postfix' and other stuff, after that in terminal I can send emails to any email address, but my form action on PHP script doesn't send emails from my site... Example of site page form: `

if (isset($_POST["name"])) {
    $name = htmlspecialchars($_POST["name"]);
    $error_name = 1;
}
else {
    $name = '';     
}

if (isset($_POST["phone"])) {
    $telefone = htmlspecialchars($_POST["phone"]);
    $error_tel = 1;
}
else {
    $telefone = 0;      
}

if(empty($name)) {
    $error_name = -1;
}

if ($telefone == 0 || !intval($telefone))
{
    $error_tel = -1;
}

if ($error_name == -1 || $error_tel == -1) $mail_sent = 0;
else {

    $sender = "root@mysite.com"; //senders name Сервер-отправитель
    $recipient = "recipient@mail.com"; //recipient получатель письма (Вы)
    $mail_body = "Name: " . $name; //name отправителя
    $phone_body = "Mobile phone: " . $telefone; //phone body отправителя
    $subject = "Order from " . $name; //subject тема письма и от кого (ФИО)
    $header = "From: ". $sender . " 
"; //optional headerfields

    ini_set('SMTP', 'localhost');

    if(mail($recipient, $subject, $mail_body, $phone_body, $header)) {
        $mail_sent = 1;
    } else $mail_sent = -1;


}

?>`

`

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>MySite.com</title>

<div>
  <form action="" method="post">                
    <label>
        <input type="text" name="name" placeholder="Your name"/>
        <span>
          <?php
                    if ($error_name == -1) echo "<h5><span style = 'color: red;'>Name?</span></h5>";
                    else if ($error_tel == -1) echo "<h5><span style = 'color: red;'>Type name again!</span></h5>";
                ?>      
              </span>
    </label>
    <label>
        <input type="tel" name="phone" placeholder="tel: 701-1234567" pattern="[0-9]{3}-[0-9]{7}"/>
        <span>
            <?php
                     if ($error_tel == -1) echo "<h5><span style = 'color: red;'>Your phone?</span></h5>";
                     else if ($error_name == -1) echo "<h5><span style = 'color: red;'>Type phone again!</span></h5>";
                  ?>
        </span>
    </label>
    <button>Call back</button>
    <?php
            if ($mail_sent == 1) echo "<h5><span style = 'color: blue;'>Thank you, we call you soon!</span></h5>";
            else if ($mail_sent == -1) echo "<h5><span style = 'color: red;'>Some error with emailing, try to call us!</span></h5>
                <h4>8-701-1234567</h4>";
        ?>                           
  </form>
</div>

`

Steps of installing postfix on server:

  1. I was set up 'mailutils' to the server through SSH with this command: "sudo apt-get install mailutils".

    • Postfix Configuration: Choosed 'Internet site' (Will be General type of mail configuration). Typed my domain as the System mail name of my server.
  2. Then made changes in file 'main.cf' by command: "sudo nano /etc/postfix/main.cf"

    • Found 'myhostname' line and typed my domain name like this: "myhostname = mysite.com".
    • Found 'mydestination' line and deleted all execpt this: "mydestination =".
    • Found 'inet_interfaces' line and typed this: "inet_interfaces = loopback-only".
  3. Also changed my host name by command: "sudo nano /etc/hostname".

    • Typed my site domain.
  4. Then created file by command: "sudo nano /etc/postfix/virtual".

    • Typed my mail address as root and postmaster to send all emails from server to my mail address: root mymailaddress@yandex.ru postmaster mymailaddress@yandex.ru
  5. Then added to main.cf file (to map file with the email address where to send root's inbox emails) by command: "sudo nano /etc/postfix/main.cf".

    • Added these line of text in the end of the file: "virtual_alias_maps = hash:/etc/postfix/virtual".
  6. Transformed 'virtual' file for postfix by command: "sudo postmap /etc/postfix/virtual".

  7. Rebooted my host server by command: "sudo reboot".

After that checked mail sending from terminal by these command: 1. echo "Hello men!" | mail -s "Hi subject!" anyemailaddress@ya.ru or 2. sendmail myemail@gmail.com and it's work, but when I'm trying to send email using my site's PHP form - it does not send emails...!

Then I tried edit php.ini file by command: "sudo nano /opt/lampp/etc/php.ini". - found line 'sendmail_path' and wrote this: sendmail_path = "/usr/sbin/sendmail -t -i -f"

Is any body know why it is not work? Which configuration files of the server I need to edit to get my PHP form sending emails?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题