Email problem in email ()

I am trying to send an image on my server as an attachment. To complete this task, I used the following PHP script that captures a JPG (called "php.jpg") located in a directory called "screenshots" from my server and sends it as an attachment.

<?php

$path = "screenshots/php.jpg";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
        $data = fread($fp, 8192);
        if (strlen($data) == 0) break;
        $content .= $data;
      } while (true);
$content_encode = chunk_split(base64_encode($content));


$mime_boundary = "<<<--==+X[".md5(time())."]";

$headers .= "From: Automatic <an.e.mail@domain.net>\r\n"; 
$headers .= "To: SomeName <me@gmail.com>\r\n"; 

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";

$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "Email content and what not: \r\n";
$message .= "This is the file you asked for! \r\n";
$message .= "--".$mime_boundary."\r\n";

$message .= "Content-Type: image/jpeg;\r\n";
$message .= " name=\"php.jpg\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"php.jpg\"\r\n";
$message .= "\r\n";
$message .= $content_encode;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";

$ok = mail("me@gmail.com", "file by email", $message, $headers);

In general, the script works. I receive an email in my inbox containing the message text above and a JPG attachment. Stack overflow will not allow me to post a photo because I'm new, but a screenshot is available here: http://i48.tinypic.com/xfuee0.png

My problem arises when I try to view an attachment. When you click on an attachment, a new browser window simply opens and the icon for the missing image is displayed.

- script, ?

. !

+3
3

, , "Content-Transfer-Encoding", base64.

$message .= "Content-Transfer-Encoding: quoted-printable\r\n";

:

$message .= "Content-Transfer-Encoding: base64\r\n";
+3

, . ( (!).)

:

$message .= "--".$mime_boundary."\r\n"; 

$message .= "--".$mime_boundary."--\r\n"; 

(.. "" ).

0

:

-, $content $message $headers .

$headers = "From: Automatic <an.e.mail@domain.net>\r\n";

, :

$headers .= "From: Automatic <an.e.mail@domain.net>\r\n";

, .

Secondly, it exists \r\ninstead of plain \n, which should work on every system, even on Windows. I doubt this is a problem.

Thirdly, the border of the closing mime does not coincide with the open.

0
source

Source: https://habr.com/ru/post/1727717/


All Articles