Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:33:21+00:00 2026-05-25T21:33:21+00:00

I have a PHP script which sends an HTML email with an attached image.

  • 0

I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can’t get the attachment to display in an <img> tag in the email body. The attached file is called postcard.png and the original filename on the server is 4e60348f83f2f.png. I’ve tried giving the image URL as various things: cid:postcard.png, cid:4e60348f83f2f.png, postcard.png, and 4e60348f83f2f.png. Nothing works.

I think the key part that I’m doing wrong is here, because this makes it a separated attachment instead of an inline attachment that I can use:

Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$fname" // i.e.: "postcard.png"

I’ve tried changing it to use a CID but I don’t really know how to do that, and this didnt’ work at all:

Content-Transfer-Encoding: base64
Content-ID: <$fname> // i.e.: postcard.png

Here’s the full code: (It’s based on this code from a comment in the php mail() page.)

<?php
$to      = "recipient@email.com";
$email   = "sender@email.com";
$name    = "Namename";
$subject = "An inline image!"; 
$comment = "Llookout <b>Llary</b> it's <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>";

$To          = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName    =strip_tags($name);
$FromEmail   =strip_tags($email);
$Subject     =strip_tags($subject);

$boundary1   =rand(0,9)."-"
    .rand(10000000000,9999999999)."-"
    .rand(10000000000,9999999999)."=:"
    .rand(10000,99999);
$boundary2   =rand(0,9)."-".rand(10000000000,9999999999)."-"
    .rand(10000000000,9999999999)."=:"
    .rand(10000,99999);

$filename1 = "4e60348f83f2f.png"; //name of file on server with script
$handle      =fopen($filename1, 'rb'); 
$f_contents  =fread($handle, filesize($filename1)); 
$attachment=chunk_split(base64_encode($f_contents));
fclose($handle); 

$ftype       ="image/png";
$fname       ="postcard.png"; //what the file will be named


$attachments='';
$Headers     =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="$boundary1"
AKAM;

$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype;
    name="$fname"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$fname"


$attachment

ATTA;


$Body        =<<<AKAM
This is a multi-part message in MIME format.

--$boundary1
Content-Type: multipart/alternative;
    boundary="$boundary2"

--$boundary2
Content-Type: text/plain;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$TextMessage
--$boundary2
Content-Type: text/html;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$HTMLMessage

--$boundary2--

$attachments
--$boundary1--
AKAM;

// Send email
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>";
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:33:21+00:00Added an answer on May 25, 2026 at 9:33 pm

    I finally found the answer, which turns out to be remarkably simple. This page is what helped me figure it out, but I’ll demonstrate the parts that I needed to get it done below.

    First off, there’s the creation of the boundary string, and the image, correctly encoded and chunked:

    // Create a boundary string.  It needs to be unique (not in the text) so ...
    // We are going to use the sha1 algorithm to generate a 40 character string:
    $sep = sha1(date('r', time()));
    
    // Also now prepare our inline image - Also read, encode, split:
    $inline = chunk_split(base64_encode(file_get_contents('figure.gif')));
    

    In the HTML part of the email, the image is referenced like this (using the boundary string):

    <img src="cid:PHP-CID-{$sep}">
    

    Then you create another part of the email below the HTML part for the inline attachment, like this:

    --PHP-related-{$sep}
    Content-Type: image/gif
    Content-Transfer-Encoding: base64
    Content-ID: <PHP-CID-{$sep}>
    {$inline}
    

    …and that is that! Easier than implementing PHPmailer or any of the other libraries, if this is all you’re doing. No doubt for more complicated task, you’ll want to get one of those libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script which saves the original image, then resizes it -
I have a php script (generator.php) which generates certain HTML/XML/SVG depending on various conditions.
I have a script which sends an email to myself from a contact page:
I have a simple PHP script that sends a message to a specified email
I have a php script which accesses a MSSQL2005 database, reads some data from
I have a PHP script which executes a shell command: $handle = popen('python last',
I have this PHP script which i'm grabbing images from a directory and displaying
I have a PHP script which creates and listens on a port for connections
I have a PHP script (news-generator.php) which, when I include it, grabs a bunch
I have a small script which im using to test PHP mail(), as below:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.