Начнем с азов. Отправка почты в php выполняется с помощью функции mail:
1 |
bool mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters]] ) |
1 2 3 4 5 6 7 8 9 |
to - адрес получателя subject - тема письма message - текст письма Далее слудуют необязательные параметры: additional_headers - дополнительные заголовки additional_parameters - дополнительные параметры командной строки Возвращаемое значение: true - письмо принято к доставке false - письмо не принято к доставке |
Функция отправки почты с вложениеми:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function KMail($to, $from, $subj, $text, $files = null, $isHTML = false){ $boundary = "------------".strtoupper(md5(uniqid(rand()))); $headers = "From: ".$from."\r\n X-Mailer: koz1024.net\r\n MIME-Version: 1.0\r\n Content-Type: multipart/alternative;boundary=\"$boundary\"\r\n\r\n "; if (!$isHTML){ $type = 'text/plain'; }else{ $type = 'text/html'; } $body = $boundary."\r\n\r\n Content-Type:".$type."; charset=utf-8\r\n Content-Transfer-Encoding: 8bit\r\n\r\n ".$text."\r\n\r\n"; if ((is_array($files))&&(!empty($files))){ foreach($files as $filename => $filecontent){ $body .= $boundary."\r\n Content-Type: application/octet-stream;name=\"".$filename."\"\r\n Content-Transfer-Encoding:base64\r\n Content-Disposition:attachment;filename=\"".$filename."\"\r\n\r\n ".chunk_split(base64_encode($filecontent)); } } return mail($to, $subj, $body, $headers); } |
Использование:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$files = array('archive.zip' => file_get_contents('archive.zip'), 'myphoto.png' => file_get_contents('myfoto.png')); if (KMail('test@example.com', 'ivan@example.com', 'проба пера', 'Привет, высылаю файлы...', $files, false)){ echo 'Mail Отправлен'; }else{ echo 'Произошла ошибка'; } //или так $html = "(HTML код письма)"; if (KMail('test@example.com', 'ivan@example.com', 'проба пера', $html, null, true)){ echo 'Mail Отправлен'; }else{ echo 'Произошла ошибка'; } |