-2

I am trying to inline embed an uploaded image file in an email using phpmailer but the image can not be embedded. The email has broken image.

Here's the code which I am using:

if(isset($_FILES['file1']['name']) && $_FILES['file1']['error'] == 0)
{
    $file_name = $_FILES['file1']['name'];
    $file_type = $_FILES['file1']['type'];
    $temp_file = $_FILES['file1']['tmp_name'];

    $file_name = strtolower(basename($file_name));
    $file_name = preg_replace("/\s+/", "-", $file_name);

    $image_file_type = pathinfo($file_name, PATHINFO_EXTENSION);
    $allowed_file_types = array("jpg", "jpeg", "png", "gif");

    // Check if image file is a actual image or fake image
    $check = getimagesize($temp_file);
    if($check !== false)
    {
        // Allow certain file formats
        if(in_array($image_file_type, $allowed_file_types))
        {
            // Open the file and read its content
            $file_handle = fopen($temp_file, "rb");
            $data = fread($file_handle, filesize($temp_file));
            fclose($file_handle);

            $attachment = base64_encode($data);
            $mail->AddEmbeddedImage($attachment, "logo");   
        }
    }
}


$mail->Body .= "<img src='cid:logo' alt='logo' />";

Relevant HTML used is:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">

<label for="file1">Attach File:</label>
<input type="file" name="file1" id="file1" />

In email, I am getting following broken image:

enter image description here

Where is I am doing wrong?

EDIT:

My question is totally different. It is NOT a duplicate of Send email with PHPMailer - embed image in body

I am dealing with dynamic uploaded file and not with a static image file.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Sachin
  • 1,646
  • 3
  • 22
  • 59
  • 1
    Possible duplicate of [Send email with PHPMailer - embed image in body](https://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – ᴄʀᴏᴢᴇᴛ Aug 31 '18 at 15:19
  • your html is incomplete; did you not close off the form here? – Funk Forty Niner Aug 31 '18 at 15:30
  • `$mail->Body .=` - that is also unclear; did you not start off with a `$mail->Body = "...";` ? – Funk Forty Niner Aug 31 '18 at 15:31
  • @ᴄʀᴏᴢᴇᴛ that edit I believe was meant for your flag as. – Funk Forty Niner Aug 31 '18 at 15:31
  • I only put my relevant code there, no need to write unnecessary code. – Sachin Aug 31 '18 at 15:34
  • well check for errors; if you feel irrelevance, then I can't be sure of missing syntax. I'll have to pass on this one, because I feel there stands to be too many comments asking for clarification; sorry. – Funk Forty Niner Aug 31 '18 at 15:36
  • no error at all, only broken image. do i need to move uploaded file to some folder in order to inline embed? Why temporary uploaded file or using fread() don't work even? – Sachin Aug 31 '18 at 15:37
  • 1
    This *is* a duplicate because there is no difference between a local static file and an uploaded file. You have two parts to what you're trying to do: 1) store an uploaded file 2) attach an image file to an email from the local file system. Solve those things separately. The first is covered by the file upload examples provided with PHPMailer, the second by reading the docs for `addEmbeddedImage`. – Synchro Sep 01 '18 at 17:22
  • @Synchro I'm trying out this way which you told but not successful yet. Do I need to use move_uploaded_file() to save uploaded file to some folder on my server before attaching it with phpmailer. Because uploaded temp file is not read by fread() and inline attached by AddEmbeddedImage(). I'm getting broken image i.e. no inline image. – Sachin Sep 03 '18 at 05:50
  • can you show the code you used with addEmbeddedImage() ? – ᴄʀᴏᴢᴇᴛ Sep 03 '18 at 07:25
  • Yes, you do need to use `move_upload_file` to handle uploads safely, just as [the file upload example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps) shows. We've not seen any evidence that your upload is working at all - your code just assumes that it is and does no error checking. As others have pointed out, `addEmbeddedImage` expects a path to a file, not pre-encoded binary data. – Synchro Sep 03 '18 at 09:12
  • the code finally did work after using move_upload_file() – Sachin Sep 03 '18 at 10:35

1 Answers1

1

if you look at the doc for addEmbeddedImage(), you will see that the function expects a path to the image file and not a base64 string.

you have to use addStringEmbeddedImage() if you want to embed an image from base64.

ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
  • well, i also tried without base64_encode(), didn't work. i tried temp file and even fread. none of them worked – Sachin Aug 31 '18 at 15:52
  • i don't understand how uploaded file will go to embed inline. – Sachin Aug 31 '18 at 15:52
  • and did you try with `addStringEmbeddedImage()` ? if it still does not work, can you post the html output of the mail ? – ᴄʀᴏᴢᴇᴛ Aug 31 '18 at 15:53
  • but how to use addEmbeddedImage() for uploaded file? – Sachin Aug 31 '18 at 16:49
  • just save the uploaded file somewhere and give the path to `addEmbeddedImage()` – ᴄʀᴏᴢᴇᴛ Sep 03 '18 at 07:23
  • @CROZET oh so we must have to save our uploaded file somewhere on my server so that it can be used for addEmbeddedImage() function. That's what I keep on asking for ages but none has replied me to the point. – Sachin Sep 03 '18 at 07:42
  • Yes we have. It's exactly the solution suggested in the first comment posted, as well as this answer, and the official docs linked to from this answer say the same. – Synchro Sep 03 '18 at 09:18