发送具有多个附件HTML电子邮件
我们将创建一个自定义PHP功能,所有代码都将在此功能中分组,以获得更好的可用性。
multi_attach_mail()有助于使用PHP发送具有多个附件的电子邮件。
'multi_attach_mail()'函数需要以下参数。
- '$to' - 必填。指定收件人电子邮件地址。
- '$subject' - 必填。指定电子邮件主题。
- '$message' - 必填。指定电子邮件正文内容(文本或者HTML)。
- '$senderemail' - 必填。指定发件人电子邮件地址。
- '$sendername' - 必填。指定发件人名称。
- '$files' - 可选。一系列文件路径以添加电子邮件。
此函数在成功或者错误时返回true。
/*
* Custom PHP function to send an email with multiple attachments
* $to Recipient email address
* $subject Subject of the email
* $message Mail body content
* $senderEmail Sender email address
* $senderName Sender name
* $files Files to attach with the email
*/
function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $files = array()){
//Sender info
$from = $senderName." <".$senderEmail.">";
$headers = "From: $from";
//Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
//Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
//Preparing attachment
if(!empty($files)){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$file_name = basename($files[$i]);
$file_size = filesize($files[$i]);
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i], "rb");
$data = @fread($fp, $file_size);
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" .
"Content-Description: ".$file_name."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderEmail;
//Send email
$mail = mail($to, $subject, $message, $headers, $returnpath);
//Return true if email sent, otherwise return false
if($mail){
return true;
}else{
return false;
}
}
使用自定义PHP功能发送具有多个附件的电子邮件
以下示例显示了如何使用我们的自定义PHP功能来发送具有多个附件的电子邮件。
调用'multi_attach_mail()'函数并通过所需的参数。
- '$to' - 接收器电子邮件地址。
- '$from' - 发件人电子邮件地址。
- '$fromname' - 发件人姓名。
- '$subject' - 电子邮件的主题。
- '$files' - 数组格式的附件文件路径。
- '$htmlcontent' - 电子邮件正文内容。
//Email configuration
$to = 'recipient@example.com';
$from = 'sender@example.com';
$fromName = 'Sender Name';
$subject = 'Send Email with Multiple Attachments in PHP by onitroad';
//Attachment files
$files = array(
'files/onitroad-web-view.png',
'files/onitroad-site.pdf'
);
$htmlContent = '
<h3>Email with Multiple Attachments by onitroad</h3>
<h4>This HTML email is sent from the script with multiple attachments using PHP.</h4>
<p>Total Attachments: '.count($files).'</p>';
//Call function and pass the required arguments
$sendEmail = multi_attach_mail($to, $subject, $htmlContent, $from, $fromName, $files);
//Email sending status
if($sendEmail){
echo 'The email is sent successfully.';
}else{
echo 'Email sending failed!';
}
PHP Mail()函数提供了从脚本发送电子邮件的简单方法。
我们可以使用PHP Mail()函数向包含文本或者HTML内容的电子邮件发送电子邮件。
除了HTML内容之外,我们可以使用PHP与电子邮件发送附件。
使用PHP Mail()函数中的一些添加标题来发送带有PDF/Image/Word附件的电子邮件。
本教程将向我们展示如何在PHP中使用单个或者多个附件发送电子邮件。
我们的示例脚本可帮助Web开发人员发送文本或者HTML电子邮件,包括任何类型的文件(如图像,.doc,.docx,.pdf,.txt等)作为PHP的附件。
要使整个过程简单,我们将在函数中对整个代码进行分组。
我们可以通过调用单个函数发送具有多个附件的电子邮件。
日期:2020-06-02 22:15:59 来源:oir作者:oir
