function email($sender, $senderMail, $recipient, $recipientMail, $subject, $message, $attachments = '') { // E-Mail strings $senderMailString = "{$sender} <{$senderMail}>"; $recipientMailString = strReplaceByArray(array('<', '>'), '', $recipient) . " <{$recipientMail}>"; if ($senderMail == "") { $senderMail = absender_mail(); } $header = "From: {$senderMailString}\r\n"; $header .= "Reply-To: {$senderMailString}\r\n"; $header .= "X-Mailer: le-serveur\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Transfer-Encoding: 8bit\r\n"; // Concat message $html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3.org/TR/html4/loose.dtd\">"; // Add html tag if not set if (!strstr($message, "<html>")) { $html .= "<html><head></head><body>"; } // Include message $html .= $message; // Add html tag if not set if (!strstr($message, "</html>")) { $html .= "</body></html>"; } $html .= "\r\n"; // Attachments? if ($attachments == "") { $header .= "Content-type: text/html; charset=utf-8\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; } else { $boundary = 'le-serveur-' . md5(date('r', time())); $header .= "Content-type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--" . $boundary . "\r\n"; $header .= "Content-type: text/html; charset=utf-8\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; foreach ($attachments as $attachment) { // Build attachment string $attachmentStr = "--" . $boundary . "\r\n"; $attachmentStr .= "Content-Type: " . $attachment['type'] . "; name=\"" . $attachment['name'] . "\"\r\n"; $attachmentStr .= "Content-Transfer-Encoding: base64\r\n"; $attachmentStr .= "Content-Disposition: attachment; filename=\"" . $attachment['name'] . "\"\r\n\r\n"; $attachmentStr .= chunk_split(base64_encode(file_get_contents($attachment['path']))); $attachmentStr .= "--" . $boundary; // Prepare for mail transfer $attachmentStr = htmlspecialchars_decode($attachmentStr, ENT_QUOTES); // Add to the mail text $html .= $attachmentStr; } } // Convert HTML characters $header = htmlspecialchars_decode($header, ENT_QUOTES); // Set return path $returnPath = "-f" . $senderMail; // Send e-mail return mail($recipientMailString, "=?utf-8?B?" . base64_encode($subject) . "?=", $html, $header, $returnPath); }
function strReplaceByArray($searches = array(), $subjects = array()) { $retArray = array(); foreach ($subjects as $key => $subject) { if (is_array($subject)) { $retArray[$key] = strReplaceByArray($searches, $subject); } else { $retArray[$key] = str_replace(array_keys($searches), $searches, $subject); } } return $retArray; }