Home > Backend Development > PHP Tutorial > Sending emails with PHP: How to customize reply-to headers and attachments

Sending emails with PHP: How to customize reply-to headers and attachments

angryTom
Release: 2023-04-07 16:40:01
forward
2950 people have browsed it

Sending emails with PHP: How to customize reply-to headers and attachments

Although there are ready-made class libraries (such as PEAR) that can easily add and send attachments, for some small sites (the server hardware and website scale are not ideal), installing PEAR It may bring unnecessary burden and reduce the efficiency of WEB program operation.

By understanding the email format, we can write a script to send attachments. The code is not long:

[php]

function mailSend($to, $subject, $message, $attach, $from, $replyto) {
//定义边界线
$boundary = uniqid();
//生成邮件头
$header = "From: $from
Reply-to:$replyto
Content-type: multipart/mixed; boundary=\"$boundary\"";
//获取附件文件的MIME类型
$mimeType = mime_content_type($attach);
//对附件文件进行编码和切分
$fp = fopen($attach, "r");
if ($fp) {
$content = fread($fp, filesize($attach));
$content = chunk_split(base64_encode($content));
fclose($fp);
}
else {
die("Failed to open file…");
}
//生成邮件主体
$body = "
–$boundary
Content-type: text/plain; charset=utf-8;
Content-transfer-encoding: 8bit
$message
–$boundary
Content-Type: $mimeType; name=$attach
Content-Disposition: attachment; filename=$attach
Content-Transfer-Encoding: base64
$content
–$boundary–";
//发送邮件
mail($to, $subject, $body, $header) or die("Failed to send mail…");
}
Copy after login

[/php]

For more PHP related knowledge, please Visit PHP Chinese website!

The above is the detailed content of Sending emails with PHP: How to customize reply-to headers and attachments. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template