[SOLVED] Images missing in HTML attachment (using FileEmbedd

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
snursita
Forum Newbie
Posts: 8
Joined: Thu Jul 12, 2007 2:58 am

[SOLVED] Images missing in HTML attachment (using FileEmbedd

Post by snursita »

Hello,

I want to send email with 4 HTML attachments in it, each of the attachment have images.
The problems:
1. The images in the HTML attachments are missing. I already use the server path for images in the attachment (for example <img src='/server/img.jpg'>).
2. All the attachments are from Page 4 instead of from Page 1, Page 2, Page 3 and Page 4.

Is it possible to have images in HTML Attachment using the FileEmbedder plugin? Is there something wrong in my code so that all the attachments are from the last attachment?

I use PHP 4.3.9 and Swift 3.2.6.

Please help, Thanks!

Code: Select all

class CommissionMailer {
  var $commission;
    function CommissionMailer(){}
    function mailNewCommission($commissionid) {
      if(!empty($commissionid)) {
        $this->commission = new CommissionForm();
	$this->commission->id = $commissionid;
	$details = $this->commission->getDetails();		
	$prefix = "<html><body>";
	$suffix = "</body></html>";

        $page1 = $prefix.$this->commission->getPage(1, $details, true).$suffix;
  	$page2 = $prefix.$this->commission->getPage(2, $details, true).$suffix;
  	$page3 = $prefix.$this->commission->getPage(3, $details, true).$suffix;
  	$page4 = $prefix.$this->commission->getPage(4, $details, true).$suffix;

	$a1 = new Swift_Message_Attachment($page1,"commission-p1.html","text/html");
	$a2 = new Swift_Message_Attachment($page2,"commission-p2.html","text/html");
	$a3 = new Swift_Message_Attachment($page3,"commission-p3.html","text/html");
	$a4 = new Swift_Message_Attachment($page4,"commission-p4.html","text/html");

	$attachments = array();
	array_push($attachments, $a1);
	array_push($attachments, $a2);
	array_push($attachments, $a3);
	array_push($attachments, $a4);

	$ss = new SwiftSend();
	$message ="Please see Commission/Fee Request Forms attached.";
	$subject = "[Commission/Fee Request] Submitted: ".$details['client_company'];
	$requestor = new User();
	$requestor->id = $details['receiverid'];
	$requestor->getDetails();
	$sendto = array($requestor->email);
	$ss->sendWithAttachment($message, $subject, $sendto, $attachments);
    }
  }
}
class SwiftSend {
  var $swift;
  var $smtp;

  function SwiftSend() {
    $smtp_path = "localhost"; 
    $this->smtp =& new Swift_Connection_SMTP($smtp_path, 25);
    $this->swift =& new Swift($this->smtp);
    $this->swift->attachPlugin(new Swift_Plugin_AntiFlood(50, 10), "anti-flood");	
    $this->swift->attachPlugin(new Swift_Plugin_FileEmbedder(), "file_embedder");
  }
  function sendWithAttachment($message, $subject, $arrayto=null, $attachments=null, $from='', $replyto='', $arraycc=null, $arraybcc=null) {
    $this->swift->log->enable();
    $this->swift->log->setMaxSize(1);
    if(!empty($from)) $sender = $from;
    else $sender = new Swift_Address("publisher@domain.com", "Intranet");
    if(empty($replyto)) {
      $replyto = new Swift_Address("nursita@domain.com", "Nursita");
    }
    $swiftmessage =& new Swift_Message();
    $swiftmessage->attach(new Swift_Message_Part($message, 'text/html'));
    $swiftmessage->setReplyTo($replyto);
    $swiftmessage->setSubject($subject);

    $recipients =& new Swift_RecipientList();
    $recipients->addBcc($replyto);
    if(is_array($arrayto) or is_email($arrayto)) {
      $continue = false;
      if(!is_null($arrayto) and count($arrayto) != 0) {
        foreach(@$arrayto as $toemail) {
  	  if((!empty($toemail) and is_email($toemail)) or is_a($toemail,"Swift_Address"))
  	    $recipients->addTo($toemail);
  	  }
  	  $continue = true;
  	}
  	if(is_email($arrayto)) {
	  $recipients->addTo($arrayto);
  	  $continue = true;
  	}
  	if($continue) {
    	  if(!is_null($arraycc) and count($arraycc) != 0) {
    	    foreach(@$arraycc as $ccemail) {
    	      if(!empty($ccemail) or is_a($ccemail,"Swift_Address"))
    		$recipients->addCc($ccemail);
    	      }
    	    }
    	    if(!is_null($arraybcc) and count($arraybcc) != 0) {
    	      foreach(@$arraybcc as $bccemail) {
                if(!empty($bccemail) or is_a($bccemail,"Swift_Address"))
                  $recipients->addBcc($bccemail);
              }
            }
    	    if(!is_null($attachments) and count($attachments) != 0) {
    	      foreach(@$attachments as $a) {
                $swiftmessage->attach($a);
    	      }
    	    }
    	    if ($this->swift->send($swiftmessage, $recipients, $sender))
 	      return "Message sent";
            else
 	      return "<font color=red>Sorry, the system failed to send your email</font>.<br>Errors: ".$this->swift->log->dump();
            $this->swift->disconnect();
    	}
    } 
    $this->swift->log->disable();
  }
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hi Sorry, I'm not following your code. I see you include the FileEmbedder plugin, but I don't see the message source which would invoke its usage. If you're using FileEmbedder you would not need to attach the images since the plugin will do that.

it scan for strings like this in your message body and then attaches the images itself.

Code: Select all

Hello <img src="/path/to/world.jpg" />
Can you post a use case example of your code please?
snursita
Forum Newbie
Posts: 8
Joined: Thu Jul 12, 2007 2:58 am

Post by snursita »

If you see on the CommissionMailer class, the attachment is made by calling a getPage function that will return a string of HTML.

Code: Select all

$prefix = "<html><body>";
  $suffix = "</body></html>";

  $page1 = $prefix.$this->commission->getPage(1, $details, true).$suffix;
  $page2 = $prefix.$this->commission->getPage(2, $details, true).$suffix;
  $page3 = $prefix.$this->commission->getPage(3, $details, true).$suffix;
  $page4 = $prefix.$this->commission->getPage(4, $details, true).$suffix;

  $a1 = new Swift_Message_Attachment($page1,"commission-p1.html","text/html");
  $a2 = new Swift_Message_Attachment($page2,"commission-p2.html","text/html");
  $a3 = new Swift_Message_Attachment($page3,"commission-p3.html","text/html");
  $a4 = new Swift_Message_Attachment($page4,"commission-p4.html","text/html");
The HTML string returned by getPage method is made by grabbing a template and subtitutes the fields in "[fieldname]" format with relevant info, including the image paths with appropriate path.

Code: Select all

define ("EMAIL_IMAGES ","/home/sites/mydomain.com/www/images/");
define ("INT_IMAGES ","http://mydomain.com/images/");
class CommissionForm {
  function getPage($page, $comm=null, $email=false) {
    $content = file_get_contents("templates/commission-page".$page.".html");
    // Only these lines are related to image display; I use this function to display images both on web page and email
    if($email) $imgsrc = EMAIL_IMAGES;
    else $imgsrc = INT_IMAGES;

    // Replacing logo image in page 1, page 2, page 3, page 4
    $content = str_replace("[img_path]", $imgsrc, $content);

    // Replacing the checkbox images in the template - page 1
    $client_tobill = $resident_tobill = $landlord_tobill = $broker_tobill = $imgsrc . "x.jpg";
    if(isset($comm['client_tobill']) and $comm['client_tobill']=="Y") $client_tobill = $imgsrc . "y.jpg";
      $content = str_replace("[client_tobill]", $client_tobill, $content);
    if(isset($comm['resident_tobill']) and $comm['resident_tobill']=="Y") $resident_tobill = $imgsrc . "y.jpg";
      $content = str_replace("[resident_tobill]", $resident_tobill, $content);
    if(isset($comm['landlord_tobill']) and $comm['landlord_tobill']=="Y") $landlord_tobill = $imgsrc . "y.jpg";
      $content = str_replace("[landlord_tobill]", $landlord_tobill, $content);
    if(isset($comm['broker_tobill']) and $comm['broker_tobill']=="Y") $broker_tobill = $imgsrc . "y.jpg";
      $content = str_replace("[broker_tobill]", $broker_tobill, $content);

    // Replacing the checkbox images in the template - page 2, page 3, page 4
    $n = "<img src='".$imgsrc . "x.jpg'>";
    $y = "<img src='".$imgsrc . "y.jpg'>";
  
    $sp_consulting = $sp_orientation = $sp_valuation = $sp_proprep = $sp_tenantrep = $sp_audit 
    = $sp_prm = $sp_fm = $sp_pm = $sp_other = $n;
    if(isset($comm['sp_consulting']) and $comm['sp_consulting']=='Y') $sp_consulting = $y;
    if(isset($comm['sp_orientation']) and $comm['sp_orientation']=='Y') $sp_orientation = $y;
    if(isset($comm['sp_valuation']) and $comm['sp_valuation']=='Y') $sp_valuation = $y;
    if(isset($comm['sp_proprep']) and $comm['sp_proprep']=='Y') $sp_proprep = $y;
    if(isset($comm['sp_tenantrep']) and $comm['sp_tenantrep']=='Y') $sp_tenantrep = $y;
    if(isset($comm['sp_audit']) and $comm['sp_audit']=='Y') $sp_audit = $y;
    if(isset($comm['sp_prm']) and $comm['sp_prm']=='Y') $sp_prm = $y;
    if(isset($comm['sp_fm']) and $comm['sp_fm']=='Y') $sp_fm = $y;
    if(isset($comm['sp_pm']) and $comm['sp_pm']=='Y') $sp_pm = $y;
    if(isset($comm['sp_other']) and $comm['sp_other']=='Y') $sp_other = $y;
    $content = str_replace("[sp_consulting]", $sp_consulting, $content);  		
    $content = str_replace("[sp_orientation]", $sp_orientation, $content);  		
    $content = str_replace("[sp_valuation]", $sp_valuation, $content);  		
    $content = str_replace("[sp_proprep]", $sp_proprep, $content);  		
    $content = str_replace("[sp_tenantrep]", $sp_tenantrep, $content);  		
    $content = str_replace("[sp_audit]", $sp_audit, $content);  		
    $content = str_replace("[sp_prm]", $sp_prm, $content);  		
    $content = str_replace("[sp_fm]", $sp_fm, $content);  		
    $content = str_replace("[sp_pm]", $sp_pm, $content);  		
    $content = str_replace("[sp_other]", $sp_other, $content);  		
    $content = str_replace("[sp_other_val]", $comm['sp_other_val'], $content);  		

    return $content;
  }
}
commission-page1.html

Code: Select all

<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td align="center">  
      <font style="font-size:12px"><b>COMMISSION/FEE REQUEST FORM - PAGE 1</b></font>
      <br>[client_company]
      <br>[resident_name]
      <br>[landlord_currentpremises]
    </td>
    <td width="1%"><img src="[img_path]logo-small.jpg"></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <td colspan="2">
      <table width="100%" border="0" cellspacing="1" cellpadding="1">
        <tr bgcolor="#CCCCCC">
          <td><b><i>TENANT/INVESTOR/CLIENT (IF APPLICABLE)</i></b></td>
          <td width="50%" align="right"><b>TO BILL</b> <img src="[client_tobill]"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<!-- Etc etc page 1 details !-->
commission-page2.html

Code: Select all

<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td align="center">  
      <font style="font-size:10px"><b>COMMISSION/FEE REQUEST FORM - PAGE 2</b></font>
      <br>[client_company]
      <br>[resident_name]
      <br>[landlord_currentpremises]
		</td>
    <td width="1%"><img src="[img_path]logo-small.jpg"></td>
  </tr>
</table>
<!-- Etc etc page 2 details !-->
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <td valign="top">
    <table width="100%" border="0" cellspacing="1" cellpadding="1">
      <tr bgcolor="#CCCCCC">
        <td><b>Services Provided</b></td>
      </tr>
      <tr><td>[sp_consulting] Consulting Study</td></tr>
      <tr><td>[sp_orientation] Orientation</td></tr>
      <tr><td>[sp_valuation] Valuation</td></tr>
      <tr><td>[sp_proprep] Property Representation</td></tr>
      <tr><td>[sp_tenantrep] Tenant/Investor Representation</td></tr>
      <tr><td>[sp_audit] Audit</td></tr>
      <tr><td>[sp_prm] Project Management</td></tr>
      <tr><td>[sp_fm] Facilities Management</td></tr>
      <tr><td>[sp_pm] Property Management</td></tr>
      <tr><td>[sp_other] Other: [sp_other_val]</td></tr>
    </table>
  </td>
</table>
<!-- Etc etc page 2 details !-->
commission-page3.html and commission-page4.html basically have the same parts as page 2 but have more details after that.

Here's the result: screenshot

This CommissionMailer class will display the images just fine if I merge Page 1-4 and put that as the email body instead of as HTML attachments.

Code: Select all

// If I don't add attachment and put this as email body, the images are shown just fine
  $message =$prefix.$page1."<hr>".$page2."<hr>".$page3."<hr>".$page4."<hr>".$suffix;
Sorry if I didn't make myself clear.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ah, I think I see what you're doing. Are you trying to send some HTML attachments (forms to complete) and wanting images to be embedded inside those attachements? That won't work unfortunately since once the attachment is downloaded any content-id values will be out of scope. You'd need remote images for that (i.e. starting with http://).

If you were simply trying to embed images directly into the message body it would work since the images would remain in scope. Hopefully I've understood you correctly this time :)
snursita
Forum Newbie
Posts: 8
Joined: Thu Jul 12, 2007 2:58 am

Post by snursita »

Wahh, okay now I understand. I thought the plugin will embed the images in the HTML file itself :D

Do you know why it's sending the same four attachments (commission-p4.html) though? Maybe I should use PDF attachment instead and see if it's still sending the same attachments.

Thanks!
Post Reply