SMTP Функция
- <?php
- $srvRsp = '';
- global $srvRsp;
- $cnf['smtp_port'] = '465';
- $cnf['smtp_host'] = 'ssl://smtp.gmail.com';
- $cnf['smtp_password'] = 'test';
- $cnf['smtp_charset'] = 'utf-8';
- $cnf['smtp_from'] = 'otTester';
- function smtpmail($mail_to, $sjt, $message, $h = '') {
- global $cnf;
- $s = "Date: " . date("D, d M Y H:i:s") . " UT\r\n";
- $s .= 'Subject: =?' . $cnf['smtp_charset'] . '?B?' . base64_encode($sjt) . "=?=\r\n";
- if ($h) {
- $s .= $h . "\r\n\r\n";
- } else {
- $s .= "Reply-To: " . $cnf['smtp_user'] . "\r\n";
- $s .= "MIME-Version: 1.0\r\n";
- $s .= "Content-Type: text/plain; charset=\"" . $cnf['smtp_charset'] . "\"\r\n";
- $s .= "Content-Transfer-Encoding: 8bit\r\n";
- $s .= "From: \"" . $cnf['smtp_from'] . "\" <" . $cnf['smtp_user'] . ">\r\n";
- $s .= "To: $mail_to <$mail_to>\r\n";
- $s .= "X-Priority: 3\r\n\r\n";
- }
- $s .= $message . "\r\n";
- if (!$sck = fsockopen($cnf['smtp_host'], $cnf['smtp_port'], $err, $errstr, 30)) {
- if ($cnf['smtp_debug']) {
- echo $err . "<br>" . $errstr;
- }
- return 0;
- }
- if (!server_parse($sck, "220", __LINE__)) {
- return 0;
- }
- fputs($sck, "HELO " . $cnf['smtp_host'] . "\r\n");
- if (!server_parse($sck, "250", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, "AUTH LOGIN\r\n");
- if (!server_parse($sck, "334", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, base64_encode($cnf['smtp_user']) . "\r\n");
- if (!server_parse($sck, "334", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, base64_encode($cnf['smtp_password']) . "\r\n");
- if (!server_parse($sck, "235", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, "MAIL FROM: <" . $cnf['smtp_user'] . ">\r\n");
- if (!server_parse($sck, "250", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, "RCPT TO: <" . $mail_to . ">\r\n");
- if (!server_parse($sck, "250", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, "DATA\r\n");
- if (!server_parse($sck, "354", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, $s . "\r\n.\r\n");
- if (!server_parse($sck, "250", __LINE__)) {
- fclose($sck);
- return 0;
- }
- fputs($sck, "QUIT\r\n");
- fclose($sck);
- return TRUE;
- }
- function server_parse($sck, $response, $line = __LINE__) {
- global $cnf;
- while (@substr($srvRsp, 3, 1) != ' ') {
- if (!($srvRsp = fgets($sck, 256))) {
- return 0;
- }
- }
- if (!(substr($srvRsp, 0, 3) == $response)) {
- return 0;
- }
- return true;
- }
- //юзать так:
- echo 'sndd';
- }
Надеюсь влезет. Выпилил дебаг. Гдето стырил и чуть допилил.
Фулл версия http://pastebin.com/8J0P02Vh
Фулл версия http://pastebin.com/8J0P02Vh