Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * smtp mail
- *
- * @param $server smtp server
- * @param $port smtp port
- * @param $login smtp username
- * @param $password smtp user password
- * @param $to recipient(s) (user@domain[, ...])
- * @param $from sender address
- * @param $subject email subject
- * @param $msg email text
- * @param $html true if text is html, else false
- * @param $file file(s) (array(array(content type, file name, file contents)[, ...]))
- * @return boolean
- */
- function smtpmail($server, $port, $login, $password, $to, $from, $subject, $msg, $html = false, $file = array())
- {
- $ssock = fsockopen($server, $port, $errno, $errstr, 30);
- if (!$ssock) return false;
- // recipients
- $to = explode(',', $to);
- foreach ($to as $k => $v) {
- preg_match('/[^@\s\<]+@[^@\s\>]+/', $to[$k], $match);
- if (!$match[0]) unset($to[$k]);
- else $to[$k] = $match[0];
- }
- if (empty($to)) return false;
- // mail from
- preg_match('/[^@\s\<]+@[^@\s\>]+/', $from, $match);
- if (!$match[0]) return false;
- $from = $match[0];
- // initiating
- fputs($ssock, '');
- fgets($ssock, 1024);
- fputs($ssock, sprintf("HELO %s\r\n", $_SERVER['HTTP_HOST']));
- fgets($ssock, 1024);
- // authorizing
- fputs($ssock, "AUTH LOGIN\r\n");
- fgets($ssock, 1024);
- fputs($ssock, sprintf("%s\r\n", base64_encode($login)));
- fgets($ssock, 1024);
- fputs($ssock, sprintf("%s\r\n", base64_encode($password)));
- fgets($ssock, 1024);
- // registering sender
- fputs($ssock, sprintf("MAIL FROM: <%s>\r\n", $from));
- fgets($ssock, 1024);
- // registering recipient(s)
- foreach ($to as $address) {
- fputs($ssock, sprintf("RCPT TO: <%s>\r\n", $address));
- fgets($ssock, 1024);
- }
- // sending data
- fputs($ssock, "DATA\r\n");
- fgets($ssock, 1024);
- $boundary = md5(time());
- $data = sprintf("Subject: =?UTF-8?B?%s?=\r\nFrom: %s\r\nTo: %s\r\nContent-Type: multipart/%s; boundary=%s\r\n",
- base64_encode($subject), $from, implode(', ', $to), empty($file) ? 'alternative' : 'mixed', $boundary);
- if (strcmp($msg, ''))
- $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",
- $data, $boundary, $html ? 'html' : 'plain', base64_encode($msg));
- for ($i = 0; $file[$i]; $i++)
- $data = sprintf("%s\r\n--%s\r\nContent-Type: %s; name=\"%s\"\r\nContent-Transfer-Encoding: base64\r\n".
- "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n%s\r\n", $data, $boundary,
- $file[$i][0], $file[$i][1], $file[$i][1], chunk_split(base64_encode(file_get_contents($file[$i][2]))));
- $data = sprintf("%s--%s--\r\n.\r\n", $data, $boundary);
- fputs($ssock, $data);
- fgets($ssock, 1024);
- // exiting
- fputs($ssock, "QUIT\r\n");
- fgets($ssock, 1024);
- fclose($ssock);
- return true;
- }
Add Comment
Please, Sign In to add comment