PHPMailer与textarea形式和HTML

时间:2012-02-14 11:40:04

标签: php html forms email phpmailer

我正在尝试将PHP教程中的一些代码放在一起,该教程展示了如何创建一个非常基本的PHPMailer表单,将纯文本电子邮件发送到邮件列表。简单就是我想要的大部分,因为有几个人会使用这个表单,虽然我想包括在表单中使用HTML的功能。因此,例如,如果我想使用<center>标记或插入托管图片<img src="www.link.com/img.jpg">,以使电子邮件更丰富。

目前,我无法弄清楚如何启用html嵌入。如果我将HTML代码放入表单中,它只是输出代码<b><i>text</i></b>,因为我希望电子邮件显示为: text

我原以为它可能是IsHTML(True);变量,但是当我发送电子邮件时,一切似乎都正常。如果我删除<input type并将其更改为<span,则电子邮件信息只是空白。

这是我正在处理的原始代码:http://yorkspace.wordpress.com/simple-php-mailing-list/

这里是sendmail.php,在这里编辑邮件,预览,然后发送:

<?php

require "maillist-settings.inc.php";
if ($_POST['preview'])
{
    $email_array = $_POST['emaillist'];

    if (is_array($email_array))
    {
        $subject = $_POST['emailtitle'];
        $message = $_POST['emailmessage'];
        $bcc_list = implode(", ", $email_array);

        echo "<table><tr><td>";    
            echo "<form action=\"" . $_SERVER['SCRIPT_URL'] . "\" method=\"POST\">";
            foreach ($email_array as $email_address)
            {
                echo "<input type=\"hidden\" name=\"emaillist[]\" value=\"" .  $email_address . "\">";
            }
            echo "<input type=\"hidden\" name=\"emailtitle\" value=\"" . $subject . "\">";
            echo "<input type=\"hidden\" name=\"emailmessage\" value=\"". $message ."\">";
            echo "<input type=\"submit\" name=\"sendemail\" value=\"Send Email\"></form>";

        echo "</td></tr></table>";

}
else if ($_POST['sendemail'])
{
    LoadLib_PHPMailer();
    $mail = new MLMail;

    $email_array = $_POST['emaillist'];

    if ( is_array($email_array) == TRUE )
    {
            $mail->AddAddress($mail->From);
            $mail->Subject = stripslashes($_POST['emailtitle']);
            $mail->Body = stripslashes($_POST['emailmessage']);

            if(!$mail->Send())
                echo "There has been an error sending email:<br><br><b>" . $mail->ErrorInfo . "</b>";
            else
                echo "Message has been sent successfully.<br><br><a href=\"" . $_SERVER['SCRIPT_URL'] . "\">Send another message</a>";
            $mail->ClearAddresses();
    }
}
else
{
    $sEditSubject = $_POST['emailtitle'];
    if (isset($sEditSubject))
        $subject = $sEditSubject;
    else
        $subject = "";

    $sEditMessage = $_POST['emailmessage'];
    if (isset($sEditMessage))
        $message = $sEditMessage;
    else
    {
    $message = "\n\n\n<br>FOOTER</BR>\n\n";
    }
    echo "Subject:";
    echo "<br><input type=\"text\" name=\"emailtitle\" size=\"80\" value=\"" . htmlspecialchars(stripslashes($subject)) . "\"></input>";
    echo "<br>Message:";
    echo "<br><textarea name=\"emailmessage\" rows=\"20\" cols=\"72\">" . htmlspecialchars(stripslashes($message)) . "</textarea>";
    echo "<br><input type=\"submit\" name=\"preview\" value=\"Preview\"></form>";
}

?>

这是从sendmail.php调用的maillist-settings.inc.php

<?php
$listname = "Mailing List";
$listfile = "list.csv";
$sPHPMAILER_PATH = "phpmailer";
$sProtocol = "http://";
$sReferrer = "index.php";
$bEmailSend = true;
$sSendType = "smtp";
$sFromEmailAddress = "email@email.com";
$sFromName = "my name";
$sSMTPHost = "mail.smtp.com";
$sSMTPAuth = true;
$sSMTPUser = "email@email.com";
$sSMTPPass = "password";
$iSMTPPort = 25;
$ishtml = true;

require $sPHPMAILERpath . "class.phpmailer.php";
require $sPHPMAILERpath . "class.smtp.php";

// Define parameters as class MLMail
class MLMail extends PHPMailer {
    // Set default variables for all new objects
    var $From;
    var $FromName;
    var $Mailer;
    var $IsHtml;
    var $WordWrap;
    var $Host;
    var $SMTPAuth;
    var $Username;
    var $Password;
    var $Port;

    function MLMail() {
        parent::SetLanguage('en','phpmailer/language/');
        $this->From = $GLOBALS['sFromEmailAddress'];
        $this->FromName = $GLOBALS['sFromName'];
        $this->Mailer = $GLOBALS['sSendType'];
        $this->IsHtml = $GLOBALS['ishtml'];
        $this->WordWrap = $GLOBALS['sWordWrap'];
        if ($this->Mailer == "smtp")
        {
            $this->Port = $GLOBALS['iSMTPPort'];
            $this->Host = $GLOBALS['sSMTPHost'];
            $this->SMTPAuth = $GLOBALS['sSMTPAuth'];
            if ($this->SMTPAuth) {
                $this->Username = $GLOBALS['sSMTPUser'];
                $this->Password = $GLOBALS['sSMTPPass'];
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用的脚本是Phpmailer的扩展。当你提到isHTML变量时你是对的,但是这个变量需要使用Phpmailer类的内置方法来设置。

在sendmail.php文件中添加以下(第三行):

  $mail->AddAddress($mail->From);
  $mail->Subject = stripslashes($_POST['emailtitle']);

  $mail->IsHTML(true);

  $mail->Body = stripslashes($_POST['emailmessage']);

您可能还想提供非HTML电子邮件(因为某些电子邮件客户端不接受HTML电子邮件),在这种情况下,您可以在$ mail-&gt; Body line之后添加类似的内容:< / p>

  $mail->AltBody= striptags(stripslashes($_POST['emailmessage']));

这也将迎合非HTML版本的发送 - 作为后备。