Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Pear:SMTP邮件脚本,带有附件、纯文本和基于HTML的_Php_Email_Pear - Fatal编程技术网

PHP Pear:SMTP邮件脚本,带有附件、纯文本和基于HTML的

PHP Pear:SMTP邮件脚本,带有附件、纯文本和基于HTML的,php,email,pear,Php,Email,Pear,我正在创建一个脚本,该脚本将生成一封邮件,这样就有两个选项可用,即“text/plain”或“text/html”。在生成邮件头时,我可以为其中任何一个生成邮件头,但当我尝试为它们创建替代环境时,生成的邮件中出现了一个邮件头错误。实际上,我无法处理头数组 我同时提供了脚本和输出邮件 PHP脚本 if(isset($_POST['cperson'])) { $cperson=$_POST['cperson']; $email=$_POST['email']

我正在创建一个脚本,该脚本将生成一封邮件,这样就有两个选项可用,即“text/plain”或“text/html”。在生成邮件头时,我可以为其中任何一个生成邮件头,但当我尝试为它们创建替代环境时,生成的邮件中出现了一个邮件头错误。实际上,我无法处理头数组

我同时提供了脚本和输出邮件

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");    
 }
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");    
 }
提前感谢:)

基于我使用混合和替代邮件执行的以下邮件

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--
哇,我找到解决办法了

这是输出邮件

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--
内容类型:多部分/混合;
boundary=“bdd34484bf73eb6782690e90985334b2”
--bdd34484bf73eb6782690e90985334b2
内容类型:多部分/备选;
边界=“1e5d6e084aa19ef37bf6f314c2dfe404”
--1e5d6e084aa19ef37bf6f314c2dfe404
内容类型:文本/纯文本;
内容传输编码:7bit
文本示例
--1e5d6e084aa19ef37bf6f314c2dfe404
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
HTML示例
--1e5d6e084aa19ef37bf6f314c2dfe404--
--bdd34484bf73eb6782690e90985334b2
内容类型:图片/png;
name=“logo.png”
内容传输编码:base64
IVBORW0KGGOAAAANSUHEUGAAAPAAOCAYAAAABHAFTAABHNCSVQICAGIFAHKIAAALWSFLZ
AAANGQADYEB6KQAAABL0RVH0U29MDHDHCM--------------------W9QVFSEE SNG6GYJ
--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");

        $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");    
 }
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");    
 }
if(isset($\u POST['cperson']))
{
$cperson=$_POST['cperson'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
需要一次“./php/Mail.php”;
$from=$cperson.“;
$to=“接收方”;
$subject=$subject;
$text=“文本示例”;
$html=“html示例”;
$mime_boundary=md5(mt_rand());/----主边界
$alt_boundary=md5(mt_rand());/----备用边界
/*$headers=数组(
'From'=>$From,
'至'=>$至,
“主题”=>$Subject,
“MIME版本”=>“1.0”,
“内容类型”=>“多部分/混合;边界=\”.$mime\U边界.\“\r\n.”--“$mime\U边界.”\r\n.“.内容类型:多部分/可选;边界=”.$alt\U边界.\r\n”)*/
//编辑标题部分
$headers=数组(
'From'=>$From,
'至'=>$至,
“主题”=>$Subject,
“MIME版本”=>“1.0”,
“内容类型”=>“多部分/混合;\n边界=\”.$mime\U边界。“\”);
/*$body=“--”$alt\U边界。“\n”。
“内容类型:文本/普通;字符集=\”iso-8859-1\“\n”。
“内容传输编码:7bit\r\n”。
$text.“\r\n.”--“$alt\u边界”。\r\n”;
“内容类型:text/html;字符集=\”iso-8859-1\“\n”。
“内容传输编码:7bit\r\n”。
$html。“\r\n”。“-”$alt\u边界。“\r\n”*/
//编辑的初始身体部位
$body=“-->$mime\U边界”。\r\n。
“内容类型:多部分/可选;\n”。
“边界=\”.$alt\u边界。“\”\n\n”。
“-”$alt\u边界。“\n”。
“内容类型:文本/普通;\n”。
“内容传输编码:7bit\r\n”。
$text.“\n\n.”--“$alt\u边界”。\r\n”。
“内容类型:text/html;字符集=\”iso-8859-1\“\n”。
“内容传输编码:7bit\r\n”。
$html。“\n\n”“”--“$alt\U边界。”--\r\n”;
$tmp_name=$_文件['doc1']['tmp_name'];
$type1=$_文件['doc1']['type'];
$file_name1=$_FILES['doc1']['name'];
$size1=$_文件['doc1']['size'];
如果(文件存在($tmp\U名称1))
{
如果(是否上载了文件($tmp\U名称1))
{
$file1=fopen($tmp_name1,'rb');
$data1=fread($file1,filesize($tmp_name1));
fclose(1美元);
$data1=块分割(base64_编码($data1));
}
$body.=“-”$mime\U边界。“\n”。
“内容类型:”.$type1.;\n”。
“名称=\”.$file\u name1.\“\n”。
//“内容处置:附件;\n”。
//“filename=\”{$fileatt\u name}\“\n”。
“内容传输编码:base64\n\n”。
$data1。“\n\n”;
}
$body.=“-”$mime\U边界。“-\n”;
$host=”ssl://smtp.gmail.com";
$port=“465”;
$username=“username”;
$password=“password”;
$smtp=Mail::工厂('smtp',
数组('host'=>$host,
“端口”=>$port,
“auth”=>正确,
“用户名”=>$username,
“密码”=>$password));
$mail=$smtp->send($to、$headers、$body);
if(PEAR::isError($mail))
{
echo(“”$mail->getMessage()”

”; } 其他的 { 标题(“Location:inquiry.php?msg=success”); } } 其他的 { 标题(“位置:inquiry.php?msg=fail”); }
为什么要使用
多部分/混合
?它应该是
multipart/alternative
。主标题中不能有两个
Content-type
标题。您需要将其中一个移动到嵌入内容中。谢谢@Barmar。我添加了创建脚本的基础。你怎么说。因为它能产生完美的结果。@Barmar。先生,我使用了“multipart/mixed”以便它可以支持邮件附件和“multipart/alternative”来创建邮件的替代环境。如果我能在两天前找到这个答案,它将节省我两天的时间。这正是我想要的。