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 尝试使用mail()从html表单自动发送内联和/或附件,但继续获取二进制字符串_Php_Email - Fatal编程技术网

Php 尝试使用mail()从html表单自动发送内联和/或附件,但继续获取二进制字符串

Php 尝试使用mail()从html表单自动发送内联和/或附件,但继续获取二进制字符串,php,email,Php,Email,我正在尝试为我正在设计的俱乐部制作一个电子邮件表单,我希望它检查我是否在html电子邮件中包含附件或内联图像。到目前为止,一切都正常,除了当我尝试包含一个附件时,它会在90秒后超时,并发送一些电子邮件,并且文件的开头填充了“----内容类型:application/pdf;name=“BrainMonkey-ooPic-RS.pdf”内容传输编码:base64内容处置:attachment;filename=“test-RS.pdf”“然后是代表文件的二进制字符串,最后是html电子邮件,发送的

我正在尝试为我正在设计的俱乐部制作一个电子邮件表单,我希望它检查我是否在html电子邮件中包含附件或内联图像。到目前为止,一切都正常,除了当我尝试包含一个附件时,它会在90秒后超时,并发送一些电子邮件,并且文件的开头填充了“----内容类型:application/pdf;name=“BrainMonkey-ooPic-RS.pdf”内容传输编码:base64内容处置:attachment;filename=“test-RS.pdf”“然后是代表文件的二进制字符串,最后是html电子邮件,发送的邮件当然少了附件。代码如下:

<?php 
//------------------------ Check if Subject is filled out ------------------------------>
if (isset($_POST['subject']))
{

//-------------- Upload File from User Computer for attachment ------------------------->
$target_path = "attachments/";
$target_path = $target_path . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_path)) {
    $file_location = $target_path;
    $fileatt = $file_location;
    $fileattname =basename($file_location);

    // read file into $data var
    $file = fopen($fileatt, "rb");
    $data = fread($file,  filesize( $fileatt ) );
    fclose($file);

    // split the file into chunks for attaching
    $content = chunk_split(base64_encode($data));
    $isattached = "yes";
    echo "Attachment attached ".$fileattname." uploaded and is good!";
} else {
    $isattached = "no";
    echo "No Attachment sent ".$fileattname;
}

//------------ Upload File from User Computer for inline image ------------------------>
$target_path2 = "attachments/";

$target_path2 = $target_path2 . basename( $_FILES['file2']['name']);

if(move_uploaded_file($_FILES["file2"]["tmp_name"], $target_path2)) {
    $file_location2 = $target_path2;
    $fileatt2 = $file_location2;
    $fileattname2 =basename($file_location2);
    $inline = '<tr><td><center><img src="http://www.mywebsite.com/JWEC/JWECMail/'. $target_path2 .'"></center><br></td></tr>';
    echo "Inline Attached ".$inline;
} else {
    $inline = ' ';
    echo "<br>No Inline Attached ".$inline."<br>";
}       

//------------------- Set up From, Replyto, and $message ------------------------------>
                $name = "ME";
                $from = "myemail@mywebsite.com";
                $replyto= $from;
                $message = $_REQUEST['message']; 

//------------------------ Setup Header -------------------------------------------->
                // handles mime type for better receiving
                if ($isattached == "yes") {
                    $ext = strrchr( $fileatt , '.');
                    echo $ext;
                    $ftype = "";
                    if ($ext == ".doc") $ftype = "application/msword";
                    if ($ext == ".jpg") $ftype = "image/jpeg";
                        if ($ext == ".gif") $ftype = "image/gif";
                    if ($ext == ".png") $ftype = "image/png";
                    if ($ext == ".zip") $ftype = "application/zip";
                        if ($ext == ".pdf") $ftype = "application/pdf";
                    if ($ftype=="") $ftype = "application/octet-stream";
                }
                //$uid = md5(uniqid(time()));


//---------------------------- Connect to DB ------------------------------------------>
        $con = mysql_connect("mywebsitemysql.com","username","password");   
        if (!$con)  
        {   
            die('Could not connect: ' . mysql_error()); 
        }   
        mysql_select_db("database"); 

