如何使用php会话变量发送邮件

时间:2014-07-12 16:50:25

标签: php

我想用以下会话变量发送邮件,但我得到没有变量的html内容。有什么建议吗?

............................................... ............................................

PHP代码:

$servicenumber = $_SESSION['number'];
$amount = $_SESSION['amount'];



require_once('libraries/PHPMailer.php');

$mail = new PHPMailer;

$mail->IsSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';                 // Specify main and backup server
$mail->Port = 587;                                    // Set the SMTP port
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                // SMTP username
$mail->Password = 'key';                  // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also    accepted

$mail->From = 'someone@gmail.com';
$mail->FromName = 'test';
$mail->AddAddress('some@gmail.com', 'Pranjal');  // Add a recipient
$mail->AddAddress('ellen@example.com');               // Name is optional

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

$mail->Subject = 'Here is the subject';
$mail->Body    = '
                  <table class="six columns" style="border-spacing: 0;border-collapse:    collapse;padding: 0;vertical-align: top;text-align: left;margin: 0 auto;width: 280px;">
                    <tr style="padding: 0;vertical-align: top;text-align: left;">
                      <td class="panel" style="word-break: break-word;-webkit-hyphens:     auto;-moz-hyphens: auto;hyphens: auto;padding: 10px !important;vertical-align: top;text-align: left;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;margin: 0;line-height: 19px;font-size: 14px;background: #f2f2f2;border: 1px solid #d9d9d9;border-collapse: collapse !important;">
                        <h6 style="color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;padding: 0;margin: 0;text-align: left;line-height: 1.3;word-break: normal;font-size: 20px;">Transaction receipt</h6>
                        <p style="margin: 0;margin-bottom: 10px;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;padding: 0;text-align: left;line-height: 19px;font-size: 14px;">Details</p>

                        <table style="border-spacing: 0;border-collapse: collapse;padding: 0;vertical-align: top;text-align: left;">
                          <tr style="padding: 0;vertical-align: top;text-align: left;">
                            <td style="word-break: break-word;-webkit-hyphens: auto;-moz-hyphens: auto;hyphens: auto;padding: 0px 0px 10px;vertical-align: top;text-align: left;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;margin: 0;line-height: 19px;font-size: 14px;border-collapse: collapse !important;">
                              Service Number: <a style="text-decoration: none;">&nbsp;&nbsp;&nbsp;&nbsp;</a>  <? echo $service;?>
                            </td>
                          </tr>
                        </table>
                        <hr style="color: #d9d9d9;background-color: #d9d9d9;height: 1px;border: none;">
                        <table style="border-spacing: 0;border-collapse: collapse;padding: 0;vertical-align: top;text-align: left;">
                          <tr style="padding: 0;vertical-align: top;text-align: left;">
                            <td style="word-break: break-word;-webkit-hyphens: auto;-moz-hyphens: auto;hyphens: auto;padding: 0px 0px 10px;vertical-align: top;text-align: left;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;margin: 0;line-height: 19px;font-size: 14px;border-collapse: collapse !important;">
                              Amount Paid:<a style="text-decoration: none;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a> <? echo $amount;?>
                            </td>
                          </tr>

                        </table>
                        <hr style="color: #d9d9d9;background-color: #d9d9d9;height: 1px;border: none;">

                                                  </td>
                      <td class="expander" style="word-break: break-word;-webkit-hyphens: auto;-moz-hyphens: auto;hyphens: auto;padding: 0 !important;vertical-align: top;text-align: left;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;margin: 0;line-height: 19px;font-size: 14px;visibility: hidden;width: 0px;border-collapse: collapse !important;"></td>
                    </tr>
                  </table>

                  <br>

                      </td>
                      <td class="expander" style="word-break: break-word;-webkit-hyphens: auto;-moz-hyphens: auto;hyphens: auto;padding: 0 !important;vertical-align: top;text-align: left;color: #222222;font-family: &quot;Helvetica&quot;, &quot;Arial&quot;, sans-serif;font-weight: normal;margin: 0;line-height: 19px;font-size: 14px;visibility: hidden;width: 0px;border-collapse: collapse !important;"></td>
                    </tr>
                  </table>

                </td>
              </tr>
            </table>


';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}

1 个答案:

答案 0 :(得分:0)

请记住在输出之前运行session_start()。 并且您的邮件内容字符串有错误。您需要使用<? echo $amount;?>将变量包含在字符串中。请改用'.$amount.'