2

こんにちは、doc、docx、pdf を添付してメールを送信しようとしています。doc と docx でメールを受信して​​います。しかし、pdf を添付してメールを送信しようとすると、機能しません。したがって、「アップロードされたファイルはサポートされていないファイル タイプです」というエラー メッセージが表示されます。エラーが見つかりません。誰でも私を助けることができますか?私のコード:

<?php 
    $to = $_POST["txtTo"];
    $subject = $_POST["txtSubject"];
    $from = $_POST["txtFormEmail"];
    $message = $_POST["txtDescription"];
    $random_hash = md5(date('r', time()));
    $headers .= "From: ".$from."<".$from.">\nReply-To: ".$_POST["txtFormEmail"]."";
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    $strFilesName = $_FILES["fileAttach"]["name"];
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
    ob_start();
    //Turn on output buffering 
    ?> --PHP-mixed-<?php  echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php  echo $random_hash; ?>" --PHP-alt-<?php  echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --PHP-alt-<?php  echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <?php  echo $message; ?> --PHP-alt-<?php  echo $random_hash; ?>-- --PHP-mixed-<?php  echo $random_hash; ?> Content-Type: <?php  echo $_FILES["fileAttach"]["type"]; ?>; name="<?php  echo $strFilesName; ?>" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php  echo $attachment; ?> --PHP-mixed-<?php  echo $random_hash; ?>-- <?php 
    $message = ob_get_clean();

    if ($_FILES["fileAttach"]["type"] == "application/pdf"|| $_FILES["fileAttach"]["type"]=="application/msword"||$_FILES["fileAttach"]["type"]==application/vnd.openxmlformats-officedocument.wordprocessingml.document")if($_FILES["fileAttach"]["size"] < 1024000){if ($_FILES["fileAttach"]["error"] > 0){ echo "Error:
    " . $_FILES["fileAttach"]["error"]."<br />";}else{$mail_sent = @mail( $to, $subject, $message, $headers );
}

echo $mail_sent ? "Mail sent" : "Mail failed";}else{echo ' File Size should be with in 1000 KB';} }else{echo 'The uploaded file is not supported file type.'; }?>
4

4 に答える 4

9

シンプルなphpメール機能を使用してメールを送信し、dompdfを使用してhtmlをpdfに変換すると正常に動作します。

        <?php 
        $content="<html>html content here</html>"      
        $html2pdf = Yii::app()->ePdf->HTML2PDF();
        $html2pdf->WriteHTML($content);
        $to = "dheerajchouhan85@gmail.com";
        $from = "no-reply@email.com";
        $subject = "Thank you for your Contribution";
        $message = "<p>Your Message</p>";

        $separator = md5(time());
        $eol = PHP_EOL;
        $filename = "example.pdf";
        $pdfdoc = $html2pdf->Output('', 'S');
        $attachment = chunk_split(base64_encode($pdfdoc));
        $headers = "From: " . $from . $eol;
        $headers .= "MIME-Version: 1.0" . $eol;
  $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
  $body = "Content-Transfer-Encoding: 7bit" . $eol;
  $body .= "This is a MIME encoded message." . $eol; 
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
        $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
        $body .= $message . $eol;
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol;
        $body .= "Content-Disposition: attachment" . $eol . $eol;
        $body .= $attachment . $eol;
        $body .= "--" . $separator . "--";
        mail($to, $subject, $body, $headers);
         ?>
于 2014-03-03T07:34:55.723 に答える
5

Swiftmailerを使い始めると、あなたの生活は楽になります。

使用例:

require_once('swift/lib/swift_required.php');

$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
    ->setFrom(array($from))
    ->setTo(array($to))
    ->setEncoder(Swift_Encoding::get7BitEncoding())
    ->setSubject($subject)
    ->setBody($body, 'text/html')
    ->addPart(strip_tags($body), 'text/plain')
    ->attach(Swift_Attachment::fromPath($filename))
;
$mailer->send($message);
于 2013-08-23T07:02:51.827 に答える
1
<?php 
    //define the receiver of the email 
    $to = 'elangovan2men@gmail.com'; 

    //define the subject of the email 
    $subject = 'Test email with attachment'; 

    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 

    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: test@gmail.com\r\nReply-To: test@gmail.com"; 

    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

    //read the atachment file contents into a string,encode it with MIME base64,and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('testing.pdf'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    Hello World!!! 
    This is simple text email message. 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    <h2>Email Functionality</h2> 
    <p>This is something with <b>HTML</b> formatting.</p> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/pdf; name="attachment.pdf"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  

    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>
于 2014-08-08T10:06:12.333 に答える