//----------------------- Select First Names and Emails --------------------------->
        $result = mysql_query("SELECT fname, email FROM  table");   
        if(mysql_num_rows($result) <= 0)    
        {
            echo "<div class=\"box red\"><center>Email Submissions Failed, No members to send to.</center></div>"; 
        }
        else    
        {
            $count = 0;     
            while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) 
            {  
//---------------------- Set To & Subject -------------------------------------------->
                $to = $row['email']; 
                $subjectf = $_POST["subject"];
                $subject = "Here is a " . $subjectf . " for you ";
                $person = $row['fname'];
            $footer = "<br><br> Best Regards,<br> My Website Club  br><br> If you feel we have sent this email to you in error, or if you <br> would like to be removed from future JW Elite Club emails, you may unsubscribe here:<br> http://www.mywebsite.com/JWEC/unsubscribe.php?email=".$to."<br>";
//------------------------- Message --------------------------------------------------->
                $messagehtml = "  
                  <html> 
                     <head></head> 
                      <body>  
                   <table border='0' cellpadding='0' cellspacing='0' align='center' width='550'> 
            <tr><td><img src='http://www.mywebsite.com/JWEC/JWECMail/include/header.jpg' width='550' alt='My Website Club'><br><br></td></tr> 
            <tr><td>Dear " . $person . ",<br><br></td></tr>
        <tr><td>" . $message . "<br><br></td></tr>
            ".$inline."
        <tr><td>" . $footer . "<br></td></tr>
        <tr><td><img src='http://www.mywebsite.com/JWEC/JWECMail/include/footer.jpg' width='550' alt='My Company and My Website - Keep Walking'><br><br></td></tr>
            </table> 
            </body> 
            </html> 
            ";

//------------------ Set Headers ------------------------------------------------->
            // build the headers for attachment and html
                "--{$mime_boundary}--\n";
                $h = "From: $name<$from>" . "\r\n";
                $h .= "Reply-To: $from"."\r\n";
                $h .= "Return-Path: $from" . "\n";
                $h .= "Date: ".date("r")."\n";
                $h .= "MIME-Version: 1.0\n";
                $h .= "Content-type:text/html; charset=iso-8859-1 \n";
                $h .= "--{$mime_boundary}--";

                if ($isattached == "yes") {     
                $h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
                $h .= "Content-Transfer-Encoding: base64\r\n";
                $h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
                $h .= $content."\r\n";
                $h .= "--{$mime_boundary}--";
                }

                // send mail
                if (mail($to, $subject, $messagehtml, $h))
                        $count++;
                    echo "Fileattname = ".$fileattname." | Inline = ".$inline."<br><br>";
                    }   
        echo "<div class=\"box green\"><center>$count Emails Sent.</center></div>";
    }
} else { ?>
        <form name='Sendhtml' action='' method='post' enctype='multipart/form-data'><br />
            Subject: <input id='subject' name='subject' /><br/><br/>
            Message: <br /><br /><textarea name="message" cols="40" rows="15"></textarea><br /><br />
            Include Image?<br /><br/><input type='file' name='file2' id='file2' /><br/><br/>
            Include Attachment?<br /><br /><input type='file' name='file' id='file' /><br/><br/>
            <input type='submit' name='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='reset' value='Reset' />
        </form>
<?php } ?>

我很确定问题出在页眉($h),但我不确定是什么。请帮忙

提前谢谢大家,


罗伯特

我会坚持我的老建议。。。PHPMailer

网页链接:

下拉杆:

示例代码

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

你的一些邮件头格式也不正确,我建议你使用PHPMailer,如果你想要一个简单的发送邮件功能,你可以使用下面这个。。。(不包括验证)

