web-dev-qa-db-ja.com

JMailを使用したスクリプトがメールを送信しない

計算を行うために作成したコンポーネントがあります。結果を自分にメールで送信できるようにしたいと思います。次に示すように、tmplページのdefault.phpのコントローラーでメール関数を作成しようとしました。

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';
}

}

次に、/ components/component/views/view/tmpl/default.phpにアクションを記述し、次のようにコーディングします。

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

ただし、メールは送信されません。ここで私が間違っていることについて何か提案はありますか?

2
user3597234

実際には、sendMail関数のコントローラーイベントを使用する必要があります。既存のお問い合わせフォームをご覧ください。

フォームビュー: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/views/contact/tmpl/default_form.php

ビューには、タスクを設定する送信(contact.submit)があります。 https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/views/contact/tmpl/default_form.php

次に、コントローラーはそのタスクを取得します: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controllers/contact.php#L2

次に、プライベート_sendMail関数を呼び出します: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controllers/contact.php#L14

この関数は、電子メールの内容を生成して送信するすべての作業を行います。

3
Chad Windnagle

$calculatorEmailAddressは宣言されていないようです。それが問題である場合の解決策については、Mathewsの回答を参照してください。

また、メーラー機能で発生した1つの問題はSMTPに関係しており、フォームコンポーネントがないか、メール用の基本的なJoomla APIが実際に何らかのエラーのログを記録します。

//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();
}

この手法では、管理可能な形式でSMTPエラーを取得できます。phpmailerはデフォルトでエコーするだけなので、ob_startを使用すると、必要に応じてエラーログに保存することもできます。

$ body変数は、定義されていないように見える多くの変数も使用しますが、それは別の関数で処理されるだけですが、正しく動作することを確認するためにそれぞれをテストします。

編集:私はこれに近い質問をしましたが、私はこの完全な解決策に私を導いたことを認めたいと思います: SMTPエラーなし、メール送信なし

1
Jordan Ramstad

アプリケーション入力からcalculatorsEmailAddressを確実に取得していますか?関数で宣言されていないようです。

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

JMailer::isEmailメソッドを使用して、受信者を設定する前にアドレスを検証します。

0
Mathew Lenning