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; } }