I created a codes using php its working already with attachment but its only .doc i want to change it to .pdf i tried to change the application/msword to application/pdf and the file name from PO.doc to PO.pdf but its corrupted when i receive it on my email. Can you suggest me what should i do? Below is my codes for that. Thank you.
<?php
$headers = "From:<noreply#example.com>";
$to = 'email#example.com';
$subject = 'Purchase Order';
$txt .="
<html>
<body>
<p><b> PO Number:</b> $purchasenumber</p>
<p><b> Style Code:</b> $styleCode</p>
<p><b> Generic Number:</b> $gennum</p>
<p><b> Vendor Name:</b> $vendname</p>
<p><b> Planned Delivery Date:</b> $pdelivdate</p> <br/> <br/>";
// Always set content-type when sending HTML email
$message = "MIME-Version: 1.0" . "\r\n";
// $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fileatt_name2 = "PurchaseOrder.doc";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$data2 = chunk_split(base64_encode($txt));
$message = "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: application/msword;\n" . // {$fileatt_type}
" name=\"{$fileatt_name2}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name2}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";
// Send the message
$send = mail($to, $subject, $message, $headers);
?>
This is not as simple as changing the extension (.doc, .pdf etc.) to convert a file from one type to the other. A more specialized process is needed. What you tried is analogous to taking an apple and dressing it up as a pear. It may look like a pear at first, but when taking a bite you'd discover it is actually an apple.
If you want to convert the .doc to a PDF you can convert it using programs like Microsoft Word (although I'm sure that Libre- or Open-office can do this too). If a more automated manner is desired, consider implementing other solutions like livedocs or phpdocs although the latter is quite pricy.
Depending on the platform (Linux, Windows, OSX) other options might be available as was answered in questions like this one.
Related
I'm trying to send an email with an attachment in PHP 8 and, whilst it sends the email with the attachment okay, the attachments all come out as corrupted for the recipient. I've tried jpg, png and pdf and they all come out corrupted.
Worked fine with PHP 7.4 and previous but doesn't seem to work with PHP 8.
What has changed?
Here is the code -
Note: the other mail variables $recipient_email,$subject,$message are set prior to this code.
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name1 = $_FILES['attachment']['tmp_name'];
$type1 = $_FILES['attachment']['type'];
$name1 = $_FILES['attachment']['name'];
$size1 = $_FILES['attachment']['size'];
if (file_exists($tmp_name1)){
if(is_uploaded_file($tmp_name1)){
$file1 = fopen($tmp_name1,'rb');
$data1 = fread($file1,filesize($tmp_name1));
fclose($file1);
$data1 = chunk_split(base64_encode($data1));
}
$headers = "From: $myname <$mymail>\r\n" .
"Reply-To: <$mymail>\r\n" .
"Return-Path: <$mymail>\r\n";
"X-Mailer: PHP\r\n" .
"Disposition-Notification-To: ".$mymail." <$bcc>\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$body = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$body .= "--{$mime_boundary}\n" .
"Content-Type: {$type1};\n" .
" name=\"{$name1}\"\n" .
$data1 . "\n\n" .
"Content-Transfer-Encoding: base64\n\n";
$body .= "--{$mime_boundary}--\n";
mail($recipient_email,$subject,$body,$headers,"-f ".$mymail);
DOH, silly me.I had two lines of code transposed.
Should be this at the end -
"Content-Transfer-Encoding: base64\n\n" .
$data1 . "\n\n";
Instead of -
$data1 . "\n\n" .
"Content-Transfer-Encoding: base64\n\n";
I've seen the other questions/answers in StackOverflow but I cannot for the life of me get this to work correctly, and I'm really curious to see what I'm doing wrong.
An insurance company has PDFs that they'd like their customers to fill online and submit via their site. The PDF form will be sent via email as a PDF attachment all filled out.
I have a PHP script that sends the PDF, but it's not saving form data (names, addresses, etc.). It's only sending the blank PDF. Here's the code:
<?php
$fileatt = "file-name.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "file-name.pdf";
$email_from = "From"; // Who the email is from
$email_subject = "Form"; // The Subject of the email
$email_message = "Text.";
$email_to = "someemail#gmail.com"; // Who the email is to
$headers = "De: ".$email_from;
//no need to change anything else under this point
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo 'Sent...';
//unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs
}
else {
echo 'Error...';
}
?>
I appreciate any input/feedback. I've thought about using PHPMailer but I think this is like 99% done and I'm curious to see what it's missing?
(The PDF file was created with Word then using Adobe Acrobat Pro all the input fields and the submit button was created, the submit button points to the PHP script that sends the PDF)
I am testing an auto reply multipart email using PHP. The problem is when I receive the email in my hotmil, the whole content (including html and the random hashes) appears as raw text. Here is my code:
$cname = "test";
$to = "me#myemail.com";
$autoReplyTo = "me#myemail.com";
$autoReplySubject = "Enquiry";
$mime_boundary = md5(date('U'));
$autoReplyheaders = "From: XXXXX <" . $to . ">" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
"Content-Type: multipart/alternative; boundary=$mime_boundary" .
"Content-Transfer-Encoding: 7bit". "\r\n";
$plain_text = "Dear " . $cname . ".\r\n";
$plain_text = "Thank you for contacting us.\r\n";
$plain_text .= "We are currently processing your query and will contact you shortly. We appreciate the time and interest you have shown in our company.\r\n";
$plain_text .= "Sales Team\r\n";
$plain_text .= "Note: This is an auto-generated email, please do not reply.\r\n";
$html_text = '<html><head><title>AUTO REPLY</title></head><body>';
$html_text .= '<p><img src="http://www.xxxxxx.xx/images/logo.png" /></p>';
$html_text .= '<p>Dear '.$cname.',<br />
Thank you for contacting us.<br />
We are currently processing your query and will contact you shortly.<br />
We appreciate the time and interest you have shown in our company.</p>';
$html_text .= '<p><b>Sales Team</b></p>';
$html_text .= '<p><i>Note: This is an auto-generated email, please do not reply.</i></p>';
$html_text .= '</body></html>';
$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text.
"--" . $mime_boundary.
"Content-Type: text/html; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text.
"--".$mime_boundary."--";
mail($autoReplyTo, $autoReplySubject, $autoReplyMessage, $autoReplyheaders);
What am I doing wrong?
This may or may not be the only issue, but you are missing line breaks after the Content-Type headers in the plain and HTML sections:
$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text. "\r\n".
//---------^^^^^^^^^
// Added another linebreak before MIME boundary
"--" . $mime_boundary.
"Content-Type: text/html; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text."\r\n\r\n".
// ---------^^^^^^^^^^
"--".$mime_boundary."--";
Rather than attempt to craft multipart/mime messages manually, lots of us here on SO would recommend a mailing library like PHPMailer which handles this much more easily. Building messages manually tends to be quite error-prone, and subject to inconsistencies between implementations by the SMTP server, or differences between native platform linebreaks.
I'm trying to generate a VCard via PHP, and them email it out to the user. I wrote an initial script with hard-coded data, but the end-result will fill in the VCard from MySQL.
When viewing the VCard side-by-side with a legitimate VCard (downloaded and tested from another site) they look pretty much identical, but when I try and import my generated VCard it shows up with no data. Actually, if I open it on my phone, it doesn't even recognize that it is a vcard, and instead just sends me to a broken Google Doc.
I've borrowed some code from wikipedia for formatting the vcard, and everything seems fine. Do you see any errors in my formatting? I've tried different line breaks - to no avail. Ideas?
Here is the code for my generation / mail:
<?php
$content = "BEGIN:VCARD\r";
$content .= "VERSION:3.0\r";
$content .= "CLASS:PUBLIC\r";
$content .= "FN:Joe Wegner\r";
$content .= "N:Wegner;Joe ;;;\r";
$content .= "TITLE:Technology And Systems Administrator\r";
$content .= "ORG:Wegner Design\r";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r";
$content .= "EMAIL;TYPE=internet,pref:__munged__#wegnerdesign.com\r";
$content .= "TEL;TYPE=work,voice:__munged__\r";
$content .= "TEL;TYPE=HOME,voice:__munged__\r";
$content .= "URL:http://www.wegnerdesign.com\r";
$content .= "END:VCARD";
mail_attachment("Joe Wegner.vcf", $content, "__munged__#wegnerdesign.com", "__munged__#wegnerdesign.com", "Wegner Design Contacts", "__munged__#wegnerdesign.com", "Joe Wegner's Contact Info", "");
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "application/octet-stream";
$headers = "FROM: ".$from_mail;
$data = chunk_split(base64_encode($content));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
echo "sending message";
mail($mailto, $subject, $message, $headers);
}
?>
Update 1: This makes me more confused, but perhaps it will help you debug. I've downloaded my (bad) generated VCard to my computer, as well as a good VCard downloaded from a different website. As expected, my generated one opens with no data, but the good one works fine. I then created a third empty file with a .vcf extension, and copied the text from my (bad) file into that empty file. I opened that file, and all the data showed perfectly. To test even further, I copied the text from the good VCard file into my bad file, and it still opened with no data. So, it appears it's something about encoding or some other file thing that I don't understand. It's not permissions - that's all identical.
Update 2: I changed my PHP so that it would force me to download the VCard as well as email it. The downloaded file opens perfectly fine, so the error is either happening in how I'm encoding (right word?) the file, or how GMail is interpretting it.
Update 3: Fixed : Figured it out. I'm not sure why this is - because every other tutorial I can find out there says the opposite - but there were a few key changes. First, I changed the encoding on the email from base64 to 8bit, and I changed the attachment content to just be the string passed to the email function (so that it is in 8bit form, not 64). That made the VCard valid and readable on my desktop. To get it to read on my android I had to change the $fileatt_type variable to "text/x-vcard", otherwise Gmail thinks it is a document.
You are missing a \n at the end of each line. If you look at a normal vCard (with notepad++ for example), it has a CR and LF at the end of each line, the one you create only has CR ('\r'). this works for me:
$content = "BEGIN:VCARD\r\n";
$content .= "VERSION:3.0\r\n";
$content .= "CLASS:PUBLIC\r\n";
$content .= "FN:Joe Wegner\r\n";
$content .= "N:Wegner;Joe ;;;\r\n";
$content .= "TITLE:Technology And Systems Administrator\r\n";
$content .= "ORG:Wegner Design\r\n";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r\n";
$content .= "EMAIL;TYPE=internet,pref:joe#wegnerdesign.com\r\n";
$content .= "TEL;TYPE=work,voice:7089181512\r\n";
$content .= "TEL;TYPE=HOME,voice:8352355189\r\n";
$content .= "URL:http://www.wegnerdesign.com\r\n";
$content .= "END:VCARD\r\n";
I had to get vcards working in android and iphone recently. I used the following function which is a modified version of the one listed above. This will send vcards that both gmail and mail on the iphone will be able to open. They work in Thunderbird as well.
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "text/x-vcard";
$headers = "FROM: ".$from_mail;
$data = $content;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
//echo "sending message";
mail($mailto, $subject, $message, $headers);
}
I wrote a vCard validator based on the RFC which could help. It's not complete, but the cleaned files are at least nicely compatible with the tools and services I've tried (Gmail, gnokii and some others I can't remember). HTH.
I'm writing a mailform i php for placeing orders, and sense they have to get send a picture to me for the order to work properly, I'd like to be able to attach the file in the formmail. How shuld I do this? I have seen some different sulutions but non that I've complety understand.
You need to set the right mail-headers, and then attach the file by encoding it to whatever form you have declared in the header, like in this snippet:
All you need to do here, is read the file, and encode it (to base64 in this case)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
first you'll need a boundary, like a rule to tell where one part stops, and the other begins
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
then set the headers right, to support attachement
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
then build up your message
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" . // start text block
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_content . "\n\n" .
"--{$mime_boundary}\n" . // start attachement
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" . // this is the file...
"--{$mime_boundary}\n";
and then... sent the message using mail ;-)
mail($email_to, $email_subject, $email_message, $headers)
I would also suggest php_mailer http://sourceforge.net/project/showfiles.php?group_id=26031
Has all of the options you could ever want and lets you build custom length forms without "TOO" much pain
There are also a bunch of tutorials and would gladly send along an example of a project I did recently if you want
$sl="select max(id)AS maxid from photos";
$res=mysql_query($sl);
$rowl=#mysql_fetch_array($res);
$adid=$rowl['maxid'];
$filedir="/photo_gallery/";
$file1=$filedir."img".$adid.$_FILES['myfile']['name'];
//echo $file1;
#move_uploaded_file($_FILES['myfile']['tmp_name'],$file1);
$upd="update photos set photo='".$file1."',Added_date=now() where id=$adid";
//echo $upd;
mysql_query($upd);
#unlink($file1);