how to send attachment in mail in php? – Send Email with Attachment in PHP. Mail send with attachments in PHP step by step.
Table of Contents
how to send attachment in mail in php?
In this post, I will see how i can send an email with attachments using mail() function. This custom function or PHP mail attachment script is able to send a plain text email message together with a single attachment file.
Send HTML Email with Attachment
[php]
PHP Email with Attachment by itsolutionstuck
This email is sent from the PHP script with attachment.
‘;
$headers = “From: $fromName”.” <".$from.">“;
$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;
$headers .= “\nMIME-Version: 1.0\n” . “Content-Type: multipart/mixed;\n” . ” boundary=\”{$mime_boundary}\””;
$message = “–{$mime_boundary}\n” . “Content-Type: text/html; charset=\”UTF-8\”\n” .
“Content-Transfer-Encoding: 7bit\n\n” . $htmlContent . “\n\n”;
if(!empty($file) > 0){
if(is_file($file)){
$message .= “–{$mime_boundary}\n”;
$fp = @fopen($file,”rb”);
$data = @fread($fp,filesize($file));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= “Content-Type: application/octet-stream; name=\””.basename($file).”\”\n” .
“Content-Description: “.basename($file).”\n” .
“Content-Disposition: attachment;\n” . ” filename=\””.basename($file).”\”; size=”.filesize($file).”;\n” .
“Content-Transfer-Encoding: base64\n\n” . $data . “\n\n”;
}
}
$message .= “–{$mime_boundary}–“;
$returnpath = “-f” . $from;
$mail = @mail($to, $subject, $message, $headers, $returnpath);
echo $mail?”
Email Sent Successfully!
“:”
Email sending failed.
“;
?>
[/php]
Don’t Miss : PHP Curl Get Request with Parameters
Sending Email to Multiple Recipients:
[php]
$headers .= “\nCc: mail@itsolutionstuck.com”;
$headers .= “\nBcc: mail@itsolutionstuck.com”;
[/php]