How to find 'Content-ID' of an attachment

Discussion in 'PHP' started by rickey80, Feb 26, 2009.

  1. #1
    Hi everyone,
    I have coded an html email script in php. What I want to do is send an email with image embedded in the body. I can do it by using <img src = image path> method. But outlook will block those html email due to security risk. So I found a solution which first attach the image as an attachment and then reference that attachment into the html code using the Content-ID of the attachment. And use it in the img tag like this, <img src = cid:Content-ID>

    My problem is I don't know how to find content-id of an attachment. So I guess one and use it as follows. But it's not working. If anyone can please support me in this regard and let me know where I have done wrong I will be very faithful.

    <?php
    $to = "myemail@yahoo.com"; //this is testing mail address
    $subject = "Mime mails are very useful";
    $from = "anymail@yahoo.com";

    $fileattname = "/images/845104292.jpg";

    $semi_rand = md5( time() );
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers = "From: $from";
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $fileatt = "../images/845104292.jpg"; //here no need to use another variable for this. But for testing purposes I use it.

    $file = fopen( $fileatt, 'rb' );
    $data = fread( $file, filesize( $fileatt ) );
    fclose( $file );



    $message = "--{$mime_boundary}\n" .
    "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    "\n<html><body><p>This is body of the message<img border=\"0\" src = \"cid:nnn@mime.mail\" width=\"99\" height=\"131\"></p></body></html>\n\n";

    $data = chunk_split( base64_encode( $data ) );

    $message .= "--{$mime_boundary}\n" .
    "Content-Type: image/jpeg;\n" .
    " name=\"{$fileattname}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"845104292.jpg\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n Content-ID: <nnn@mime.mail>" .
    "--{$mime_boundary}--\n";

    $qry = mail($to, $subject, $message, $headers);


    if ($qry)
    {
    echo "EMail Sent";
    }
    else
    {
    echo "EMail Not sent";
    }
    ?>

    Thank you
    Rickey
     
    rickey80, Feb 26, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There's no special magic value for Content-ID; just use some random string @ your domain. It should come before the data, though, as part of the headers - not after.
     
    SmallPotatoes, Feb 26, 2009 IP
  3. rickey80

    rickey80 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Dear SmallPotatoes,
    Thank you very much for your guidance. I have just created a content ID as you said but I was bit confused how to define content ID in header section. I have used multipart/mixed as content type in header section. If I'm using content ID, how the content type should change? Please guide me again to solve this problem. It's great help to me. Thanks in advance.
     
    rickey80, Mar 11, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not really sure what your question is. In theory you should use multipart/related if you want to reference the images within the HTML. With multipart/mixed it's more like they're attachments, and the client will decide whether and where to display them.
     
    SmallPotatoes, Mar 11, 2009 IP
  5. rickey80

    rickey80 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Dear SmallPotatoes,
    Thanks for your reply. My problem is where to define the content ID? Is it inside header part or in the message. If it is in message, then no problem. But earlier reply you mentioned define it in header section. I can't think how to define it inside header section. If you can please correct my script and post here. That would be a great help. Sorry for troubling you. Thank you very much.
     
    rickey80, Mar 11, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In a MIME message, each MIME part has its own header, which follows the separator (what you have as $mime_boundary). The Content-ID for any individual part is defined there, along with its Content-Type (image/jpeg or whatever).
     
    SmallPotatoes, Mar 12, 2009 IP