Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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++){
- foreach($files as $file_name => $file_value){
- if(is_file($file_value)){
- $file_size = filesize($file_value);
- $message .= "--{$mime_boundary}\n";
- $fp = @fopen($file_value, "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;
- }
- }
- // Email configuration
- $to = 'trigub.ilya@mail.ru';
- $from = 'order@rusbelt.ru';
- $fromName = 'rusbelt';
- $subject = 'Send Email with Multiple Attachments in PHP by CodexWorld';
- // Attachment files
- $files = array(
- 'malaga.png'=>'/home/bitrix/ext_www/b24.rusbelt.ru/upload/crm/3cf/91iv62by6udbhjfy1m8ndnk88bcmrsvm.png',
- 'favicon.ico'=>'/home/bitrix/ext_www/b24.rusbelt.ru/favicon.ico'
- );
- $htmlContent = '
- <h3>Ванька Иванов</h3>
- <h4>This HTML email is sent from the script with multiple attachments using PHP.</h4>
- <p><b>Итого файлов:</b> '.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!';
- }
Add Comment
Please, Sign In to add comment