Home>

Creating an automatic reply mail with an attached file.
The text and subject are displayed correctly without being garbled, but the parts that appear to be attachments are displayed as follows.

▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼
------ = _ Boundary_3806_5b7e718e2469f_
Content-Type: application/octet-stream;name = "=? ISO-2022-JP? B? BWFudWFsX3dlYmgucGRm? ="
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename = "=? ISO-2022-JP? B? BWFudWFsX3dlYmgucGRm? ="</Code></pre>
<p>The corresponding part in the php file is as follows.<br />
I tried various causes, but it doesn't work, so I would appreciate it if you could tell me.</p>
<pre><code data-language = "PHP">function send_dmail_webh ($name, $mailto, $id, $product) {
  mb_language ("Japanese");
  mb_internal_encoding ('SJIS');
//destination
  $mail_to = $mailto;
  // source address
  $mail_from = "[email protected]";
  $mime_type = "application/octet-stream";
    // Since it is a multipart, specify the part delimiter string
  $boundary = '---- = _ Boundary_' .uniqid (rand (1000,9999) .'_ ') .'_';
  $filepath = "/files/manual_webh.pdf";
  $filename = basename ($filepath);

  $filename = mb_convert_encoding ($filename, 'ISO-2022-JP');
  $filename = "=? ISO-2022-JP? B?". base64_encode ($filename). "? =";
  // header
  $headers = "";
  $headers. = "Content-Type: text/plain;charset = ISO-2022-JP \ n";
  $headers. = "Content-Transfer-Encoding: 7bit \ n";
  $headers. = "From:". $mail_from;
  $headers. = "\ n";
  //subject
  $mail_subject = "";
  $mail_subject. = "[". $product. "] Guidance";
  //Text
  $mail_body = "";
  $mail_body. = $name. "Like \ r \ n \ r \ n";
  $mail_body. = "▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ \ n";
  // attachment
  $mail_body. = "-{$boundary} \ n";
  $mail_body. = "Content-Type: {$mime_type};name = \" {$filename} \ "\ n".
    "Content-Transfer-Encoding: base64 \ n".
    "Content-Disposition: attachment;filename = \" {$filename} \ "\ n \ n";
  $f_encoded = chunk_split (base64_encode ($filebody));
  $mail_body. = $f_encoded. "\ n \ n";
  // send processing
  mb_send_mail ($mail_to, $mail_subject, $mail_body, $headers);
}
php
  • Answer # 1

    You seem to write something completely different from the page you are referring to. Read and correct it properly.

    $mail_body. = "-{$boundary} \ n";

    This alone cannot be interpreted. Because there is no description that-{$boundary}is a boundary.
    The site that you are referring to has the following description.

    $head. = "Content-Type: multipart/mixed;boundary = \" {$boundary} \ "\ n";
    $head. = "Content-Transfer-Encoding: 7bit";
    // Text
    $body. = "-{$boundary} \ n";
    $body. = "Content-Type: text/plain;charset = ISO-2022-JP;"
    "Content-Transfer-Encoding: 7bit \ n";
    // Attachment processing
    $body. = "-{$boundary} \ n";
    $body. = "Content-Type: {$mime_type};name = \" {$filename} \ "\ n".
    "Content-Transfer-Encoding: base64 \ n".
    "Content-Disposition: attachment;filename = \" {$filename} \ "\ n";

    Content-Type: multipart/mixed;boundary = \"{$boundary} \"says "The email contains multiple types of documents, The line is indicated by this string. "

    Furthermore,Content-Typeindicating the specific type of document such astext/plainis declared under each border.

  • Answer # 2

    Below is the final file format.

    function send_dmail_webh ($name, $mailto, $id, $product) {
      mb_language ("Japanese");
      mb_internal_encoding ('SJIS');
    //destination
      $mail_to = $mailto;
      // source address
      $mail_from = "[email protected]";
      $mime_type = "application/octet-stream";
        // Since it is a multipart, specify the part delimiter string
      $boundary = '---- = _ Boundary_' .uniqid (rand (1000,9999) .'_ ') .'_';
      $filepath = "..../files/manual_webh.pdf";
      $filename = basename ($filepath);
      $filename = mb_convert_encoding ($filename, 'ISO-2022-JP');
      $filename = "=? ISO-2022-JP? B?". base64_encode ($filename). "? =";
      // header
      $headers = "";
      $headers. = "From:". $mail_from;
      $headers. = "\ n";
      $headers. = "MIME-Version: 1.0 \ n";
      $headers. = "Content-Type: multipart/mixed;boundary = \" {$boundary} \ "\ n";
      $headers. = "Content-Transfer-Encoding: 7bit";
      $headers. = "\ n";
      //subject
      $mail_subject = "";
      $mail_subject. = "[". $product. "] Guidance";
      //Text
      $mail_body. = "-{$boundary} \ n";
      $mail_body. = "Content-Type: text/plain;charset = ISO-2022-JP;"
    "Content-Transfer-Encoding: 7bit \ n";
      $mail_body = "";
      $mail_body. = $name. "Like \ r \ n \ r \ n";
      $mail_body. = "▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ △ ▼ \ n";
      // attachment
      $mail_body. = "-{$boundary} \ n";
      $mail_body. = "Content-Type: {$mime_type};name = \" {$filename} \ "\ n".
        "Content-Transfer-Encoding: base64 \ n".
        "Content-Disposition: attachment;filename = \" {$filename} \ "\ n \ n";
      $f_encoded = chunk_split (base64_encode ($filebody));
      $fp = fopen ($filepath, "r") or die ("Error on mailing. (attachment file cannot open)");
      $contents = fread ($fp, filesize ($filepath));
      fclose ($fp);
      $mail_body. = $f_encoded. "\ n \ n";
      // send processing
      mb_send_mail ($mail_to, $mail_subject, $mail_body, $headers);
    }