简体   繁体   中英

Laminas-mail. send as UFT8

I work with Laminas (former Zendframework 3). I want to send a mail with Laminas-Mail. I dont know how to change the Content-Type . Currently it is Content-Type: text/html but I want to send UTF8 mails, so therefore I want to use Content-Type: text/html; charset=utf-8 Content-Type: text/html; charset=utf-8 .

I tried to change $html->type but I failed.

use Laminas\Mail;
use Laminas\Mail\Transport\Smtp as SmtpTransport;
use Laminas\Mail\Transport\SmtpOptions;
use Laminas\Mime\Message as MimeMessage;
use Laminas\Mime\Part as MimePart;

# ...
# much code
# ...

// Produce HTML
$bodyHtml = $this->viewRenderer->render('mymodule/email/email1');
$html = new MimePart($bodyHtml);
$html->type = "text/html; charset=utf-8"; #Seems not to work
$body = new MimeMessage();
$body->addPart($html);
$mail = new Mail\Message();
$mail->setEncoding('UTF-8');
$mail->setBody($body);
$mail->setFrom('bobafit@example.com','Boba Fit');
$mail->addTo('bobafit@example.com','Boba Fit');
$mail->setSubject('Thank you for reading my Question');
// Setup SMTP transport
$transport = new SmtpTransport();
$options   = new SmtpOptions($this->config['smtp']);
$transport->setOptions($options);
$transport->send($mail);

Please check the official Dok:

https://docs.laminas.dev/laminas-mail/message/attachments/

Type and charset are defined separatly.

You are probably looking for something like that:

$html = new MimePart($htmlMarkup);
$html->type = Mime::TYPE_HTML;
$html->charset = 'utf-8';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM