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:
-
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.
-
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".
-
Also changed my host name by command: "sudo nano /etc/hostname".
- Typed my site domain.
-
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
-
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".
Transformed 'virtual' file for postfix by command: "sudo postmap /etc/postfix/virtual".
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?