Gcobani Posted March 10, 2023 Share Posted March 10, 2023 Hi Team I need some help with my logic, i am trying to send email attachment i managed to create one attachment. But the problem is the document i cant able to view it says "failed to load pdf document, expected application/pdf.pf , found plain text". Logic to achieve this using php mail function. <?php $filenameee = $_FILES['id']['name']; //$fileName = $_FILES['id']['temp_name']; $amount =$_REQUEST['amount']; $purpose =$_REQUEST['purpose']; $voucher =$_REQUEST['voucher']; $gender =$_REQUEST['gender']; $fname =$_REQUEST['fname']; $lname =$_REQUEST['lname']; $title =$_REQUEST['title']; $typeID =$_REQUEST['typeID']; $email =$_REQUEST['email']; $number =$_REQUEST['number']; $maritalstatus =$_REQUEST['maritalstatus']; $street1 =$_REQUEST['street1']; $street2 =$_REQUEST['street2']; $town =$_REQUEST['town']; $province =$_REQUEST['province']; $code =$_REQUEST['code']; $propertyownership =$_REQUEST['propertyownership']; $placeofwork =$_REQUEST['placeofwork']; $jobtitle =$_REQUEST['jobtitle']; $jobstreet=$_REQUEST['jobstreet']; $jobstreet2 =$_REQUEST['jobstreet2']; $jobtown =$_REQUEST['jobtown']; $worknumber =$_REQUEST['worknumber']; $jobprovince =$_REQUEST['jobprovince']; $jobcode =$_REQUEST['jobcode']; $income =$_REQUEST['income']; $bankname=$_REQUEST['bankname']; $branchname =$_REQUEST['branchname']; $accountno =$_REQUEST['accountno']; $accounttype =$_REQUEST['accounttype']; $signature =$_REQUEST['signature']; $bankstatement =$_REQUEST['bankstatement']; $payslip=$_REQUEST['payslip']; $message = "Name: ". $fname . $lname . "\r\n Email: " . $email . "\r\n Cell Number: " . $number . "\r\n Monthly Income: " . $income . "\r\n Loan Amount: " . $amount . "\r\n Town:" . $town; $subject ="Application Form"; $fromname ="ACI FINANCE"; $fromemail = $email; //if u dont have an email create one on your cpanel $mailto = 'gcobani.Mkontwana@agilelimitless.org.za'; //the email which u want to recv this email $content = file_get_contents($filenameee); $content = chunk_split(base64_encode($content)); // a random hash will be necessary to send mixed content $separator = md5(time()); // carriage return type (RFC) $eol = "\r\n"; // main header (multipart mandatory) $headers = "From: ".$fromname." <".$fromemail.">" . $eol; $headers .= "MIME-Version: 1.0" . $eol; $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol; $headers .= "Content-Transfer-Encoding: binary"; $headers .= "This is a MIME encoded message." ; $headers .="Content-Length: . filesize($filenameee)"; $headers .="Accept-Ranges: bytes" ; @readfile($filenameee); $logo = 'img/logo.png'; $link = '#'; $body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>"; $body .= "<table style='width: 100%;'>"; $body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>"; $body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>"; $body .= "</td></tr></thead><tbody><tr>"; $body .= "<td style='border:none;'><strong>Name:</strong> {$fname} {$lname}</td>"; $body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>"; $body .= "</tr>"; $body .= "<tr><td style='border:none;'><strong>CellNumber:</strong> {$number}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td style='border:none;'><strong>Loan Amount:</strong> {$amount}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td style='border:none;'><strong>Monthly income:</strong> {$income}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td style='border:none;'><strong>Town:</strong> {$town}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td style='border:none;'><strong>Upload bankstatement </strong> {$bankstatement}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td style='border:none;'><strong>Upload payslip:</strong> {$payslip}</td></tr>"; $body .= "<tr><td></td></tr>"; $body .= "<tr><td colspan='2' style='border:none;'><strong></strong> </td></tr>"; // message //$body = "--" . $separator . $eol; $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol; $body .= "Content-Transfer-Encoding: binary" . $eol; $body .= $message . $eol; // attachment $body .= "--" . $separator . $eol; $body .= "Content-Type: application/pdf; name=\"" . $filenameee . "\"" . $eol; $body .= "Content-Transfer-Encoding: binary" . $eol; $body .= 'Content-Disposition: inline; $filenameee="' . $filenameee . '"'; $body .= $content . $eol; $body .= "</tbody></table>"; $body .= "</body></html> Quote Link to comment Share on other sites More sharing options...
requinix Posted March 10, 2023 Share Posted March 10, 2023 Don't do emails yourself like this. Use a library like PHPMailer or SwiftMailer. They know how to send emails properly. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 10, 2023 Share Posted March 10, 2023 1 hour ago, Gcobani said: //$fileName = $_FILES['id']['temp_name']; the uploaded file is at the path/name given by the ['tmp_name'] element. you must also test if the file upload was successful before referencing any of the uploaded file information. if the total size of the post form data exceeds the post_max_size setting, both the $_POST and $_FILES arrays will be empty. after you detect if a post method form was submitted, you must test for this condition. after you have detected that there is data in $_FILES, you must test the ['error'] element to make sure the file was uploaded without an error. ref: https://www.php.net/manual/en/features.file-upload.php for the $_POST field data, use the $_POST array, not the $_REQUEST array. don't copy variables to variables for nothing, this is just a waste of typing. if you have more than about two form fields, you should use a data-driven design, and dynamically validate and process the form data. except maybe during testing, when you are entering your own email address at your web hosting in the form, these emails are not being sent from the email address that someone enters in the form. they are being sent from the mail server at your web hosting and the from email address must either directly correspond to the sending mail server or you must set the dns zone records at the sending mail server to indicate it is allowed to send email for the domain in the from email address. Quote Link to comment Share on other sites More sharing options...
Gcobani Posted March 10, 2023 Author Share Posted March 10, 2023 @mac_gyver what should i use in shoring i achieve this....? I have been stuck with this problem for almost a week now. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 10, 2023 Share Posted March 10, 2023 you need to attempt to implement each point that was made and observe the result. ask specific programming questions if you get stuck after attempting each point. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.