Jan Walraven
DevOps



Categories:
  • Arduino (2)
  • CSS (3)
  • Docker (10)
  • ESXi (1)
  • Git (4)
  • Javascript (6)
  • Linux (31)
  • Mac (6)
  • Magento (4)
  • Mysql (13)
  • PHP (9)
  • Zend framework 2 (9)
  • Posted on May 1, 2013

    Sending an HTML email with text as alternative body

    
    use ZendMail;
    use ZendMimeMessage as MimeMessage;
    use ZendMimePart as MimePart;
     
     
    function sendMail($htmlBody, $textBody, $subject, $from, $to)
    {
        $htmlPart = new MimePart($htmlBody);
        $htmlPart->type = "text/html";
     
        $textPart = new MimePart($textBody);
        $textPart->type = "text/plain";
     
        $body = new MimeMessage();
        $body->setParts(array($textPart, $htmlPart));
     
        $message = new MailMessage();
        $message->setFrom($from);
        $message->addTo($to);
        $message->setSubject($subject);
     
        $message->setEncoding("UTF-8");
        $message->setBody($body);
        $message->getHeaders()->get('content-type')->setType('multipart/alternative');
     
        $transport = new MailTransportSendmail();
        $transport->send($message);
    }
    

    Thanx to Rob Allen