2013-08-23 167 views
2

嗨,我想发送带有doc,docx和pdf附件的邮件。我正在收到doc和docx的电子邮件。但是当我尝试用pdf附件发送邮件时,它不起作用。因此,我收到错误消息'上传的文件不支持文件类型'。我不能找到错误。谁能帮我?我的代码:使用Swiftmailer如何使用php发送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.'; }?> 
+3

我不知道谁有胆量编辑您的问题 –

回答

5

开始,你的生活会更容易。

使用例如:

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); 
+0

我的代码工作正常,除了PDF文件。你能帮我编辑我的代码吗? – user2613946

+0

我在将代码放入本网站时遇到问题。所以它看起来不整洁。对不起。无论如何感谢您的帮助。 – user2613946

+1

谢谢你的朋友。我解决了这个问题。我将内容类型从application/pdf更改为application/octet-stream。现在它工作正常。 – user2613946

6

使用简单的PHP邮件功能来发送邮件和DOMPDF将HTML转换成PDF格式将正常工作。

 <?php 
     $content="<html>html content here</html>"  
     $html2pdf = Yii::app()->ePdf->HTML2PDF(); 
     $html2pdf->WriteHTML($content); 
     $to = "[email protected]"; 
     $from = "[email protected]"; 
     $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); 
     ?> 
+1

+1为自定义代码。 – Gogol

+0

对答案和noc2spam的评论+1。 我不明白,OP询问“如何使用PHP的邮件命令发送邮件”,许多答案就像“不要使用邮件,使用更简单的东西”! – Marecky

1
<?php 
    //define the receiver of the email 
    $to = '[email protected]'; 

    //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: [email protected]\r\nReply-To: [email protected]"; 

    //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"; 
?> 
0

希望这对您有所帮助。

<form enctype="multipart/form-data" method="POST" action=""> 
    <label>Your Name <input type="text" name="sender_name" /> </label> <br> 
    <label>Your Email <input type="email" name="sender_email" /> </label> <br> 
    <label>Subject <input type="text" name="subject" /> </label> <br> 
    <label>Message <textarea name="message"></textarea> </label> <br> 
    <label>Attachment <input type="file" name="my_file" /></label><br> 
    <label><input type="submit" name="button" value="Submit" /></label> 
</form> 

<?php 
echo "<pre>";print_r($_REQUEST);echo "</pre>"; //comment this line 
echo "<pre>";print_r($_FILES);echo "</pre>"; //comment this line 
if($_POST && isset($_FILES['my_file'])) 
{ 

    $from_email   = '[email protected]_domain.com'; //from mail, it is mandatory with some hosts 
    $recipient_email = '[email protected]'; //recipient email (most cases it is your personal email) 

    //Capture POST data from HTML form and Sanitize them, 
    $sender_name = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name 
    $reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header 
    $subject  = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); //get subject from HTML form 
    //$message  = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message 
    $message  = "Name : ".$sender_name."\nMessage : ".$_POST["message"]; //message 

    //Get uploaded file data 
    $file_tmp_name = $_FILES['my_file']['tmp_name']; 
    $file_name  = $_FILES['my_file']['name']; 
    $file_size  = $_FILES['my_file']['size']; 
    $file_type  = $_FILES['my_file']['type']; 
    $file_error  = $_FILES['my_file']['error']; 

    if($file_error > 0) 
    { 
     die('Upload error or No files uploaded'); 
    } 
    //read from the uploaded file & base64_encode content for the mail 
    $handle = fopen($file_tmp_name, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $encoded_content = chunk_split(base64_encode($content)); 

     $boundary = md5("sanwebe"); 
     //headers 
     $headers = "MIME-Version: 1.0\r\n"; 
     $headers .= "From:".$from_email."\r\n"; 
     $headers .= "Reply-To: ".$reply_to_email."" . "\r\n"; 
     $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

     //body plain text 
     $body = "--$boundary\r\n"; 
     $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
     $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
     $body .= chunk_split(base64_encode($message)); 

     //attachment file 
     $body .= "--$boundary\r\n"; 
     $body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
     $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
     $body .="Content-Transfer-Encoding: base64\r\n"; 
     $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
     $body .= $encoded_content; 
    $sentMail = @mail($recipient_email, $subject, $body, $headers); 
    if($sentMail) //output success or failure messages 
    {  
     die('Thank you for your email'); 
    }else{ 
     die('Could not send mail!'); 
    } 
} 
?> 
相关问题