Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2374
  • Last Modified:

phpmailer emebedded image problems

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
clickwave
Asked:
clickwave
  • 8
  • 6
  • 3
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
in which email client you had seen ?
0
 
clickwaveAuthor Commented:
In Apple Mail. In webmailers or thunderbird you can't see any image.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
may be you need to make some settings to allow / display the images instead of attachment.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Loganathan NatarajanLAMP DeveloperCommented:
check on those email clients whether they allow to display image settings
0
 
clickwaveAuthor Commented:
it's enabled. :( Image settings
0
 
boraganeshCommented:
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
 
clickwaveAuthor Commented:
boraganesh: and how could I fix that?
0
 
boraganeshCommented:
put the images path relative to phpmailerscript file
0
 
clickwaveAuthor Commented:
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
 
boraganeshCommented:
pls check the phpmailer.php file..

function AddEmbeddedImage {

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

Open in new window


0
 
clickwaveAuthor Commented:
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
 
boraganeshCommented:
pls send test mail to me boraganesh@gmail.com so i can invistigate it
0
 
clickwaveAuthor Commented:
sent!:)
0
 
boraganeshCommented:
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
 
clickwaveAuthor Commented:
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
 
boraganeshCommented:
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
 
clickwaveAuthor Commented:
Thank you for your time!:)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 8
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now