我正在尝试发送电子邮件模板,以便在注册时确认电子邮件。邮件被发送,我可以收到它,但我可以看到电子邮件中的HTML代码而不是按钮和其他元素...... 我的Mailer class is shown in jsfiddle,我不知道如何以不同的方式发布。
我发送电子邮件的功能如下:
public function sendConfirmationMail($user) {
$urlPlugin = $this->getServiceLocator()->get('url_plugin');
$mailer = $this->getServiceLocator()->get('mailer');
$mailer->getMessage()
->addTo($user->getContactEmail())
->setSubject($this->getServiceLocator()->get('translator')->translate('Confirmation'));
$mailer->sendMessage('user/user/mail/confirm',
array(
'user' => $user->getName(),
'confirm_url' => \App\Application::getBaseUrl() . $urlPlugin->fromRoute('user_confirm_rs', array()) . '?code=' . md5($user->getId() . '@' . $user->getConfirmcode()),
'valid_until' => $user->getConfirmationValidUntil()
));
}
答案 0 :(得分:4)
这与ZF2完全相同。 所有内容都在文档中:http://framework.zend.com/manual/2.2/en/modules/zend.mail.message.html
有时您可能想要提供HTML内容或多部分内容。 为此,您首先要创建一个MIME消息对象,然后进行设置 作为邮件消息对象的正文。当你这样做时,消息 class会自动设置一个“MIME-Version”标题,以及一个 适当的“Content-Type”标题。
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
$text = new MimePart($textContent);
$text->type = "text/plain";
$html = new MimePart($htmlMarkup);
$html->type = "text/html";
$image = new MimePart(fopen($pathToImage, 'r'));
$image->type = "image/jpeg";
$body = new MimeMessage();
$body->setParts(array($text, $html, $image));
$message = new Message();
$message->setBody($body);
答案 1 :(得分:1)
你需要让你的标题正确。我不确定Zend Framework,从未使用它,但这是一个使用PHP mail() function的例子:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
前两个标题是将电子邮件设置为html而不是文本
答案 2 :(得分:1)
未经过测试,但是这样:
public function sendMessage($messageTemplate, $data = array()) {
$messageView = new ViewModel($data);
$messageView->setTemplate($messageTemplate);
$messageView->setTerminal(true);
$config = \App\Application::getInstance()->getConfig();
$html = $this->getMessageBodyRenderer()
->setResolver(new \Zend\View\Resolver\TemplatePathStack(array(
'script_paths' => $config['view_manager']['template_path_stack']
));
$htmlPart = new MimePart($html);
$htmlPart->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html));
$this->message->setBody($body);
if ($this->message instanceof Message) {
$this->getTransportEngine()->send($this->message);
} else {
throw new \Exception("Invalid message object. First create new message then send.");
}
}