کد:
<?php
$to = "user1@mailserver.com, user2@mailserver.com";
$subject = "ایمیل با ساختار html";
$message = '<html><head>';
$message .= '<style>.colored{color:red;}</style>';
$message .= '</head><body>';
$message .= '<p>کاربر گرامی، سلام</p>';
$message .= '<p class="colored">این یک متن با رنگ متفاوت در ایمیل ارسالی است!</p>';
$message .= '</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <test@domain.com>' . "\r\n";
mail($to, $subject, $message, $headers);
?>
کد:
<?php
//دریافت کننده ایمیل
$to = "username@example.com";
//ارسال کننده ایمیل
$from = "yourname <yourname@example.com>";
//موضوع
$subject = "ارسال ایمیل به همراه ضمیمه";
//کد رندوم boundary
$randcode = md5("r",time());
//خط جدید
$line = "\r\n"; //یا PHP_EOL
//فایل ضمیمه
$filename = "phpbook.zip";
//تجزیه و آماده سازی فایل برای ارسال
$attachment = chunk_split(base64_encode($filename));
//سربرگ ها
$headers = "From: ".$from.$line;
$headers .= "MIME-Version: 1.0".$line;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$randcode."\"";
//متن پیام به صورت ساده
$body = "--".$randcode.$line;
$body .= "Content-Transfer-Encoding: 7bit".$line.$line;
$body .= "این یک ایمیل به همراه ضمیمه است".$line;
//متن پیام به صورت html
$body .= "--".$randcode.$line;
$body .= "Content-Type: text/html; charset=\"utf-8\"".$line;
$body .= "Content-Transfer-Encoding: 8bit".$line.$line;
$body .= "این یک ایمیل به همراه <b>ضمیمه</b> است".$line;
//تنظیمات فایل ضمیمه
$body .= "--".$randcode.$line;
$body .= "Content-Type: application/zip; name=\"".$filename."\"".$line;
$body .= "Content-Transfer-Encoding: base64".$line;
$body .= "Content-Disposition: attachment".$line.$line;
$body .= $attachment.$line;
$body .= "--".$randcode."--";
//ارسال ایمیل
if (mail($to, $subject, $body, $headers)) {
echo("<p>ایمیل شما با موفقیت ارسال شد.</p>");
}
else {
echo("<p>خطا در ارسال ایمیل! تنظیمات سرور شما از این امکان پشتیبانی نمی کند</p>");
}
?>
کد بالا با استفاده از تابع mail در php، علاوه بر ارسال متن ساده و متن به صورت html قابلیت ارسال فایل ضمیمه (attachment) را نیز دارد.
کد:
<?php
//تابع نمایش نوع فایل
function my_mime_content_type($filename){
$mime_types = array(
//files
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
//images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
//archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
//audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mpeg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mpg' => 'video/mpeg',
'wav' => 'audio/wav',
'aiff' => 'audio/aiff',
'aif' => 'audio/aiff',
'avi' => 'video/msvideo',
'wmv' => 'video/x-ms-wmv',
//adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
//ms office
'doc' => 'application/msword',
'docx' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
//open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
else {
return 'application/octet-stream';
}
}
//آدرس فایل شما
$filename = 'image/file.gif';
//خروجی
echo my_mime_content_type($filename);
?>