PHP Pear:带有附件和PLAIN-TEXT以及基于HTML的SMTP邮件脚本

时间:2014-07-09 10:39:33

标签: php email pear

我正在创建脚本,脚本将生成一个邮件,以便有两个选项以" text / plain"的形式提供。或" text / html"。在生成标题时,我能够为其中任何一个生成标题,但是当我尝试为它们创建备用环境时,生成的邮件中的标题出现错误。实际上我无法处理HEADER ARRAY。

我提供了两个:脚本和输出邮件。

PHP脚本

if(isset($_POST['cperson']))
    {

        $cperson=$_POST['cperson'];
        $email=$_POST['email'];
        $subject=$_POST['subject'];
        $message=$_POST['message'];

        require_once "../php/Mail.php";

        $from = $cperson."<".$email.">";
        $to = "Receiver<receiver@example.com>";
        $subject = $subject;

        $text="Text example";
        $html = "<html><head></head><body>HTML <b>Example</b></body></html>";

        $mime_boundary=md5(mt_rand()); //------ Main Boundary
        $alt_boundary=md5(mt_rand()); //------ Alternate Boundary

        $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed; boundary=\"".$mime_boundary."\"\r\n"."--".$mime_boundary."\r\n"."Content-Type: multipart/alternative; boundary=".$alt_boundary."\r\n");

        $body = "--".$alt_boundary."\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\r\n"."--".$alt_boundary."\r\n";
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\r\n"."--".$alt_boundary."\r\n";

        $tmp_name1 = $_FILES['doc1']['tmp_name'];
        $type1 = $_FILES['doc1']['type'];
        $file_name1 = $_FILES['doc1']['name'];
        $size1 = $_FILES['doc1']['size'];

        if (file_exists($tmp_name1))
        {

            if(is_uploaded_file($tmp_name1))
            {
                $file1 = fopen($tmp_name1,'rb');
                $data1 = fread($file1,filesize($tmp_name1));
                fclose($file1);
                $data1 = chunk_split(base64_encode($data1));
            }

            $body .= "--".$mime_boundary."\n" .
                "Content-Type: ".$type1.";\n" .
                " name=\"".$file_name1."\"\n" .
                //"Content-Disposition: attachment;\n" .
                //" filename=\"{$fileatt_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data1 . "\n\n";

        }

        $body .= "--".$mime_boundary."\n";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "username";
    $password = "password";

    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail))
    {
        echo("<p> ". $mail->getMessage() ." </p>");
    }
    else
    {
        header("Location:inquiry.php?msg=success");
    }
 }
 else
 {
     header("Location:inquiry.php?msg=fail");    
 }

输出邮件以下输出生成为邮件

Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="525d350e35975cc6e00f4374ba753aa4"


--432463601994e834b268ba151f6b0852
Content-Type: text/plain; 
Content-Transfer-Encoding: 7bit
Text example

--432463601994e834b268ba151f6b0852

--525d350e35975cc6e00f4374ba753aa4
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

--525d350e35975cc6e00f4374ba753aa4

提前致谢:)

根据以下邮件,我使用混合和替代

进行
Content-Type: multipart/mixed; 
 boundary="----=_Part_997431_715227151.1404894582403"

------=_Part_997431_715227151.1404894582403
Content-Type: multipart/alternative; 
 boundary="----=_Part_997430_1861058758.1404894582403"

------=_Part_997430_1861058758.1404894582403
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Text Message

------=_Part_997430_1861058758.1404894582403
Content-Type: text/html
Content-Transfer-Encoding: 7bit

HTML Message

------=_Part_997430_1861058758.1404894582403--

------=_Part_997431_715227151.1404894582403
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

------=_Part_997431_715227151.1404894582403--

1 个答案:

答案 0 :(得分:1)

哇,我得到了解决方案

这是输出邮件

Content-Type: multipart/mixed;
 boundary="bdd34484bf73eb6782690e90985334b2"

--bdd34484bf73eb6782690e90985334b2
Content-Type: multipart/alternative;
 boundary="1e5d6e084aa19ef37bf6f314c2dfe404"

--1e5d6e084aa19ef37bf6f314c2dfe404
Content-Type: text/plain; 
Content-Transfer-Encoding: 7bit

Text example

--1e5d6e084aa19ef37bf6f314c2dfe404
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html><head></head><body>HTML <b>Example</b></body></html>

--1e5d6e084aa19ef37bf6f314c2dfe404--
--bdd34484bf73eb6782690e90985334b2
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

--bdd34484bf73eb6782690e90985334b2--

更改和工作脚本如下 工作脚本

if(isset($_POST['cperson']))
    {

        $cperson=$_POST['cperson'];
        $email=$_POST['email'];
        $subject=$_POST['subject'];
        $message=$_POST['message'];

        require_once "../php/Mail.php";

        $from = $cperson."<".$email.">";
        $to = "Receiver<receiver@example.com>";
        $subject = $subject;

        $text="Text example";
        $html = "<html><head></head><body>HTML <b>Example</b></body></html>";

        $mime_boundary=md5(mt_rand()); //------ Main Boundary
        $alt_boundary=md5(mt_rand()); //------ Alternate Boundary

        /* $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed; boundary=\"".$mime_boundary."\"\r\n"."--".$mime_boundary."\r\n"."Content-Type: multipart/alternative; boundary=".$alt_boundary."\r\n"); */

        // Edited Header Part
        $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed;\n boundary=\"".$mime_boundary."\"");

        /* $body = "--".$alt_boundary."\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\r\n"."--".$alt_boundary."\r\n";
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\r\n"."--".$alt_boundary."\r\n"; */

        // Edited Initial Body Part
        $body = "--".$mime_boundary."\r\n" .
            "Content-Type: multipart/alternative;\n".
            " boundary=\"".$alt_boundary."\"\n\n".
            "--".$alt_boundary."\n" .
            "Content-Type: text/plain; \n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\n\n"."--".$alt_boundary."\r\n" .
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\n\n"."--".$alt_boundary."--\r\n";

        $tmp_name1 = $_FILES['doc1']['tmp_name'];
        $type1 = $_FILES['doc1']['type'];
        $file_name1 = $_FILES['doc1']['name'];
        $size1 = $_FILES['doc1']['size'];

        if (file_exists($tmp_name1))
        {

            if(is_uploaded_file($tmp_name1))
            {
                $file1 = fopen($tmp_name1,'rb');
                $data1 = fread($file1,filesize($tmp_name1));
                fclose($file1);
                $data1 = chunk_split(base64_encode($data1));
            }

            $body .= "--".$mime_boundary."\n" .
                "Content-Type: ".$type1.";\n" .
                " name=\"".$file_name1."\"\n" .
                //"Content-Disposition: attachment;\n" .
                //" filename=\"{$fileatt_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data1 . "\n\n";

        }

        $body .= "--".$mime_boundary."--\n";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "username";
    $password = "password";

    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail))
    {
        echo("<p> ". $mail->getMessage() ." </p>");
    }
    else
    {
        header("Location:inquiry.php?msg=success");
    }
 }
 else
 {
     header("Location:inquiry.php?msg=fail");    
 }