來源出處:
http://xdeath.dyndns.pro/read.php?62
以PHP的範例來說就像下面這樣
function mailto($from,$to,$subject,$msg,$filename=")
{
$mailto = “;
if(count($to) > 1)
for($i = 0;$i < count($to);$i++)
$mailto .= “$to[$i],";
else
$mailto = $to;
$boundary = uniqid(“"); // 產生分隔字串
$subject = ‘=?utf-8?B?’.base64_encode(“$subject").’?=’; // 標題加密(防亂碼)
$headers = “;
$headers .= “MIME-Version: 1.0\r\n";
$headers .= “Content-type: multipart/mixed; boundary=\"$boundary\"; charset=\"UTF-8\"\r\n"; //宣告分隔字串
$headers .= ‘From:’.$from."\r\n"; // 設定寄件者
$headers .= “X-Mailer: PHP/".phpversion()."\r\n";
$body .= “–$boundary\r\n";
$body .= “Content-type: text/plain; charset=\"UTF-8\"\r\n";// 信件本文header
$body .= “Content-Transfer-Encoding: 8bit\r\n\r\n";// 信件本文header
$body .= $msg."\r\n"; // 本文內容
//附加檔案處理
if(!is_array($filename))
$files[0] = $filename;
if( count($files) != 0)
{
for($i = 0;$i < count($files);$i++)
{
$mimeType = mime_content_type($files[$i]); // 判斷檔案類型 ,php.ini要開啟php_mime_magic.dll
if(!$mimeType)
$mimeType = “application/unknown"; // 若判斷不出則設為未知
$data = chunk_split(base64_encode(file_get_contents($files[$i])));
$file = basename($files[$i]); //傳回不包含路徑的檔案名稱(mail中會顯示的檔名)
$body .= “–$boundary\r\n";
$body .= “Content-type: $mimeType; name=$file\r\n";
$body .= “Content-transfer-encoding: base64\r\n";
$body .= “Content-disposition: attachment; filename=$file\r\n\r\n";
$body .= “$data\r\n";
}
}
$body .= “–$boundary–“;//郵件結尾
mail($mailto, $subject, $body, $headers); // 寄出信件
}
perl範例
sub mailto
{
my ($from,$to,$title,$content,$file) = @_;
my $id = “;
for(my $i = 0;$i <= 20;$i++){$id .= chr(97 + int(rand(26)));}
$title = ‘=?utf-8?B?’.substr(encode_base64($title),0,-1).’?=’;
my $header = “X-Priority: 3\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 8bit\r\nContent-type: text/html;charset=utf-8
Content-type: multipart/mixed; boundary=\"$id\"; charset=\"UTF-8\"\r\nFrom: $from\r\nSubject: $title\r\nTo: $to\r\n–$id
Content-type: text/plain; charset=\"UTF-8\"\r\nContent-Transfer-Encoding: 8bit\r\n\r\n";
#夾檔
if($file ne “){
my $filename = basename($file);
my $data = encode_base64(`cat $filename`);
$content .= “\r\n\r\n–$id\r\nContent-type: application/unknown;name=$filename\r\nContent-transfer-encoding: base64
Content-disposition: attachment; filename=$filename\r\n\r\n$data";
}
$content .= “–$id–“;
open(MM,"|/usr/lib/sendmail -oem -oi -f ‘$from’ -t ‘$to'");
print MM “$header$content";
close(MM);
}