PHPMailer - 在非对象上调用成员函数addReplyTo()

时间:2015-11-15 07:07:58

标签: php phpmailer

我正在使用phpmailer脚本,其中包含了PHPMailerAutoload.php文件的路径。但是,我在错误日志“PHP致命错误:在第41行的/var/www/html/test.php中的非对象上调用成员函数addReplyTo()时收到错误,引用:{{3} }“

<?php

$terms_name = $_POST['terms-name'];
$terms_email = $_POST['terms-email'];
$terms_telephone = $_POST['terms-telephone'];
$intRecordDate  = date('YmdHis');
$stClientIP = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $stClientIP = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}

require('db-connection.php');

/* Send Mailer to customers and client */
require_once('components/phpmailer/PHPMailerAutoload.php');

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if(isset($terms_name) && isset($terms_email) && isset($terms_telephone)) {
        $sql = "INSERT INTO terms_and_conditions_form (name, telephone, email, client_ip, record_date)
        VALUES('$terms_name', '$terms_telephone', '$terms_email', '$stClientIP', '$intRecordDate')";

        // use exec() because no results are returned
        $conn->exec($sql);
        termsMailer();
    }
}
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;


function termsMailer() {
$mail->addReplyTo('.$terms_email.');
$mail->setFrom('test@email.com', 'test');
$mail->addCC('test@email.com');

$mail->addAttachment('pdf/terms-and-conditions.pdf');
$mail->isHTML(true);


$mail->Subject = "Subject comes here..";

$mail->Body = ' 
<table style="WIDTH:100%" border="0" cellspacing="0" cellpadding="0" width="100%">
                                                                                <tbody>
<tr>
                                                                                        <td style="PADDING-BOTTOM:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in;PADDING-TOP:0in" valign="top">
                                                                                        <p class="MsoNormal"><img src="http://my-site.com/logo.png" width="92" height="48" class="CToWUd"></p></td>
                                                                                    </tr>
<TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 18.75pt" vAlign=top>
                                        <span class=MsoNormal>Hello '.$_POST['terms-name'].',</span></TD>
                                    </TR>

                                <TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 5pt" vAlign=top>
                                            <span class=MsoNormal>Thank you for accepting our Terms of Service. Below are the details that you filled in while accepting the \'Terms of Service\':</span>                                       
                                    </TD>
                                </TR>
                                    <!--TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 5pt" vAlign=top>
                                        <span class=MsoNormal>You filled in the following details while accepting the \'Terms of Service\':</span></TD>
                                    </TR-->
                                <TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 5pt" vAlign=top>
                                        <table border="0" cellpadding="1" cellspacing="1" width="50%">
                                            <TBODY>
                                                <tr>
                                                    <td width="20px"><strong>Name</strong></td>
                                                    <td width="1px"><strong>:<strong></td>
                                                    <td>'.$_POST['terms-name'].'</td>
                                                </tr>
                                                <tr>
                                                    <td width="20px"><strong>Email</strong></td>
                                                    <td width="1px"><strong>:<strong></td>
                                                        <td><span style="color: #0d83dd">'.$_POST['terms-email'].'</span></td>
                                                </tr>
                                                <tr>
                                                    <td width="20px"><strong>Contact No.<strong></td>
                                                    <td width="1px"><strong>:<strong></td>
                                                    <td>'.$_POST['terms-telephone'].'</td>
                                                </tr>                                               
                                            </TBODY>
                                        </table>
                                    </TD>
                                </TR>                               
                                <TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 5pt" vAlign=top>
                                        <span class=MsoNormal>Our team will get in touch with you via e-mail shortly.</span></TD>
                                </TR>
                                <TR>
                                        <TD style="PADDING-BOTTOM: 5pt; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; PADDING-TOP: 5pt" vAlign=top>
                                        <span class=MsoNormal>If you are not the one who has accepted the Terms of Service, kindly inform us on <a mailto:test@email.com><span style="color: #104887">test@email.com<span></a> .</span></TD>
                                </TR>
                                </tbody>
                                </table>
                            ';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        header('Location: thank-you.php');
    }

}

1 个答案:

答案 0 :(得分:1)

定义$ mail。你需要这样的东西:

function termsMailer() {
    $mail = new PHPMailer;
    $mail->addReplyTo('.$terms_email.');
    ....