Привет, я пытаюсь отправить почту с 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.'; }?>
Начните использовать 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);
Используйте Simple php mail для отправки почты, а 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); ?>
<?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"; ?>
Надеюсь, это полезно для вас.
<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 = 'noreply@your_domain.com'; //from mail, it is mandatory with some hosts $recipient_email = 'your-emailid@gmail.com'; //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!'); } } ?>