2

I have a component that I built to make calculations. I would like to allow the person to email their results to themselves. I have tried to create a mail function in the controller of the default.php for the tmpl page, shown here:

public  function sendMail () {

$mailer = JFactory::getMailer();

$config = JFactory::getConfig();

$sender = array(
    $config->get( 'config.mailfrom' ),
    $config->get( 'config.fromname' ) );
$mailer->setSender($sender);

$recipient = $calculatorEmailAddress;

$mailer->addRecipient($recipient);


$body = '<div id="emtresults">'
    . '<table border="0" width="75%" max-width="750px" cellpadding="1" cellspacing="1" class="table">'
    . '<tr class="calcresultslabels">'
    . '  <th> </th>'
    . '    <th>10foot</th>'
    . '    <th>20foot</th>'
    . '   <th>Savings</th>'
    . '   </tr>'
    . ' <tr class="calcresults">'
    . '    <th>Materials</th>'
    . '   <td align="center"> $  <?php echo round($tenmat1)?></td>'
    . ' <td align="center"> $  <?php echo round($twentymat1)?></td>'
    . ' <td align="center"> $  <?php echo round($matsavings1)?></td>'
    . ' </tr>'
    . ' <tr class="calcresults">'
    . '    <th>Tax</th>'
    . '    <td align="center"><div> $  <?php echo round($tentax1)?></td>'
    . '    <td align="center"> $  <?php echo round($twentytax1)?></td>'
    . '   <td align="center"> $  <?php echo round($taxsavings1)?></td>'
    . ' <tr>'
    . ' <tr class="calcresults">'
    . '    <th>Hours</th>'
    . '    <td align="center"> $  <?php echo round($tenhours1)?></td>'
    . '    <td align="center"> $  <?php echo round($twentyhours1)?></td>'
    . '     <td align="center"> $  <?php echo round($hourssavings1)?></td>'
    . ' </tr>'
    . ' <tr class="calcresults">'
    . '   <th>Total</th>'
    . '   <td align="center"> $  <?php echo round($tentotal1)?></td>'
    . '   <td align="center"> $  <?php echo round($twentytotal1)?></td>'
    . '   <td align="center"> $  <?php echo round($totalsavings1)?></td>'
    . ' </tr>'
    . ' </table>'
    . ' </div>';

$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setSubject('EMT Calculator App Results');
$mailer->setBody($body);

$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ' . $send->__toString();
} else {
    echo 'Mail sent';
}

}

I then have the action in the /components/component/views/view/tmpl/default.php and code it as here:

<form method='post' action='/index.php?option=com_emtapp&view=calculators&task=sendMail'>
<tr class="calcrow"><td>Email your Results:</td><td align="center"><div> <input type="email" name="calculatorEmailAddress" value=""/></div></td></tr>
<input type="hidden" name="tenmat1" value="<?php echo round($tenmat)?>">
<input type="hidden" name="twentymat1" value="<?php echo round($twentymat)?>">
<input type="hidden" name="matsavings1" value="<?php echo round($matsavings)?>">
<input type="hidden" name="tentax1" value="<?php echo round($matsavings)?>">
<input type="hidden" name="twentytax1" value="<?php echo round($twentytax)?>">
<input type="hidden" name="taxsavings1" value="<?php echo round($taxsavings)?>">
<input type="hidden" name="tenhours1" value="<?php echo round($tenhours)?>">
<input type="hidden" name="twentyhours1" value="<?php echo round($twentyhours)?>">
<input type="hidden" name="hourssavings1" value="<?php echo round($hourssavings)?>">
<input type="hidden" name="tentotal1" value="<?php echo round($tentotal)?>">
<input type="hidden" name="twentytotal1" value="<?php echo round($twentytotal)?>">
<input type="hidden" name="totalsavings1" value="<?php echo round($totalsavings)?>">


<input type='submit' value='Email Your Results'/>
</form>

However it will not send any email. Does any one have any suggestion as to what I am doing wrong here?

  • If you add var_dump($recipient);, what gets displayed? – Lodder May 16 '14 at 0:08
  • Nothing is shows, other than the refreshed page. It doesn't give any value. It is as if the function is not activated when submit is pushed. – user3597234 May 16 '14 at 2:56
  • ALso, Thanks cppl! I had the hardest time trying to get that to display as code and just gave up. – user3597234 May 16 '14 at 2:58
