如何在php邮件程序中放置if语句

时间:2018-04-21 03:20:41

标签: php email if-statement phpmailer

我试图在php邮件程序的电子邮件中添加if / else语句。我这样做的原因是我不必根据变量$mail->Body创建多个$rsvp_answer togs。

有没有人看到我做错了什么以及如何让它正常工作?

$mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = '
            <head>
            <style>
            #nameSec {
                margin: 0 auto 50px 0;
                text-align: center;
            }
            .fancyFontTitle {
                font-size: 5rem;
                line-height: 1.5em;
                color: #333;
                font-family: cursive;
            }
            #weddingDate, .title {
                font-family: sans-serif;
                font-size: 2.5rem;
            }
            .title {
                margin: 0px auto 50px auto;
                text-align: center;
                color: #2E393F;
            }
            </style>
            </head>
            <body>
                <div id="header-background" style="background:#C8DBD9;width:100%;max-width:100%;height:auto;">
                    <div id="email-header" style="width:600px;height:auto;margin:auto;display:block;padding:20px 0;">
                        <div id="nameSec">
                            <span class="fancyFontTitle"></span><br>
                        </div>
                    </div>
                    <div id="email-link" style="width:100%;padding:15px;height:auto;background:#EBEBEB;position:relative;">
                        <div id="email-link-container" style="width:600px;height:auto;margin:auto;text-align:left;position:absolute;top:50%;left:50%;-webkit-transform:translate(-50%, -50%);transform: translate(-50%, -50%);width:600px; font-family:Helvetica Neue,Helvetica,Arial,sans-serif;font-size:1.1em;">\
            '
            if ($rsvp_answer == 'Yes') { '
                            <p>' . $rsvp_name . '</p>
                            <p>' . $rsvp_email . '</p>
                            <p>Will the guest be attending: ' . $rsvp_answer . '</p>
                            <p>Will they bring a guest: ' . $rsvp_guest_answer . '</p>
                            <p>Guest 1: ' . $guest1 . '</p>
                            <p>Guest 2: ' . $guest2 . '</p>
                            <p>Guest 3: ' . $guest3 . '</p>
            ' else {
                '           <p>Sorry you cant attend. </p>
            }               
                        </div>
                    </div>
                </div>
            </body>
        ';
        $mail->AltBody = 'Proposal Request Sent';
        if(!$mail->send()) {
            echo 'Message1 could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }

2 个答案:

答案 0 :(得分:1)

你需要根据if \ else来连接正文sting:

$mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = '
            <head>
            <style>
            #nameSec {
                margin: 0 auto 50px 0;
                text-align: center;
            }
            .fancyFontTitle {
                font-size: 5rem;
                line-height: 1.5em;
                color: #333;
                font-family: cursive;
            }
            #weddingDate, .title {
                font-family: sans-serif;
                font-size: 2.5rem;
            }
            .title {
                margin: 0px auto 50px auto;
                text-align: center;
                color: #2E393F;
            }
            </style>
            </head>
            <body>
                <div id="header-background" style="background:#C8DBD9;width:100%;max-width:100%;height:auto;">
                    <div id="email-header" style="width:600px;height:auto;margin:auto;display:block;padding:20px 0;">
                        <div id="nameSec">
                            <span class="fancyFontTitle"></span><br>
                        </div>
                    </div>
                    <div id="email-link" style="width:100%;padding:15px;height:auto;background:#EBEBEB;position:relative;">
                        <div id="email-link-container" style="width:600px;height:auto;margin:auto;text-align:left;position:absolute;top:50%;left:50%;-webkit-transform:translate(-50%, -50%);transform: translate(-50%, -50%);width:600px; font-family:Helvetica Neue,Helvetica,Arial,sans-serif;font-size:1.1em;">';

            if ($rsvp_answer == 'Yes') { $mail->Body.='
                            <p>' . $rsvp_name . '</p>
                            <p>' . $rsvp_email . '</p>
                            <p>Will the guest be attending: ' . $rsvp_answer . '</p>
                            <p>Will they bring a guest: ' . $rsvp_guest_answer . '</p>
                            <p>Guest 1: ' . $guest1 . '</p>
                            <p>Guest 2: ' . $guest2 . '</p>
                            <p>Guest 3: ' . $guest3 . '</p>';
             }else {
                $mail->Body.='<p>Sorry you cant attend. </p>';
            }               
                $mail->Body.='         </div>
                    </div>
                </div>
            </body>
        ';
        $mail->AltBody = 'Proposal Request Sent';
        if(!$mail->send()) {
            echo 'Message1 could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }

答案 1 :(得分:1)

如果要创建具有条件的邮件正文,则首先创建整个字符串,然后将其分配给$ mail-&gt; Body。 示例

$str='Some String'; 
if(conditionTrue){
$str.='Concatinate onr part';
}else{
$str.='Concatinate another part';
}
//continue this way
$mail->Body=$str;