Question

I am trying to send an email template for email confirmation in registration. Mail is sent, and i can receve it, but i can see the html code in email instead of button and other elemets... My Mailer class is shown in jsfiddle, i did not know how to post it differently.

My function to send email is the following:

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()
        ));
}
Was it helpful?

Solution 2

Not tested, but something like this :

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.");
        }
}

OTHER TIPS

This is quite the same with ZF2. All is in the doc : http://framework.zend.com/manual/2.2/en/modules/zend.mail.message.html

Sometimes you may want to provide HTML content, or multi-part content. To do that, you’ll first create a MIME message object, and then set it as the body of your mail message object. When you do so, the Message class will automatically set a “MIME-Version” header, as well as an appropriate “Content-Type” header.

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);

You need to get your headers correct. I'm not sure about Zend Framework, having never used it, but here is an example using 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);

The top two headers are what sets the email as html, not text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top