Search code examples
multipartfat-free-framework

Sending multipart text and html email in Fat Free Framework


Currently I'm using the following code to send my e-mails in Fat Free Framework:

$smtp = new SMTP ( $f3->get('MAILHOST'), $f3->get('MAILPORT'), $f3->get('MAILPROTOCOL'), $f3->get('MAILUSER'), $f3->get('MAILPW') );
        $smtp->set('Content-type', 'text/html; charset=UTF-8'); 
        $smtp->set('Errors-to', '<$my_mail_address>');
        $smtp->set('To', "<$my_mail_address>");
        $smtp->set('From', '"$my_mailer_name" <$my_mail_address>');
        $smtp->set('Subject', "$subject");
        $smtp->set('Date', date("r"));
        $smtp->set('Message-Id',generateMessageID());
$smtp->send(Template::instance()->render('emails/'.$mailTemplate.'.html'));

And it works like a charm. But I would like to add a text version to this e-mail.

Is there a way to do this within the Fat Free Framework smtp plugin? If so, how should I do this? And if not, how else should I do this in F3?


Solution

  • Actually it can send a multiplart text + html mail. The SMTP class is just a protocol implementation, so it might feel a little bit bare-bone at this point. You basically need to prepare your mail body with the multipart like this:

    $text = 'Hello world.';
    $html = 'Hello <b>world</b>';
    
    $smtp = new \SMTP();
    $hash=uniqid(NULL,TRUE);
    $smtp->set('From', 'info@domain.com');
    $smtp->set('To', 'info@domain.com');
    $smtp->set('Content-Type', 'multipart/alternative; boundary="'.$hash.'"');
    $smtp->set('Subject', 'Multipart test');
    
    $eol="\r\n";
    $body  = '--'.$hash.$eol;
    $body .= 'Content-Type: text/plain; charset=UTF-8'.$eol;
    $body .= $text.$eol.$eol;
    $body .= '--'.$hash.$eol;
    $body .= 'Content-Type: text/html; charset=UTF-8'.$eol.$eol;
    $body .= $html.$eol;
    
    $smtp->send($body,true);