我让它工作了,标题完全错了,当然,谢谢大家的帮助。如果有人需要它,这里有一个新的header和mail()部分可以使用

    //Setup Headers
    if ($isattached == "yes") {
        $ext = strrchr( $fileatt , '.');
        $ftype = "";
        if ($ext == ".doc") $ftype = "application/msword";
        if ($ext == ".jpg") $ftype = "image/jpeg";
        if ($ext == ".gif") $ftype = "image/gif";
        if ($ext == ".png") $ftype = "image/png";
        if ($ext == ".zip") $ftype = "application/zip";
        if ($ext == ".pdf") $ftype = "application/pdf";
        if ($ftype=="") $ftype = "application/octet-stream";

        // read file into $data var
        $file = fopen($fileatt, "rb");
        $data = fread($file,  filesize( $fileatt ) );
        fclose($file);

        // split the file into chunks for attaching
          $content = chunk_split(base64_encode($data));
     }
     $rand = md5( time() );  
     $mime_boundary = '==Multipart_Boundary_' . $rand;
     $h = "From: $name<$from>" . "\n";
     $h .="Reply-To: $from"."\n";
     $h .= "Return-Path: $from" . "\n"
     ."MIME-Version: 1.0\n" . 
     "Content-Type: multipart/mixed; boundary=\"" . $mime_boundary . "\""
     ."\n\n";
     $message = "This is a multi-part message in MIME format.\n\n" . 
     "--" . $mime_boundary . "\n" . 
     "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" .
     $body . "\n\n";

     if ($content) {
        $message .= "--" . $mime_boundary . "\n" .
        "Content-Type: \"" . $ftype . "\";\n" .
        " name=\"" . basename($fileatt) . "\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"" . basename($fileatt) . "\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $content . "\n\n" .
        "--" . $mime_boundary . "--\n";
    }

    // send mail
    $sent = mail($to, $subject, $message, $h);
    $count++;  
    }    
    if ($sent) {
        echo "<div class=\"box green\"><center>$count Emails Sent.</center></div>"; 
    } else {
        echo "<div class=\"box red\"><center>$count Emails Sent.</center></div>"; 
    }
}
//安装头
如果($isattached==“是”){
$ext=strrchr($fileatt,“.”);
$ftype=“”;
如果($ext==“.doc”)$ftype=“应用程序/msword”;
如果($ext==“.jpg”)$ftype=“image/jpeg”;
如果($ext==“.gif”)$ftype=“image/gif”;
如果($ext==“.png”)$ftype=“image/png”;
如果($ext==“.zip”)$ftype=“application/zip”;
如果($ext==“.pdf”)$ftype=“application/pdf”;
如果($ftype==”)$ftype=“应用程序/八位字节流”;
//将文件读入$data var
$file=fopen($fileatt,“rb”);
$data=fread($file,filesize($fileatt));
fclose($文件);
//将文件拆分为块以进行附加
$content=chunk_split(base64_encode($data));
}
$rand=md5(time());
$mime_boundary='==多部分_boundary'$兰德;
$h=“From:$name”。“\n”;
$h.=“回复:$from”“\n”;
$h.=“返回路径:$from”。“\n”
“MIME版本:1.0\n”。
“内容类型:多部分/混合;边界=\”.$mime\U边界。”\“”
.“\n\n”;
$message=“这是MIME格式的多部分消息。\n\n”。
"--" . $mime\u边界。“\n”。
“内容类型:text/html;字符集=\”iso-8859-1\“\n”。
“内容传输编码:7bit\n\n”。
$body。“\n\n”;
如果($content){
$message.=“-”$mime\u边界。“\n”。
“内容类型:\”.$ftype.\”;\n”。
“name=\”.basename($fileatt)。“\”\n”。
“内容处置:附件;\n”。
“文件名=\”.basename($fileatt)。“\”\n”。
“内容传输编码:base64\n\n”。
$content。“\n\n”。
“-”$mime\U边界。”-\n”;
}
//寄信
$sent=mail($to,$subject,$message,$h);
$count++;
}    
如果($已发送){
回显“$count已发送电子邮件。”;
}否则{
回显“$count已发送电子邮件。”;
}
}
再次感谢你的帮助


<>罗伯特

也许你应该考虑使用PHPMALILL或SWIFTmail。你会避免严重的头痛(我也会这么做,提供这个答案,对不起!)我知道这会更容易,但我宁愿自己去做,这样我就能确切地知道发生了什么。谢谢你的建议,不过我会调查一下。不起作用,只收到了只有一个主题的黑色电子邮件,或者当我尝试使用附件时附件已损坏。@RonertH。。请使用更新的代码。它工作得非常好。。。。试了20次
    //Setup Headers
    if ($isattached == "yes") {
        $ext = strrchr( $fileatt , '.');
        $ftype = "";
        if ($ext == ".doc") $ftype = "application/msword";
        if ($ext == ".jpg") $ftype = "image/jpeg";
        if ($ext == ".gif") $ftype = "image/gif";
        if ($ext == ".png") $ftype = "image/png";
        if ($ext == ".zip") $ftype = "application/zip";
        if ($ext == ".pdf") $ftype = "application/pdf";
        if ($ftype=="") $ftype = "application/octet-stream";

        // read file into $data var
        $file = fopen($fileatt, "rb");
        $data = fread($file,  filesize( $fileatt ) );
        fclose($file);

        // split the file into chunks for attaching
          $content = chunk_split(base64_encode($data));
     }
     $rand = md5( time() );  
     $mime_boundary = '==Multipart_Boundary_' . $rand;
     $h = "From: $name<$from>" . "\n";
     $h .="Reply-To: $from"."\n";
     $h .= "Return-Path: $from" . "\n"
     ."MIME-Version: 1.0\n" . 
     "Content-Type: multipart/mixed; boundary=\"" . $mime_boundary . "\""
     ."\n\n";
     $message = "This is a multi-part message in MIME format.\n\n" . 
     "--" . $mime_boundary . "\n" . 
     "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" .
     $body . "\n\n";

     if ($content) {
        $message .= "--" . $mime_boundary . "\n" .
        "Content-Type: \"" . $ftype . "\";\n" .
        " name=\"" . basename($fileatt) . "\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"" . basename($fileatt) . "\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $content . "\n\n" .
        "--" . $mime_boundary . "--\n";
    }

    // send mail
    $sent = mail($to, $subject, $message, $h);
    $count++;  
    }    
    if ($sent) {
        echo "<div class=\"box green\"><center>$count Emails Sent.</center></div>"; 
    } else {
        echo "<div class=\"box red\"><center>$count Emails Sent.</center></div>"; 
    }
}