SMTP attach file and use in template

361 views
Skip to first unread message

Dee

unread,
Apr 15, 2016, 2:55:52 PM4/15/16
to f3-fra...@googlegroups.com
Hey it's my first time using the smtp-class in a serious way.

Sending mails works like a charm, but now i want to attach images, which i use in the template i send.

My whole code:


        public function sendNewsletter(){
              $contentArray = $this->f3->get('POST');

                $this->f3->set('mailContent_attention',$contentArray['attention']);
            $this->f3->set('mailContent_headline',$contentArray['headline']);

                $order   = array("\r\n", "\n", "\r");
          $replace = '<br />';
           $contentArray['text'] = str_replace($order, $replace, $contentArray['text']);

                $this->f3->set('mailContent_content',$contentArray['text']);
           // $recipient = $contentArray['recipient'];
            $recipients = explode(",", $contentArray['recipient']);
        // Encoding
            $this->smtp->set('Content-type', 'text/html; charset=UTF8');
           // von wem
             $this->smtp->set('From','"domain.tld" <'.$this->f3->get('mailfrom').'>');
              $this->smtp->set('Reply-To','<'.$this->f3->get('mailreply').'>');
              // Betreff
             $this->smtp->set('Subject','Newsletter');
       
       $file
= 'logo.png';
       $this
->smtp->attach( './img/test/'.$file );
       
foreach ($recipients as $key => $value) {
         $this
->smtp->set('To', '<'.trim($value).'>');
         
if(!$this->smtp->send(\Template::instance()->render('/template/mail/newsletter.mail.template.html','text/html')))
           
return $value;
       
}


       
return true;


 
}




I get two different error_log messages:

 - Attachment ./img/test/logo.png not found

 - basename() expects parameter 1 to be string, array given


i tried to figure out via getcwd() in which directory it starts, but no solution.


My image is located in : /app/ui/img/test




Regards,

Dee



EDIT: new error: file_get_contents() expects parameter 1 to be a valid path, array given

 located in smtp class of fatfree at line 257


foreach ($this->attachments as $attachment) {
if (is_array($attachment['filename'])) {
list($alias,$file)=each($attachment);
$filename=$alias;
$attachment['filename']=$file;
}
else
$filename=basename($attachment);
$out.='--'.$hash.$eol;
$out.='Content-Type: application/octet-stream'.$eol;
$out.='Content-Transfer-Encoding: base64'.$eol;
if ($attachment['cid'])
$out.='Content-ID: '.$attachment['cid'].$eol;
$out.='Content-Disposition: attachment; '.
'filename="'.$filename.'"'.$eol;
$out.=$eol;
$out.=chunk_split(
base64_encode(file_get_contents($attachment))).$eol;  //line 257
}




ved

unread,
Apr 18, 2016, 6:55:18 AM4/18/16
to Fat-Free Framework
If the image is at "/app/ui/img/test" and "app" is a subfolder of your F3 root, then you should use attach('./app/ui/img/test/logo.png')

If that doesn't do it, please let us know what version of F3 you are using?

The smtp class had some issues recently and was just updated these past few hours so you can try using the version from: https://github.com/bcosca/fatfree-core and let us know if the issue still happens.

Dee

unread,
Apr 18, 2016, 8:10:42 AM4/18/16
to Fat-Free Framework
Thank you for your reply.
I used 3.5.0, edited the smtp class till it worked fine.

The version of 3.5.2 looks like my own fix.

Do you know, how i could use an attacked image in the mail template itself?

ved

unread,
Apr 18, 2016, 9:29:31 AM4/18/16
to f3-fra...@googlegroups.com
Ok, editing this as I ran some more tests and figured it out:

You'll have to attach files and set the last parameter to some value. For example:

$mail->attach('image.png', null, '<myimage>');

Then on the body's html, use: 

<img src="cid:myimage">

This should work.

ikkez

unread,
Apr 18, 2016, 9:45:02 AM4/18/16
to Fat-Free Framework
Is attaching images for inline html usage still a proper solution today? I thought it just blows up the mail message size and raises the chance of being catched by spam software. using absolute image paths to external servers has so much pros IMO:

* smaller emails that do not bloat the inbox
* the message is send faster because it's size is smaller
* images can be loaded fast from a CDN and can be exchanged / fixed even after the mail was send

but nevertheless, one thing in my mind... if attaching and embedding does not work, you could use inline base64 encoded image data instead to get it working.


Am Montag, 18. April 2016 15:29:31 UTC+2 schrieb ved:
Hi, no, not really, sorry.

I see that the attach method has a $cid parameter that I assume is there exactly for that purpose since inline images on emails usually make use of the cid on the images src parameter.

That being said, I just ran a quick test with an image and tried setting the cid parameter and then using it on an image src but it doesn't seem to work. Also, this last $cid parameter still isn't properly documented on F3's docs so I'm not sure how to use it.

Maybe we can get some feedback from bcosca or some other collaborator regarding this.

ved

unread,
Apr 18, 2016, 10:17:36 AM4/18/16
to Fat-Free Framework
I've edited by previous reply with the correct way to do this on F3 after running some more tests.

But yeah I agree with what ikkez stated, the only advantage I can think of (and I'm not even sure if this is still the case today) is that external images are more likely to be hidden from email clients with some kind of "display external images" option as gmail used to do for a while.

bcosca

unread,
Apr 18, 2016, 4:57:54 PM4/18/16
to Fat-Free Framework
E-mail providers block external images by default because of the fact that even 1px transparent images can be used to track down if your email address is alive so spammers can sell your address to other spammers and send you more useless stuff.

Dee

unread,
Apr 18, 2016, 5:22:50 PM4/18/16
to Fat-Free Framework
Exactly, if you want to bypass a typical "show images" you have to attach them.

Anatol Buchholz

unread,
Apr 19, 2016, 1:58:54 AM4/19/16
to f3-fra...@googlegroups.com
Hi Dee, 

I haven´t seen a CID attribute in F3 SMTP which you would most likely need.
If you cannot get it running with it phpmailer could jump in.

Also have a look at the cons using CID :

the article also explains the base64 option mentioned above.

With this fatfree comes into race again. Have a look at template extend:

In other words, you could write a template extend to base64 encode your images and
use SMTP class to send your message.

This said, base64 is known to be blocked as well.

(The medium is the message),
cheers,

– anatol

ved

unread,
Apr 19, 2016, 6:33:18 AM4/19/16
to Fat-Free Framework
@anatol, F3 does support Content-ID headers on the smtp class, so no need to use phpmailer or any 3rd party library.

Please see my 2nd reply on this thread. It was edited so you probably didn't see the changes.

Good article by sendgrid describing the pros and cons of every method though.

Anatol Buchholz

unread,
Apr 19, 2016, 6:48:48 AM4/19/16
to Fat-Free Framework
Hi ved,

F3 does support Content-ID headers

Thanks for pointing out!

– anatol
Reply all
Reply to author
Forward
0 new messages