SMTP Функция

  1. <?php
  2. $srvRsp = '';
  3. global $srvRsp;
  4. $cnf['smtp_user'] = '[email protected]';
  5. $cnf['smtp_port'] = '465';
  6. $cnf['smtp_host'] = 'ssl://smtp.gmail.com';
  7. $cnf['smtp_password'] = 'test';
  8. $cnf['smtp_charset'] = 'utf-8';
  9. $cnf['smtp_from'] = 'otTester';
  10. function smtpmail($mail_to, $sjt, $message, $h = '') {
  11.  global $cnf;
  12.  $s = "Date: " . date("D, d M Y H:i:s") . " UT\r\n";
  13.  $s .= 'Subject: =?' . $cnf['smtp_charset'] . '?B?' . base64_encode($sjt) . "=?=\r\n";
  14.  if ($h) {
  15.  $s .= $h . "\r\n\r\n";
  16.  } else {
  17.  $s .= "Reply-To: " . $cnf['smtp_user'] . "\r\n";
  18.  $s .= "MIME-Version: 1.0\r\n";
  19.  $s .= "Content-Type: text/plain; charset=\"" . $cnf['smtp_charset'] . "\"\r\n";
  20.  $s .= "Content-Transfer-Encoding: 8bit\r\n";
  21.  $s .= "From: \"" . $cnf['smtp_from'] . "\" <" . $cnf['smtp_user'] . ">\r\n";
  22.  $s .= "To: $mail_to <$mail_to>\r\n";
  23.  $s .= "X-Priority: 3\r\n\r\n";
  24.  }
  25.  $s .= $message . "\r\n";
  26.  if (!$sck = fsockopen($cnf['smtp_host'], $cnf['smtp_port'], $err, $errstr, 30)) {
  27.  if ($cnf['smtp_debug']) {
  28.  echo $err . "&lt;br&gt;" . $errstr;
  29.  }
  30.  return 0;
  31.  }
  32.  
  33.  if (!server_parse($sck, "220", __LINE__)) {
  34.  return 0;
  35.  }
  36.  fputs($sck, "HELO " . $cnf['smtp_host'] . "\r\n");
  37.  if (!server_parse($sck, "250", __LINE__)) {
  38.  fclose($sck);
  39.  return 0;
  40.  }
  41.  fputs($sck, "AUTH LOGIN\r\n");
  42.  if (!server_parse($sck, "334", __LINE__)) {
  43.  fclose($sck);
  44.  return 0;
  45.  }
  46.  fputs($sck, base64_encode($cnf['smtp_user']) . "\r\n");
  47.  if (!server_parse($sck, "334", __LINE__)) {
  48.  fclose($sck);
  49.  return 0;
  50.  }
  51.  fputs($sck, base64_encode($cnf['smtp_password']) . "\r\n");
  52.  if (!server_parse($sck, "235", __LINE__)) {
  53.  fclose($sck);
  54.  return 0;
  55.  }
  56.  fputs($sck, "MAIL FROM: <" . $cnf['smtp_user'] . ">\r\n");
  57.  if (!server_parse($sck, "250", __LINE__)) {
  58.  fclose($sck);
  59.  return 0;
  60.  }
  61.  fputs($sck, "RCPT TO: <" . $mail_to . ">\r\n");
  62.  if (!server_parse($sck, "250", __LINE__)) {
  63.  fclose($sck);
  64.  return 0;
  65.  }
  66.  fputs($sck, "DATA\r\n");
  67.  if (!server_parse($sck, "354", __LINE__)) {
  68.  fclose($sck);
  69.  return 0;
  70.  }
  71.  fputs($sck, $s . "\r\n.\r\n");
  72.  
  73.  if (!server_parse($sck, "250", __LINE__)) {
  74.  fclose($sck);
  75.  return 0;
  76.  }
  77.  fputs($sck, "QUIT\r\n");
  78.  fclose($sck);
  79.  return TRUE;
  80. }
  81.  
  82. function server_parse($sck, $response, $line = __LINE__) {
  83.  global $cnf;
  84.  while (@substr($srvRsp, 3, 1) != ' ') {
  85.  
  86.  if (!($srvRsp = fgets($sck, 256))) {
  87.  return 0;
  88.  }
  89.  }
  90.  
  91.  if (!(substr($srvRsp, 0, 3) == $response)) {
  92.  return 0;
  93.  }
  94.  return true;
  95. }
  96.  
  97. //юзать так:
  98. if (smtpmail("[email protected]", "тема", "sms")) {
  99.  echo 'sndd';
  100. }
Надеюсь влезет. Выпилил дебаг. Гдето стырил и чуть допилил. ;-)
Фулл версия http://pastebin.com/8J0P02Vh

Реклама

Мы в соцсетях

tw tg yt gt