Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:13:46+00:00 2026-05-28T14:13:46+00:00

I’m trying to send an encrypted email with an attachment from PHP, however, my

  • 0

I’m trying to send an encrypted email with an attachment from PHP, however, my e-mail is just diplayed as plain text in the email-client (in this case MS Outlook).
This is the code I use to send the email:

$semi_rand = md5(time());   
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";   
$fileatt = "\path\to\attachment";
$headers = array();
$headers['From'] = $email_from;
$headers['Subject'] = $email_subject; 
$headers['MIME-Version'] = "1.0";
$headers['Content-Type'] = "multipart/mixed; boundary=\"{$mime_boundary}\"";    

$file = fopen($fileatt,'rb');   
$data = fread($file,filesize($fileatt));
$data = chunk_split(base64_encode($data));     
fclose($file);   

//message part
$email_message = "This is a multi-part message in MIME format.\n\n" .   
                "--{$mime_boundary}\n" .   
                "Content-Type:text/html; charset=\"UTF-8\"\n" .   
                "Content-Transfer-Encoding: 7bit\n\n
                                     Please find the file attached\n\n";   


//file part
$email_message .= "--{$mime_boundary}\n" .   
                  "Content-Type: {$fileatt_type};\n" .   
                  " name=\"{$fileatt_name}\"\n" .   
                  "Content-Transfer-Encoding: base64\n\n" .   
                 $data . "\n\n" .   
                  "--{$mime_boundary}--\n";   

$mfile = fopen("msg.txt", "w");
fwrite($mfile, $email_message);
fclose($mfile);


$key = file_get_contents("mailcert.cer");

$encrypt = openssl_pkcs7_encrypt("msg.txt", "enc.txt", $key, $headers);
if($encrypt){
    $data = file_get_contents("enc.txt");
    $parts = explode("\n\n", $data, 2);

    // Send mail
    $ok = mail($email_to, $email_subject, $parts[1], $parts[0]);
}  

The script works, the email is delivered and it’s possible to decrypt it in Outlook, however, the result then is something like this:

--==Multipart_Boundary_x6434b5a09f1f49c571a633802cd36772x

Content-Type:text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Please find the file attached

--==Multipart_Boundary_x6434b5a09f1f49c571a633802cd36772x
Content-Type: application/octet-stream;
 name="1327490599scrippie.txt"
Content-Transfer-Encoding: base64

JG9sZElwID0gIjE5NS40Ni4zOS43MyINCiRuZXdJcCA9ICIqIg0KDQojIEdldCBhbGwgb2JqZWN0
cyBhdCBJSVM6Ly9Mb2NhbGhvc3QvVzNTVkMNCiRpaXNPYmplY3RzID0gbmV3LW9iamVjdCBgDQog
ICAgU3lzdGVtLkRpcmVjdG9yeVNlcnZpY2VzLkRpcmVjdG9yeUVudHJ5KCJJSVM6Ly9Mb2NhbGhv
[etc....]

--==Multipart_Boundary_x6434b5a09f1f49c571a633802cd36772x--

Is there another way of sending encrypted emails with attachments? Or is there a solution to this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:13:47+00:00Added an answer on May 28, 2026 at 2:13 pm

    The solution to this problem is to include the headers for the original message in the file containing the message.

    I added something like this before writing the file to disk:

    foreach($headers as $headerkey => $headerval){
        $email_message = $headerkey . ": " . $headerval . "\r\n" . $email_message;
    }
    

    Then, remove the MIME-version and Content-Type headers from the array before passing it to the openssl_pkcs7_encrypt() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.