使用PHPMailer发送邮件时出现问题

时间:2019-04-09 15:37:53

标签: phpmailer

无法通过PhpMailer发送多个HTML邮件。

完成调查后,我试图发送2封电子邮件。

  1. 发送邮件(调查详细信息)给管理员。
  2. 发送邮件(优惠券代码)给客户。

我创建了2个单独的发送邮件的功能

  1. sendSurveyDetails()
  2. emailCoupon()

然后在完成调查之后,调用sendSurvey()函数在内部执行这两个函数。

不幸的是,只有1个功能有效,即sendSurveyDetails();

请在下面检查我的代码。

公共函数sendSurvey(){

//Send survey details to the admin
$this->sendSurveyDetails();

//Send coupon to the customer after completing the survey
$this->emailCoupon();       

}

    public function emailCoupon() {

        require('lib/PHPMailer/PHPMailerAutoload.php');
        $mail = new PHPMailer;

        $mail->setFrom(''.$this->adminWeb.'', 'Your Coupon');
        $mail->addReplyTo(''.$this->atlReservation.'', 'Your Coupon');

        $mail->IsHTML(true);
        $mail_html = implode('',file('guest/email-coupon.php'));

        //Set who the message is to be sent to
        $mail->addAddress(''.$_POST['Email'].'', ''.$_POST['FirstName'].' '.$_POST['LastName'].'');

        //Set the subject line
        $mail->Subject = 'Your Coupon';

        //Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';

                //FirstName
                $mail_html = str_replace("%%firstname%%", $_POST['FirstName'], $mail_html);

                //LastName
                $mail_html = str_replace("%%lastname%%", $_POST['LastName'], $mail_html);

                //Email
                $mail_html = str_replace("%%email%%", $_POST['Email'], $mail_html);

                //Acct
                $mail_html = str_replace("%%acct%%", $_GET['guid'], $mail_html);

                //Coupon
                $mail_html = str_replace("%%coupon%%", $_POST['coupon'], $mail_html);

                //Add Info
                $mail_html = str_replace("%%add_inf%%", $_POST['Additional_Info'], $mail_html);

                //Resort Info
                $mail_html = str_replace("%%resort_inf%%", $_POST['AddInfo_Selections'], $mail_html);

                //Email ID
                $random_hash = rand();
                $mail_html = str_replace("%%random_hash%%", $random_hash, $mail_html);

            $mail->Subject = 'Your Coupon';
            $mail->Body    = $mail_html;

        //send the message, check for errors
        if (!$mail->send()) {

            echo 'SENDING_FAILED';

        } else {

            echo 'MAIL_SENT';

        }

    }


    public function sendSurveyDetails() {

        require('lib/PHPMailer/PHPMailerAutoload.php');
        $mail = new PHPMailer;

        //Set who the message is to be sent to
        $sqlx = $this->getMAXid_summary();
        $getRID = mysql_fetch_object($sqlx);

        $q = $this->surveyDetails($getRID->rid, NULL);
        $sD = mysql_fetch_object($q);

        $mail->setFrom(''.$this->adminWeb.'', ''.$sD->sid.' Survey Details');
        $mail->addReplyTo(''.$this->atlReservation.'', ''.$sD->sid.' Survey Details');

        $mail->IsHTML(true);
        $mail_html = implode('',file('guest/email-survey-details.php'));

        $mail->addAddress(''.$this->QN.'', 'Managers');
        $mail->AddCC(''.$this->adminWeb.'', 'Webmaster');
        $mail->AddCC(''.$this->atlReservation.'', 'Reservations');

        //Set the subject line
        $mail->Subject = ''.$sD->sid.' Survey Details';

        //Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';

                //Guest Details
                $mail_html = str_replace("%%acct%%", $sD->guid, $mail_html);
                $mail_html = str_replace("%%sid%%", $sD->sid, $mail_html);
                $mail_html = str_replace("%%survey_date%%", $sD->survey_date, $mail_html);
                $mail_html = str_replace("%%coupon%%", $sD->coupon, $mail_html);
                $mail_html = str_replace("%%checkin%%", $sD->checkin_date, $mail_html);
                $mail_html = str_replace("%%firstname%%", $sD->firstname, $mail_html);
                $mail_html = str_replace("%%lastname%%", $sD->lastname, $mail_html);
                $mail_html = str_replace("%%nationality%%", $sD->nationality, $mail_html);
                $mail_html = str_replace("%%email%%", $sD->email, $mail_html);
                $mail_html = str_replace("%%subscribe%%", $sD->newsletter_subscribe, $mail_html);
                $mail_html = str_replace("%%newsletter_email%%", $sD->newsletter_email, $mail_html);
                $mail_html = str_replace("%%add_inf%%", $sD->add_resort_info, $mail_html);
                $mail_html = str_replace("%%resort_inf%%", $sD->resort_info, $mail_html);
                $mail_html = str_replace("%%fb%%", $sD->fb, $mail_html);
                $mail_html = str_replace("%%twitter%%", $sD->twitter, $mail_html);

                //Part 1
                $mail_html = str_replace("%%first_trip%%", $sD->first_trip, $mail_html);
                if ($sD->first_trip == 'NO') {
                    $mail_html = str_replace("%%ctr_azores%%", $sD->ctr_azores, $mail_html);
                    $mail_html = str_replace("%%ctr_dgte%%", $sD->ctr_dgte, $mail_html);
                    $mail_html = str_replace("%%ctr_pg%%", $sD->ctr_pg, $mail_html);
                } else {
                    $mail_html = str_replace("%%ctr_azores%%", 0, $mail_html);
                    $mail_html = str_replace("%%ctr_dgte%%", 0, $mail_html);
                    $mail_html = str_replace("%%ctr_pg%%", 0, $mail_html);
                }

                if ($sD->source == '1') {
                    $source = 'Referral by a friend.';
                } else if ($sD->source == '2') {
                    $source = 'Internet/Google Search';
                } else if ($sD->source == '3') {
                    $source = 'Advertisement';
                } else if ($sD->source == '4') {
                    $source = 'Recommendation of a travel agent/dive shop.';
                } else if ($sD->source == '5') {
                    $source = 'Dive Show';
                } else if ($sD->source == '6') {
                    $source = 'Other';
                } else if ($sD->source == '7') {
                    $source = 'Facebook';
                } else if ($sD->source == '8') {
                    $source = 'Twitter';
                }

                $mail_html = str_replace("%%source%%", $source, $mail_html);

                if ($sD->source == '1' || $sD->source == '5' || $sD->source == '6' || $sD->source == '7' || $sD->source == '8') {
                    $mail_html = str_replace("%%source_inf%%", $sD->source_inf, $mail_html);
                }

                if ($sD->m_reserve == '1') {
                    $reserve = 'Through a travel agent/dive shop.';
                } else if ($sD->m_reserve == '2') {
                    $reserve = 'Directly with resort via email';
                } else if ($sD->m_reserve == '3') {
                    $reserve = 'Other';
                } else {

                }

                $mail_html = str_replace("%%reserve%%", $reserve, $mail_html);                  
                $mail_html = str_replace("%%num_people%%", $sD->num_people, $mail_html);
                $mail_html = str_replace("%%age_group%%", $sD->age_group, $mail_html);
                $mail_html = str_replace("%%trans_arrangements%%", $sD->trans_arrangements, $mail_html);
                $mail_html = str_replace("%%web_info%%", $sD->web_info, $mail_html);
                $mail_html = str_replace("%%web_comments%%", $sD->web_comments, $mail_html);


                $mail_html = str_replace("%%trip_stay%%", $sD->trip_stay, $mail_html);
                $mail_html = str_replace("%%trip_return%%", $sD->trip_return, $mail_html);
                $mail_html = str_replace("%%recommend%%", $sD->recommend, $mail_html);
                $mail_html = str_replace("%%stay_rep%%", $sD->stay_rep, $mail_html);
                $mail_html = str_replace("%%met_manila%%", $sD->met_manila, $mail_html);
                $mail_html = str_replace("%%met_resort%%", $sD->met_resort, $mail_html);
                $mail_html = str_replace("%%diver_cert%%", $sD->diver_cert, $mail_html);
                $mail_html = str_replace("%%dive_logged%%", $sD->dive_logged, $mail_html);
                $mail_html = str_replace("%%recognize_staff%%", $sD->recognize_staff, $mail_html);
                $mail_html = str_replace("%%staff_name%%", $sD->staff_name, $mail_html);
                $mail_html = str_replace("%%stay_comments%%", $sD->stay_comments, $mail_html);

                $qr1 = $this->surveyDetails($getRID->rid, 1);
                while ($r1 = mysql_fetch_array($qr1)) {

                    if($r1['rate'] == '6') { 
                        $rate1 = "N/A"; 
                    } else {
                        $rate1 = '<strong>'.$r1['rate'].' &#9733;</strong>';
                    }
                    $revContent1 .= ''.$r1["rate_name"].': '.$rate1.' &nbsp; '.$r1['comments'].' <hr noshade="noshade" size="1" color="#EEEEEE">';
                }

                $mail_html = str_replace("%%revContent1%%", $revContent1, $mail_html); 

                $qr2 = $this->surveyDetails($getRID->rid, 2);
                while ($r2 = mysql_fetch_array($qr2)) {

                    if($r2['rate'] == '6') { 
                        $rate2 = "N/A"; 
                    } else {
                        $rate2 = '<strong>'.$r2['rate'].' &#9733;</strong>';
                    }
                    $revContent2 .= ''.$r2["rate_name"].': '.$rate2.' &nbsp; '.$r2['comments'].' <hr noshade="noshade" size="1" color="#EEEEEE">';
                }

                $mail_html = str_replace("%%revContent2%%", $revContent2, $mail_html); 

                //Email ID
                $random_hash = rand();
                $mail_html = str_replace("%%random_hash%%", $random_hash, $mail_html);

            $mail->Subject = ''.$sD->sid.' Survey Details';
            $mail->Body    = $mail_html;

        //send the message, check for errors
        if (!$mail->send()) {

            //echo 'SENDING_FAILED';

        } else {

            //echo 'MAIL_SENT';

        }

    }

0 个答案:

没有答案