こんにちは私は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);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加