Gửi email kèm file đính kèm dùng hàm php mail()
(10/09/2010
Nếu bạn cần dùng hàm mail() của php để gửi email. Sau đây là đoạn code tham khảo. Nếu bạn chưa biết cách sử dụng hàm mail() của php, có thể vào đây để tham khảo http://ru.php.net/manual/en/function.mail.php
<?php
$to = 'youraddress@example.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
(Source: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php)