Solved

phpmailer emebedded image problems

Posted on 2010-11-16
17
2,276 Views
Last Modified: 2012-05-10
Hi Experts,

I'm using phpmailer to send automatic e-mails from a webshop. I'd like to embed 2 images into the mail body. My problem is, that the image is embedded correctly, but they are also there as attachements. As you can see in my code, I'm using a template system to get the mail contents. But no-sense when I write a plain html, the images also the as attachements. I also tried with relative path, or http path. The result is always the same.

The script I use:

      $mail = new PHPMailer();
          $mail->CharSet    ="UTF-8";
          $mail->ContentType  = "text/html";
      $mail->From = $sender_mail;
          $mail->FromName = $sender_name;
          $mail->AddAddress($custom_fields['user_email'],$custom_fields['user_full_name']);
          $mail->WordWrap = 75;
          $mail->Subject  = $subject ;
          $mail->IsHTML(true);
      $mail->AddEmbeddedImage("/ABSOLUTE/PATH/TO/IMAGE/header.jpg", "header","header.jpg", "base64", "image/jpeg");
      $mail->AddEmbeddedImage("/ABSOLUTE/PATH/TO/IMAGE/footer.jpg", "header","footer.jpg", "base64", "image/jpeg");
          $body = $page->output();
          $mail->AltBody  =  strip_tags(nl2br($body));
      $body = str_replace("[\]",'',$body);
      $mail->MsgHTML($body);
      $mail->Send();
0
Comment
Question by:clickwave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
17 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34144305
in which email client you had seen ?
0
 

Author Comment

by:clickwave
ID: 34144310
In Apple Mail. In webmailers or thunderbird you can't see any image.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34144328
may be you need to make some settings to allow / display the images instead of attachment.
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34144338
check on those email clients whether they allow to display image settings
0
 

Author Comment

by:clickwave
ID: 34144347
it's enabled. :( Image settings
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34144393
problem is that PHPMailer embeds inline images relative to
where the script is called from, instead of relative to the document
called in getFile().

0
 

Author Comment

by:clickwave
ID: 34144425
boraganesh: and how could I fix that?
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34144458
put the images path relative to phpmailerscript file
0
 

Author Comment

by:clickwave
ID: 34144548
Well, I put the image right beside the script, and my script changed:

...
        $mail->AddEmbeddedImage("header.jpg", "header_img","header.jpg","base64","image/jpeg");


        $body             = "<html><body><img src='cid:header_img' alt=''/></body></html>";

And the result is:
Screen-shot-2010-11-16-at-1.29.5.png
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34144593
pls check the phpmailer.php file..

function AddEmbeddedImage {

$this->attachment[$cur][6] = "inline";
}

Open in new window


0
 

Author Comment

by:clickwave
ID: 34144620
I don't have exactly this code:
public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
    // Append to $attachment array
    $this->attachment[] = array(
      0 => $string,
      1 => $filename,
      2 => basename($filename),
      3 => $encoding,
      4 => $type,
      5 => true,  // isStringAttachment
      6 => 'attachment',
      7 => 0
    );
  }

  /**
   * Adds an embedded attachment.  This can include images, sounds, and
   * just about any other document.  Make sure to set the $type to an
   * image type.  For JPEG images use "image/jpeg" and for GIF images
   * use "image/gif".
   * @param string $path Path to the attachment.
   * @param string $cid Content ID of the attachment.  Use this to identify
   *        the Id for accessing the image in an HTML form.
   * @param string $name Overrides the attachment name.
   * @param string $encoding File encoding (see $Encoding).
   * @param string $type File extension (MIME) type.
   * @return bool
   */
  public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = '') {

    if ( !@is_file($path) ) {
      $this->SetError($this->Lang('file_access') . $path);
      return false;
    }

    $filename = basename($path);
    if ( $name == '' ) {
      $name = $filename;
    }

    // Append to $attachment array
    $this->attachment[] = array(
      0 => $path,
      1 => $filename,
      2 => $name,
      3 => $encoding,
      4 => $type,
      5 => false,  // isStringAttachment
      6 => 'inline',
      7 => $cid
    );

    return true;
  }

  /**
   * Returns true if an inline attachment is present.
   * @access public
   * @return bool
   */
  public function InlineImageExists() {
    foreach($this->attachment as $attachment) {
      if ($attachment[6] == 'inline') {
        return true;
      }
    }
    return false;
  }

Open in new window

0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34144688
pls send test mail to me boraganesh@gmail.com so i can invistigate it
0
 

Author Comment

by:clickwave
ID: 34144738
sent!:)
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34144857
i have cheked teh mail...its the right behaviour..

if you would like to send inline images send it as links which will get downloaded while displying.. but presently they are ok..
0
 

Author Comment

by:clickwave
ID: 34154011
Sorry for the late answer. So you can't see any other attachment, or that's the normal? :) I promise, this is my last question :)
0
 
LVL 3

Accepted Solution

by:
boraganesh earned 500 total points
ID: 34154032
its showing attachment..but its normal behavior..so you don't have to worry about it..go ahead with what you have right know..:)
0
 

Author Closing Comment

by:clickwave
ID: 34158222
Thank you for your time!:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question