0

I am trying to set html on the output of the email send by Joomla. Here is the code:

class MailtoController extends JControllerLegacy
{
/**
 * Show the form so that the user can send the link to someone.
 *
 * @return  void
 *
 * @since 1.5
 */
public function mailto()
{
    $session = JFactory::getSession();
    $session->set('com_mailto.formtime', time());
    $this->input->set('view', 'mailto');
    $this->display();

}

/**
 * Send the message and display a notice
 *
 * @return  void
 *
 * @since  1.5
 */

public function send()
{
    // Check for request forgeries
    JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

    $app     = JFactory::getApplication();
    $session = JFactory::getSession();
    $timeout = $session->get('com_mailto.formtime', 0);

    if ($timeout == 0 || time() - $timeout < 1)
    {
        JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));

        return $this->mailto();
    }

    $SiteName = $app->get('sitename');
    $link     = MailtoHelper::validateHash($this->input->get('link', '', 'post'));

    // Verify that this is a local link
    if (!$link || !JUri::isInternal($link))
    {
        // Non-local url...
        JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));

        return $this->mailto();
    }

    // An array of email headers we do not want to allow as input
    $headers = array (
        'Content-Type:',
        'MIME-Version:',
        'Content-Transfer-Encoding:',
        'bcc:',
        'cc:'
    );

    // An array of the input fields to scan for injected headers
    $fields = array(
        'mailto',
        'sender',
        'from',
        'subject',
    );

    /*
     * Here is the meat and potatoes of the header injection test.  We
     * iterate over the array of form input and check for header strings.
     * If we find one, send an unauthorized header and die.
     */
    foreach ($fields as $field)
    {
        foreach ($headers as $header)
        {
            if (strpos($_POST[$field], $header) !== false)
            {
                JError::raiseError(403, '');
            }
        }
    }

    /*
     * Free up memory
     */
    unset ($headers, $fields);

    $email           = $this->input->post->getString('mailto', '');
    $sender          = $this->input->post->getString('sender', '');
    $from            = $this->input->post->getString('from', '');
    $subject_default = JText::sprintf('COM_MAILTO_SENT_BY', $sender);
    $subject         = $this->input->post->getString('subject', $subject_default);

    // Check for a valid to address
    $error = false;

    if (!$email || !JMailHelper::isEmailAddress($email))
    {
        $error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $email);
        JError::raiseWarning(0, $error);
    }

    // Check for a valid from address
    if (!$from || !JMailHelper::isEmailAddress($from))
    {
        $error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $from);
        JError::raiseWarning(0, $error);
    }

    if ($error)
    {
        return $this->mailto();
    }

    // Build the message to send
    $msg  = JText::_('COM_MAILTO_EMAIL_MSG');
    $link = $link;
    //$body = sprintf($msg, $SiteName, $sender, $from, $link);
    $body = "<p>Hello Test F,</p><br/><p>Thank you for registering at Deals&offers. Your account is created and activated.</p><br/>You may login to ".$SiteName." using the following username and password:</br><p>Username: ".$sender."</p><p>Password: ".$from."/p><br/><p><b>Note:</b> It is recomended to change your password after first login. ".$link."</p>";

    // Clean the email data
    $subject = JMailHelper::cleanSubject($subject);
    $body    = JMailHelper::cleanBody($body);

    // To send we need to use punycode.
    $from  = JStringPunycode::emailToPunycode($from);
    $from  = JMailHelper::cleanAddress($from);
    $email = JStringPunycode::emailToPunycode($email);

    // Send the email
    if (JFactory::getMailer()->sendMail($from, $sender, $email, $subject, $body) !== true)
    {
        JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));

        return $this->mailto();
    }

    JFactory::getApplication()->enqueueMessage('ok!', '');
    $this->input->set('view', 'sent');
    $this->display();
}

}

3

I'm not sure what the actual issue is, but assuming your email is not rendering the HTML, you simply need to enable it, by using:

$mailer->isHTML(true);
0

A wrapper over send email functionality of Joomla. If $mode = 1, email will be sent in HTML. Courtesy: CoreJoomla CjLib component.

public static function send_email($from, $fromname, $recipient, $subject, $body, $mode=1, $cc=null, $bcc=null, $attachment=null, $replyto=null, $replytoname=null){

    // Get a JMail instance
    $mail = JFactory::getMailer();

    $mail->setSender(array($from, $fromname));
    $mail->setSubject($subject);
    $mail->setBody($body);

    // Are we sending the email as HTML?
    if ($mode) {

        $mail->IsHTML(true);
    }

    if(!is_array($recipient)){

        $recipient = explode(',', $recipient);
    }

    $mail->addRecipient($recipient);
    $mail->addCC($cc);
    $mail->addBCC($bcc);
    $mail->addAttachment($attachment);

    // Take care of reply email addresses
    if (is_array($replyto)) {

        $numReplyTo = count($replyto);

        for ($i=0; $i < $numReplyTo; $i++){

            $mail->addReplyTo(array($replyto[$i], $replytoname[$i]));
        }
    } elseif (isset($replyto)) {

        $mail->addReplyTo(array($replyto, $replytoname));
    }

    return  $mail->Send();
}
  • i dont exactly understand what to to with this.thank you – giorgionasis Apr 26 '15 at 17:11
  • Please see the code $mail->IsHTML(true); which sets the HTML mode to JMailer. – Nagarjun Apr 27 '15 at 18:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.