Guest User

Untitled

a guest
Jan 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. /**
  2.  * smtp mail
  3.  *
  4.  * @param $server smtp server
  5.  * @param $port smtp port
  6.  * @param $login smtp username
  7.  * @param $password smtp user password
  8.  * @param $to recipient(s) (user@domain[, ...])
  9.  * @param $from sender address
  10.  * @param $subject email subject
  11.  * @param $msg email text
  12.  * @param $html true if text is html, else false
  13.  * @param $file file(s) (array(array(content type, file name, file contents)[, ...]))
  14.  * @return boolean
  15.  */
  16.  
  17. function smtpmail($server, $port, $login, $password, $to, $from, $subject, $msg, $html = false, $file = array())
  18. {
  19.   $ssock = fsockopen($server, $port, $errno, $errstr, 30);
  20.   if (!$ssock) return false;
  21.  
  22.   // recipients
  23.   $to = explode(',', $to);
  24.   foreach ($to as $k => $v) {
  25.     preg_match('/[^@\s\<]+@[^@\s\>]+/', $to[$k], $match);
  26.     if (!$match[0]) unset($to[$k]);
  27.     else $to[$k] = $match[0];
  28.   }
  29.   if (empty($to)) return false;
  30.  
  31.   // mail from
  32.   preg_match('/[^@\s\<]+@[^@\s\>]+/', $from, $match);
  33.   if (!$match[0]) return false;
  34.   $from = $match[0];
  35.  
  36.   // initiating
  37.   fputs($ssock, '');
  38.   fgets($ssock, 1024);
  39.   fputs($ssock, sprintf("HELO %s\r\n", $_SERVER['HTTP_HOST']));
  40.   fgets($ssock, 1024);
  41.  
  42.   // authorizing
  43.   fputs($ssock, "AUTH LOGIN\r\n");
  44.   fgets($ssock, 1024);
  45.   fputs($ssock, sprintf("%s\r\n", base64_encode($login)));
  46.   fgets($ssock, 1024);
  47.   fputs($ssock, sprintf("%s\r\n", base64_encode($password)));
  48.   fgets($ssock, 1024);
  49.  
  50.   // registering sender
  51.   fputs($ssock, sprintf("MAIL FROM: <%s>\r\n", $from));
  52.  fgets($ssock, 1024);
  53.  
  54.  // registering recipient(s)
  55.  foreach ($to as $address) {
  56.    fputs($ssock, sprintf("RCPT TO: <%s>\r\n", $address));
  57.    fgets($ssock, 1024);
  58.  }
  59.  
  60.  // sending data
  61.  fputs($ssock, "DATA\r\n");
  62.  fgets($ssock, 1024);
  63.  
  64.  $boundary = md5(time());
  65.  $data = sprintf("Subject: =?UTF-8?B?%s?=\r\nFrom: %s\r\nTo: %s\r\nContent-Type: multipart/%s; boundary=%s\r\n",
  66.                  base64_encode($subject), $from, implode(', ', $to), empty($file) ? 'alternative' : 'mixed', $boundary);
  67.  
  68.  if (strcmp($msg, ''))
  69.    $data = sprintf("%s\r\n--%s\r\nContent-Type: text/%s; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n%s\r\n",
  70.                    $data, $boundary, $html ? 'html' : 'plain', base64_encode($msg));
  71.  
  72.  for ($i = 0; $file[$i]; $i++)
  73.    $data = sprintf("%s\r\n--%s\r\nContent-Type: %s; name=\"%s\"\r\nContent-Transfer-Encoding: base64\r\n".
  74.                     "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n%s\r\n", $data, $boundary,
  75.                     $file[$i][0], $file[$i][1], $file[$i][1], chunk_split(base64_encode(file_get_contents($file[$i][2]))));
  76.  
  77.   $data = sprintf("%s--%s--\r\n.\r\n", $data, $boundary);
  78.   fputs($ssock, $data);
  79.   fgets($ssock, 1024);
  80.  
  81.   // exiting
  82.   fputs($ssock, "QUIT\r\n");
  83.   fgets($ssock, 1024);
  84.  
  85.   fclose($ssock);
  86.   return true;
  87. }
Add Comment
Please, Sign In to add comment