Hlavní navigace

PHP: funkce mail() s českou diakritikou a přílohami

4. 11. 2012 12:07 (aktualizováno) Tomáš Bedřich

V návaznosti na můj předchozí tweet jsem se rozhodl dát sem mojí funkci na odesílání mailu. Snad někomu pomůže minimálně úsporou času.
Kdyby jste měli jakékoliv nápady na vylepšení, rád si je přečtu v komentářích. Díky

<?php
/** This function can send mail with attachments, custom headers and UTF-8 encoding for body and subject
* example use: email("david@example.net", "Greeting from holiday", "Hi, here is some my photos.

See you soon", "john@example.net", "John Example", array("Reply-to" => "helena@example.net"), array("photo1.jpg", "another/photo2.jpg"));
* @param $to recipient
* @param $subject subject of the message
* @param $message text body of the message in HTML format
* @param $from sender's email
* @param $from_name [OPTIONAL] sender's name - if not set, then sender's name will be set to sender's email
* @param $headers [OPTIONAL] array of custom headers in format {"Header-name" => "Header value"}, if necessary
* @param $files [OPTIONAL] array of files names to attach to message in format {"path/to/file1.bin", "path/to/longer/file2.txt"}
* @return true if mail was send succesfully
* @copyright Tomáš Bedřich, ja@tbedrich.cz
*/

function email($to, $subject, $message, $from, $from_name = "", $headers = array(), $files = array()) {

// Generate boundary string
$semi_rand = md5(uniqid(time()));
$boundary = "--$semi_rand\n";

// Generate subject
$subject = "=?utf-8?B?".base64_encode($subject)."?=";

// Generate custom headers
$headers = "";
foreach($headers as $name => $value) {
$headers .= $name . ": " . $value . "\n";
}

// Generate From header
if(!empty($from_name)) $headers .= "From: =?UTF-8?B?".base64_encode($from_name)."?=\n";
else $headers .= "From: $from\n";

// Say, that this will be multipart message
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$semi_rand\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= $boundary;

// Append text in HTML format
$headers .= "Content-Type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n\n";
$headers .= $message . "\n\n";
$headers .= $boundary;

// Append attachments
for($i = 0; $i < count($files); $i++) {

// Get name of file and its mime type
$mime = mime_content_type($files[$i]);
$basename = strtolower(basename($files[$i]));

// Read the data and split them into smaller blocks
$file = fopen($files[$i], "rb");
$data = fread($file, filesize($files[$i]));
fclose($file);
$data = chunk_split(base64_encode($data));

// Append next part of message which contains file data
$headers .= "Content-Type: $mime; name=\"$basename\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"$basename\"\n\n";
$headers .= $data . "\n\n";

// Append boundary except the last file
if($i != count($files)-1) $headers .= $boundary;
}

// Close mail body
$headers .= "--$semi_rand--";

// Send mail
return mail($to, $subject, "", $headers);
}
?>

Odkaz na tuto funkci na Pastebin.com zde.

Sdílet

Žádné názory