Hello.

I'm trying to make some interlayer for letters on php 7.0, in order to subject them to a little processing and most importantly - to limit outgoing letters. In php.ini, I redirected the letters to my file, where I opened the stdin stream and took the necessary data, entered it into the table, counted it. Now I need to otppavit them back, and the same stream to send further to sendmail. I googled it for a very long time and have not yet found a solution.

PS I do everything locally in the docker, I don’t have a mail server, I have Postfix (in fact, I can send it to postfix, but then I don’t even understand how), maybe later I will be setting up all this on the server.

PSS I do not need an analogous solution to the whole problem, I need to do just that.

Here is a cut-off part of the code, without logic and other things, the most necessary for understanding the question.

#!/usr/bin/php <?php $stream = ''; $fp = fopen('php://stdin','r'); while($t=fread($fp,2048)) { if( $t===chr(0) ) break; $stream .= $t; } //$count_mails - количСство писСм с ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠ³ΠΎ Π΄ΠΎΠΌΠ΅Π½Π°. if($count_mails > 50){ exit(0); } //Π²ΠΎΡ‚ здСсь Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Ρ‡Ρ‚ΠΎ ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ письма дальшС послС всСх изысканий. 

    2 answers 2

    Hello, the solution was the line: exec ("echo '{$ stream}' | sendmail -i -t");

    Ie we sent on the stream to sendmail, where it actually went. I caught the letters to protest, using the fake mail server, as soon as I got the idea to deliver it, using a test and a test, the solution was found quickly.

      I usually use a ready-made library, since the mail () function is no longer relevant, since emails from left servers are not even sent to Google mail in spam

       // $mailSMTP = new SendMailSmtpClass('Π»ΠΎΠ³ΠΈΠ½', 'ΠΏΠ°Ρ€ΠΎΠ»ΡŒ', 'хост', 'имя отправитСля'); // $result = $mailSMTP->send('ΠšΠΎΠΌΡƒ письмо', 'Π’Π΅ΠΌΠ° письма', 'ВСкст письма', 'Π—Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ письма'); 

      but the library itself

       <?php /** * SendMailSmtpClass * * Класс для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ писСм Ρ‡Π΅Ρ€Π΅Π· SMTP с Π°Π²Ρ‚ΠΎΡ€ΠΈΠ·Π°Ρ†ΠΈΠ΅ΠΉ * ΠœΠΎΠΆΠ΅Ρ‚ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Ρ‡Π΅Ρ€Π΅Π· SSL ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ» * Π’Π΅ΡΡ‚ΠΈΡ€ΠΎΠ²Π°Π»ΠΎΡΡŒ Π½Π° ΠΏΠΎΡ‡Ρ‚ΠΎΠ²Ρ‹Ρ… сСрвСрах yandex.ru, mail.ru ΠΈ gmail.com * * @author Ipatov Evgeniy <admin@ipatov-soft.ru> * @version 1.0 */ class SendMailSmtpClass { /** * * @var string $smtp_username - Π»ΠΎΠ³ΠΈΠ½ * @var string $smtp_password - ΠΏΠ°Ρ€ΠΎΠ»ΡŒ * @var string $smtp_host - хост * @var string $smtp_from - ΠΎΡ‚ ΠΊΠΎΠ³ΠΎ * @var integer $smtp_port - ΠΏΠΎΡ€Ρ‚ * @var string $smtp_charset - ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ° * */ public $smtp_username; public $smtp_password; public $smtp_host; public $smtp_from; public $smtp_port; public $smtp_charset; public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = "utf-8") { $this->smtp_username = $smtp_username; $this->smtp_password = $smtp_password; $this->smtp_host = $smtp_host; $this->smtp_from = $smtp_from; $this->smtp_port = $smtp_port; $this->smtp_charset = $smtp_charset; } /** * ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° письма * * @param string $mailTo - ΠΏΠΎΠ»ΡƒΡ‡Π°Ρ‚Π΅Π»ΡŒ письма * @param string $subject - Ρ‚Π΅ΠΌΠ° письма * @param string $message - Ρ‚Π΅Π»ΠΎ письма * @param string $headers - Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ письма * * @return bool|string Π’ случаи ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π²Π΅Ρ€Π½Π΅Ρ‚ true, ΠΈΠ½Π°Ρ‡Π΅ тСкст ошибки * */ function send($mailTo, $subject, $message, $headers) { $contentMail = "Date: " . date("D, d MYH:i:s") . " UT\r\n"; $contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?' . base64_encode($subject) . "=?=\r\n"; $contentMail .= $headers . "\r\n"; $contentMail .= $message . "\r\n"; try { if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){ throw new Exception($errorNumber.".".$errorDescription); } if (!$this->_parseServer($socket, "220")){ throw new Exception('Connection error'); } $server_name ='test' /*$_SERVER["SERVER_NAME"]*/; fputs($socket, "HELO $server_name\r\n"); if (!$this->_parseServer($socket, "250")) { fclose($socket); throw new Exception('Error of command sending: HELO'); } fputs($socket, "AUTH LOGIN\r\n"); if (!$this->_parseServer($socket, "334")) { fclose($socket); throw new Exception('Autorization error'); } fputs($socket, base64_encode($this->smtp_username) . "\r\n"); if (!$this->_parseServer($socket, "334")) { fclose($socket); throw new Exception('Autorization error'); } fputs($socket, base64_encode($this->smtp_password) . "\r\n"); if (!$this->_parseServer($socket, "235")) { fclose($socket); throw new Exception('Autorization error'); } fputs($socket, "MAIL FROM: <".$this->smtp_username.">\r\n"); if (!$this->_parseServer($socket, "250")) { fclose($socket); throw new Exception('Error of command sending: MAIL FROM'); } $mailTo = ltrim($mailTo, '<'); $mailTo = rtrim($mailTo, '>'); fputs($socket, "RCPT TO: <" . $mailTo . ">\r\n"); if (!$this->_parseServer($socket, "250")) { fclose($socket); throw new Exception('Error of command sending: RCPT TO'); } fputs($socket, "DATA\r\n"); if (!$this->_parseServer($socket, "354")) { fclose($socket); throw new Exception('Error of command sending: DATA'); } fputs($socket, $contentMail."\r\n.\r\n"); if (!$this->_parseServer($socket, "250")) { fclose($socket); throw new Exception("E-mail didn't sent"); } fputs($socket, "QUIT\r\n"); fclose($socket); } catch (Exception $e) { return $e->getMessage(); } return true; } private function _parseServer($socket, $response) { while (@substr($responseServer, 3, 1) != ' ') { if (!($responseServer = fgets($socket, 256))) { return false; } } if (!(substr($responseServer, 0, 3) == $response)) { return false; } return true; } }