Solved

phpmailer emebedded image problems

Posted on 2010-11-16
17
2,192 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now