Solved

phpmailer emebedded image problems

Posted on 2010-11-16
17
2,204 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

932 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

9 Experts available now in Live!

Get 1:1 Help Now