使用SMTPMail插件以HTML格式发送电子邮件

时间:2015-02-09 02:36:42

标签: php email

我的服务器时不时地向一群人发送例行电子邮件。我希望能够以HTML格式发送它。我知道我需要使用$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";或类似的东西,但是我也使用了一个小插件,我发现了一段时间,它允许我在发送邮件之前连接到SMTP服务器(所以它没有来在收件箱中作为垃圾邮件,什么不是。)

无论如何,有人知道如何编辑插件以使其以HTML格式发送吗?

邮寄脚本:

include('SMTPconfig.php');
include('SMTPClass.php');
$emailbody='I\'d like this to send as html';
$elist = mysql_query("SELECT email FROM `subs` ORDER BY id ASC");
 if(mysql_num_rows($elist) > 0)
  {
  while($elist_result = mysql_fetch_array($elist))
  {
    $to= $elist_result['email']; 
    $from = 'admin@chloecrayola.co.uk';
    $subject = $_POST['sub'];
    $body = $emailbody;
    $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
    $SMTPChat = $SMTPMail->SendMail();
    }
    exit();
   }

配置文件:

$SmtpServer="mail.chloecrayola.co.uk";
$SmtpPort="25";
$SmtpUser="admin@chloecrayola.co.uk";
$SmtpPass="*******";

最后,发送电子邮件的位。我需要在这里进行更改,但我不知道我是如何做到的:

class SMTPClient
{
function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{
$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
    if ($SmtpPort == "") 
    {
    $this->PortSMTP = 25;
        }else{
    $this->PortSMTP = $SmtpPort;
    }
}
function SendMail ()
{

    if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
    {

           fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");  
           $talk["hello"] = fgets ( $SMTPIN, 1024 ); 

           fputs($SMTPIN, "auth login\r\n");
           $talk["res"]=fgets($SMTPIN,1024);
            fputs($SMTPIN, $this->SmtpUser."\r\n");
            $talk["user"]=fgets($SMTPIN,1024);

            fputs($SMTPIN, $this->SmtpPass."\r\n");
            $talk["pass"]=fgets($SMTPIN,256);

           fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");  
           $talk["From"] = fgets ( $SMTPIN, 1024 );  
           fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");  
           $talk["To"] = fgets ($SMTPIN, 1024); 

           fputs($SMTPIN, "DATA\r\n");
            $talk["data"]=fgets( $SMTPIN,1024 );

            fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
            $talk["send"]=fgets($SMTPIN,256);
           //CLOSE CONNECTION AND EXIT ... 
           fputs ($SMTPIN, "QUIT\r\n");  
           fclose($SMTPIN); 
    }  
return $talk;
}        
}

我确信一定有可能做到,但我确实毫无头绪。感谢

1 个答案:

答案 0 :(得分:0)

Content-Type: multipart/alternative;
boundary="===boundaryString=="
MIME-Version: 1.0

尝试在$ headers中使用上述MIME标头。

然后身体需要包括以下内容:

--===boundaryString==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

...
text template
...

--===boundaryString==
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable


...
html template
...

包含格式为txt和HTML的相同信息的txt和html模板始终是一个好习惯。帮助避免垃圾邮件过滤器并改善整体交付。这仍然不能替代正确的反向查找设置,其中包括DNS服务器上的SPF,DKIM,DMARK记录。