T
T
tectolog2022-02-04 13:24:41
PHP
tectolog, 2022-02-04 13:24:41

I am using PHP mail(). Why does the attached file in the letter come in the form of abracadabra, and not as an attached file?

Hello.

Situation: I use mail() php to send emails with attached files (jpg, png, etc.) from the site. A letter arrives in the mail with all the headers and content - everything is fine here, and the attached file instead of the attached file is displayed as abracadabra. Already tried everything - nothing helps. I give the code, maybe I already don’t notice some obvious things. The data is sent to the server via jQuery + ajax - everything works here too, a plug somewhere in the headers:

...
                $type   = $_POST[ 'selectTypeCalc' ];
    $size1   = $_POST[ 'inputSize1Calc' ];
    $size2   = $_POST[ 'inputSize2Calc' ];
    
    $name   = $_POST[ 'inputNameCalc' ];
    $tel    = $_POST[ 'inputTelCalc' ];
    $email  = $_POST[ 'inputEmailCalc' ];
    $message= $_POST[ 'inputMessageCalc' ];
    
    $path = '/home/site/site.ru/docs/upload/tmp/';
    $to = '[email protected]';
    $from_mail = '[email protected]';
    
    $subject = "=?UTF-8?B?".base64_encode( "Запрос на онлайн-расчет стоимости кухни" )."?=";
    $boundary = md5( uniqid( time() ) );
    $message_body = '
      <h4>Запрос на онлайн-расчет стоимости кухни со следующими данными:</h4>
      <table cellspacing="0" style="width: 300px; height: 200px;">
        <tr>
          <th>Имя:</th><td>'.$name.'</td>
        </tr>
        <tr>
          <th>Тел:</th><td>'.$tel.'</td>
        </tr>
        <tr style="background-color: #e0e0e0;">
          <th>Email:</th><td>'.$email.'</td>
        </tr>
        <tr>
          <th>Описание проекта:</th><td>'.$message.'</td>
        </tr>
        <tr>
          <th>Тип кухни:</th><td>'.$type.'</td>
        </tr>
        <tr>
          <th>Длина стороны А, см:</th><td>'.$size1.'</td>
        </tr>
        <tr>
          <th>Длина стороны B, см:</th><td>'.$size2.'</td>
        </tr>
      </table>';
    
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    $headers .= "From: КОМПАНИЯ МЕБЕЛЬГУД <" . $from_mail . ">" . "\r\n";
    $headers .= "Content-Transfer-Encoding: base64" . "\r\n"  . "\r\n"; 

    $message = chunk_split( base64_encode( $message_body ) );
    
    if( !empty( $_FILES[ 'inputFileCalc' ][ 'tmp_name' ] ) )
    {
      $filetype = $_FILES[ 'inputFileCalc' ][ 'type' ];
      $filename = $_FILES[ 'inputFileCalc' ][ 'name' ];
      
      if( move_uploaded_file( $_FILES[ 'inputFileCalc' ][ 'tmp_name' ], $path.$filename ) )
      {
        $file = $path.$filename;
        $handle = fopen( $file, "r" );
        if( !$handle ) { $status = 'err'; } 
        
        $content = fread( $handle, filesize( $file ) );
        fclose( $handle );

        $message .= "--" . $boundary . "--";
        $message .= "Content-Type: application/octet-stream; name='" . $filename . "'\r\n";
        $message .= "Content-Disposition: attachment; filename='" . $filename . "'\r\n";
        $message .= "\r\n"; 
        $message .= chunk_split( base64_encode( $content ) ); 
        $message .= "--" . $boundary . "--";
      }else{ die; }
    }

    mail( $to, $subject, $message, $headers );
...


Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2022-02-04
@tectolog

If you really want so much to reinvent the wheel yourself to encode a letter in MIME, then you should figure out how to do it right.
Content-type header is invalid. Body - also one of the chunks, framed in "--" . $boundary . "--";.
Here is a very simple example

F
FanatPHP, 2022-02-04
@FanatPHP

https://qna.habr.com/answer?answer_id=1877889#answ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question