На js это будет очень проблематично написать. Попробуйте например php.
Я к примеру использую вот этот чудесный класс:
class Mail
{
var $from;
var $to;
var $subject;
var $body;
var $html_auto = true;
var $html = false;
var $cp = 'windows-1251';
var $cp_auto = true;
var $cp_in;
var $nl;
var $parts = Array();
var $headers = Array();
function Mail()
{
substr(PHP_OS, 0, 3) == 'WIN' ? $this->nl = "\r\n" : $this->nl = "\n";
}
function add_attachment($file)
{
if (file_exists($file)) {
$file_name = basename($file);
if (function_exists('mime_content_type')) {
$ctype = mime_content_type($file);
} else {
$pathinfo = pathinfo($file);
$mime = Array(
'rar' => 'application/x-tar',
'zip' => 'application/x-zip-compressed',
'asf' => 'video/x-ms-asf',
'wmv' => 'video/x-ms-wmv',
'aiff' => 'audio/aiff',
'au' => 'audio/basic',
'mid' => 'audio/mid',
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'wma' => 'audio/x-ms-wma',
'avi' => 'video/x-msvideo',
'ivf' => 'video/x-ivf',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'art' => 'image/x-jg',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpg' => 'image/jpg',
'swf' => 'application/futuresplash',
'tiff' => 'image/tiff',
'pdf' => 'application/pdf',
'html' => 'text/html',
'htm' => 'text/html',
'xls' => 'application/x-msexcel',
'doc' => 'application/msword',
'rtf' => 'application/msword',
'rtx' => 'text/richtext',
'xml' => 'text/xml',
'hta' => 'application/hta',
'css' => 'text/css',
'txt' => 'text/plain'
);
isset($mime[$pathinfo['extension']]) ? $ctype = $mime[$pathinfo['extension']] : $ctype = 'application/octet - stream';
}
$fopen = fopen($file, 'r');
$attachment = fread($fopen, filesize($file));
fclose($fopen);
$this->parts[] = Array
(
'name' => $file_name,
'attachment' => $attachment,
'ctype' => $ctype,
);
return 0;
} else {
return 1;
}
}
function add_header($header)
{
$this->headers[] = trim($header);
}
function detect_cyr_charset($str)
{
$charsets = Array(
'k' => 0,
'w' => 0,
'd' => 0,
'i' => 0,
'm' => 0
);
for ($i = 0, $length = strlen($str); $i < $length; $i++) {
$char = ord($str[$i]);
//non-russian characters
if ($char < 128 || $char > 256) continue;
//cp866
if (($char > 159 && $char < 176) || ($char > 223 && $char < 242)) $charsets['d'] += 3;
if (($char > 127 && $char < 160)) $charsets['d'] += 1;
//koi8-r
if (($char > 191 && $char < 223)) $charsets['k'] += 3;
if (($char > 222 && $char < 256)) $charsets['k'] += 1;
//win-1251
if ($char > 223 && $char < 256) $charsets['w'] += 3;
if ($char > 191 && $char < 224) $charsets['w'] += 1;
//mac
if ($char > 221 && $char < 255) $charsets['m'] += 3;
if ($char > 127 && $char < 160) $charsets['m'] += 1;
//iso-8859-5
if ($char > 207 && $char < 240) $charsets['i'] += 3;
if ($char > 175 && $char < 208) $charsets['i'] += 1;
}
arsort($charsets);
return key($charsets);
}
function cp_convert()
{
$cp_array = Array(
'windows-1251' => 'w',
'koi8-r' => 'k',
'iso8859-5' => 'i',
'x-cp866' => 'a',
'x-mac-cyrillic' => 'm'
);
if ($this->cp_auto == true) {
$from = $this->detect_cyr_charset($this->body);
} else {
isset($cp_array[$this->cp_in]) ? $from = $cp_array[$this->cp_in] : $from = 'w';
}
isset($cp_array[$this->cp]) ? $to = $cp_array[$this->cp] : $to = 'k';
$this->subject = convert_cyr_string($this->subject, $from, $to);
$this->body = convert_cyr_string($this->body , $from, $to);
}
function build_part($part)
{
$attachment = chunk_split(base64_encode($part['attachment']));
$encoding = 'base64';
return 'Content-Type: ' . $part['ctype'] . ($part['attachment'] ? '; name = "' . $part['name'] . '"' : '') . $this->nl . 'Content-Transfer-Encoding: ' . $encoding . $this->nl . $this->nl . $attachment . $this->nl;
}
function build_multipart()
{
$boundary = 'b' . md5(uniqid(time()));
$multipart = 'Content-Type: multipart/mixed; boundary = ' . $boundary . $this->nl . $this->nl . 'This is a MIME encoded message.' . $this->nl . $this->nl . '--' . $boundary . $this->nl;
$multipart .= 'Content-Type: text/';
if ($this->html_auto == true) {
$pattern = "%</?\w+(\s[^>]*)?>%is";
preg_match($pattern, $this->body) == true ? $multipart .= 'html' : $multipart .= 'plain';
} else {
$this->html == true ? $multipart .= 'html' : $multipart .= 'plain';
}
$multipart .= '; charset=' . $this->cp . $this->nl . 'Content-Transfer-Encoding: 8bit' . $this->nl . $this->nl . $this->body . $this->nl . $this->nl . '--' . $boundary;
$i = 0;
$count = sizeof($this->parts);
while ($i < $count) {
$multipart .= $this->nl . $this->build_part($this->parts[$i]).
'--' . $boundary;
$i++;
}
return $multipart .= '--' . $this->nl;
}
function send()
{
$this->cp_convert();
$headers = '';
empty($this->from) or $headers .= 'From: ' . $this->from . $this->nl;
$i = 0;
$count = sizeof($this->headers);
while ($i < $count) {
$headers .= $this->headers[$i] . $this->nl;
$i++;
}
$headers .= 'MIME-Version: 1.0' . $this->nl;
$headers .= $this->build_multipart();
return mail($this->to, $this->subject, '', $headers);
}
}
Используем к примеру так:
$mail = new Mail;
$mail->from = $MemberObj->mail;
$mail->to = $_POST['MailTo'];
$mail->subject = $_POST['MailSubj'];
$mail->body = $_POST['MailMessage'];
$mail->add_header('X-Priority: 2');
if(isset($_FILES['MailAttach']['name']))
$mail->add_attachment($_FILES['MailAttach']['name']);
$mail->send();