Trigub_Ilia

Отправка нескольких файлов через mail()

Feb 4th, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $files = array()){
  2.     // Sender info  
  3.     $from = $senderName." <".$senderEmail.">";  
  4.     $headers = "From: $from";
  5.  
  6.     // Boundary  
  7.     $semi_rand = md5(time());  
  8.     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
  9.  
  10.     // Headers for attachment  
  11.     $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";  
  12.  
  13.     // Multipart boundary  
  14.     $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
  15.     "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
  16.  
  17.     // Preparing attachment
  18.     if(!empty($files)){
  19.         //for($i=0;$i<count($files);$i++){
  20.         foreach($files as $file_name => $file_value){
  21.             if(is_file($file_value)){
  22.  
  23.                 $file_size = filesize($file_value);
  24.  
  25.                 $message .= "--{$mime_boundary}\n";
  26.                 $fp =    @fopen($file_value, "rb");
  27.                 $data =  @fread($fp, $file_size);
  28.                 @fclose($fp);
  29.                 $data = chunk_split(base64_encode($data));
  30.                 $message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" .  
  31.                 "Content-Description: ".$file_name."\n" .
  32.                 "Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" .  
  33.                 "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  34.             }
  35.         }
  36.     }
  37.      
  38.     $message .= "--{$mime_boundary}--";
  39.     $returnpath = "-f" . $senderEmail;
  40.      
  41.     // Send email
  42.     $mail = mail($to, $subject, $message, $headers, $returnpath);  
  43.      
  44.     // Return true if email sent, otherwise return false
  45.     if($mail){
  46.         return true;
  47.     }else{
  48.         return false;
  49.     }
  50. }
  51.  
  52.  
  53. // Email configuration
  54. $to = 'trigub.ilya@mail.ru';
  55. $from = 'order@rusbelt.ru';
  56. $fromName = 'rusbelt';
  57.  
  58. $subject = 'Send Email with Multiple Attachments in PHP by CodexWorld';  
  59.  
  60. // Attachment files
  61. $files = array(
  62.     'malaga.png'=>'/home/bitrix/ext_www/b24.rusbelt.ru/upload/crm/3cf/91iv62by6udbhjfy1m8ndnk88bcmrsvm.png',
  63.     'favicon.ico'=>'/home/bitrix/ext_www/b24.rusbelt.ru/favicon.ico'
  64. );
  65.  
  66. $htmlContent = '
  67.    <h3>Ванька Иванов</h3>
  68.    <h4>This HTML email is sent from the script with multiple attachments using PHP.</h4>
  69.    <p><b>Итого файлов:</b> '.count($files).'</p>';
  70.  
  71. // Call function and pass the required arguments
  72. $sendEmail = multi_attach_mail($to, $subject, $htmlContent, $from, $fromName, $files);
  73.  
  74. // Email sending status
  75. if($sendEmail){
  76.     echo 'The email is sent successfully.';
  77. }else{
  78.     echo 'Email sending failed!';
  79. }
Add Comment
Please, Sign In to add comment