3

You should really be using a controller event for the sendMail function. Take a look at the existing contact form stuff:

The form view: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/views/contact/tmpl/default_form.php

The view has a submit which sets the task (contact.submit): https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/views/contact/tmpl/default_form.php

Then the controller gets that task: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controllers/contact.php#L23

Which then calls out to a private _sendMail function: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controllers/contact.php#L140

That function does all the work of generating the email contents and sending it out.

1

$calculatorEmailAddress does not seem to be declared, refer to Mathews's answer for a solution for that if that is the problem.

Also one issue I have run into with the mailer function has to do with SMTP, no form component or the base Joomla API for mail actually does any sort of log for the error.

//all variables in here need to be defined
$body = '<div id="emtresults">'
    . '<table border="0" width="75%" max-width="750px" cellpadding="1" cellspacing="1" class="table">'
    . '<tr class="calcresultslabels">'
    . '  <th> </th>'
    . '    <th>10foot</th>'
    . '    <th>20foot</th>'
    . '   <th>Savings</th>'
    . '   </tr>'
    . ' <tr class="calcresults">'
    . '    <th>Materials</th>'
    . '   <td align="center"> $  <?php echo round($tenmat1)?></td>'
    . ' <td align="center"> $  <?php echo round($twentymat1)?></td>'
    . ' <td align="center"> $  <?php echo round($matsavings1)?></td>'
    . ' </tr>'
    . ' <tr class="calcresults">'
    . '    <th>Tax</th>'
    . '    <td align="center"><div> $  <?php echo round($tentax1)?></td>'
    . '    <td align="center"> $  <?php echo round($twentytax1)?></td>'
    . '   <td align="center"> $  <?php echo round($taxsavings1)?></td>'
    . ' <tr>'
    . ' <tr class="calcresults">'
    . '    <th>Hours</th>'
    . '    <td align="center"> $  <?php echo round($tenhours1)?></td>'
    . '    <td align="center"> $  <?php echo round($twentyhours1)?></td>'
    . '     <td align="center"> $  <?php echo round($hourssavings1)?></td>'
    . ' </tr>'
    . ' <tr class="calcresults">'
    . '   <th>Total</th>'
    . '   <td align="center"> $  <?php echo round($tentotal1)?></td>'
    . '   <td align="center"> $  <?php echo round($twentytotal1)?></td>'
    . '   <td align="center"> $  <?php echo round($totalsavings1)?></td>'
    . ' </tr>'
    . ' </table>'
    . ' </div>';

$config = JFactory::getConfig();
//taken form Mathew Lennings answer
$calculatorsEmailAddress = JFactory::getApplication()->input->get('calculatorsEmailAddress', null, 'string');
try{
    ob_start();
    $mailer = JFactory::getMailer();

    $sender = array(
        $config->get( 'config.mailfrom' ),
        $config->get( 'config.fromname' ) );

    $mailer->setSender($sender);

    $recipient = $calculatorEmailAddress;
    $mailer->addRecipient($recipient);

    $mailer->Encoding = 'base64';
    $mailer->setSubject('EMT Calculator App Results');
    $mailer->isHTML(true);
    $mailer->setBody($body);
    $mailer->SMTPDebug = 1;

    $send = $mailer->Send();
    if ( $send !== true ) {
        echo 'Error sending email: ' . $send->__toString();
    } else {
        echo 'Mail sent';
    }
    echo ob_get_clean();
}catch(phpmailerException $e){
    echo $e->errorMessage();
}catch(Exception $e){
    echo $e->getMessage();
}

This technique can get the SMTP errors in a managable format, with ob_start to can even save it to an error log if need be since phpmailer just echo's it by default.

The $body variable also uses many variables that do not seem to be defined, I am sure that is just handled in another function though, but I would also test each one to ensure it is working correctly.

EDIT: I had a question close to this one, more or the error side though but I want to give credit for leading me to this full solution: SMTP no error but not sending email

0

Are you making sure to get the calculatorsEmailAddress from the application input? I don't see it declared in the function.

$calculatorsEmailAddress = JFactory::getApplication()->input->get('calculatorsEmailAddress', null, 'string'); 

You might want to use the JMailer::isEmail method to validate the address before setting the recipient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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