0

what I want to do is to read a document that could be a pdf, doc or docx file. And add its contents with same styling in an email body. Currently, I am sending the document as an attachment but I want it to be sent in my email body. I tried searching for a generic solution that I could use for all these different types of documents, but couldn't find something related to my scenario. Most of the solutions were related to sending file as an attachment. Your help is appreciated. Thanks.

Here's the code I'm using to send the document as an attachment.

$file = 'path/to/my/file.pdf';
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_name = basename($file);

// header
$header = "From: Support <info@support.com>\r\n";
$header .= "Reply-To: info@support.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/html; charset=\"iso-8859-1\"\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";

if (mail($mailto, $subject, $nmessage, $header))
    return true; // Or do something here
else
    return false;
ItWorksOnLocal
  • 163
  • 2
  • 17
  • 1
    I doubt you will find one generic solution that works for all file types since they all have different formats. You should write different functions that reads the different formats and then call the correct function depending on which format it is. And reading the files and add the contents in a well formatted way in an email won't be trivial at all. – M. Eriksson Sep 25 '21 at 13:12
  • @MagnusEriksson, any suggestions on which function should I use for reading doc files? I used file_get_contents but its returning giberish due to which I think I'm missing some encoding flags or something. – ItWorksOnLocal Sep 25 '21 at 13:19
  • You need to do some [proper research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Start with google something like "read doc files in PHP" and then continue doing the same for the other file types. There are libraries that can help you with it (since reading those file types are quite complex) so with some research, you'll most likely find some. – M. Eriksson Sep 25 '21 at 13:24
  • You'll probably need to extract the contents of the PDF and Word (doc, docx) files into HTML or other text mark-up. There are tools to do this in Python, and they are open source. A quick search can reveal them for you and you could explore if there is a PHP version. Here's a Python Stackoverflow question to help you start https://stackoverflow.com/questions/22756344/how-do-i-extract-data-from-a-doc-docx-file-using-python – JimmyNJ Sep 25 '21 at 22:14

0 Answers0