如何解码以Base64文本形式接收的电子邮件附件

9

我有一个仅包含文本的电子邮件备份文件。如何检索附加在其中的文档(PDF、图像、Word文件)并将其作为普通文件呈现?


2
如果你在Linux上,base64程序可以很好地将Base64字符串(例如JVBERi0xLjUNCiW1tbW)转换为目标格式。你可以在这里找到一些例子。 - Matthias Braun
2个回答

9
  1. Select the long string of text which appears in your email. That is probably one of the attachments, it usually starts like this:

    --bcaec554d754b0f76a04d9fda578--
    --bcaec554d754b0f77204d9fda57a
    Content-Type: application/pdf; name="test.pdf"
    Content-Disposition: attachment; filename="Otest.pdf"
    Content-Transfer-Encoding: base64
    X-Attachment-Id: 9ba6310dffca527f_0.1
    
  2. Copy this long string and paste it in the Base64 decoder found here.

  3. Download the output and rename it by adding the appropriate extension to it. For example testfile.pdf or filename.docx.

就是这样,你只需要使用Base64解码就可以重新创建丢失的附件。


1
如果我想以编程方式自行进行解码,该怎么办? - Ozan Kurt
@OzanKurt,您能否提供更多上下文信息,包括您打算使用哪种编程语言,以及您是在本地运行还是在特定服务器上运行。此外,是否涉及获取电子邮件附件等操作? - 13thOlympian
我已经通过PHP成功地使其工作了。但是当我手动执行该过程时,它似乎无法正常工作...有点奇怪。 - Ozan Kurt
@OzanKurt 很好!很高兴你解决了问题。也许最好能够在下面总结一下你的答案,以便其他有兴趣的人参考。祝编码愉快! - 13thOlympian

0

以下是如何使用PHP对文件进行解码的方法

 function base64_to_jpeg( $inputfile, $outputfile ) { 
  /* read data (binary) */ 
  $ifp = fopen( $inputfile, "rb" ); 
  $imageData = fread( $ifp, filesize( $inputfile ) ); 
  fclose( $ifp ); 
  /* encode & write data (binary) */ 
  $ifp = fopen( $outputfile, "wb" ); 
  fwrite( $ifp, base64_decode( $imageData ) ); 
  fclose( $ifp ); 
  /* return output filename */ 
  return( $outputfile ); 
}

如果你只想在网页上显示,或者使用 HTML,可以使用 HTML。

<img src="data:image/png;base64,Your_Base_64_Code_Here">

你可以将其用作CSS背景:

url('data:image/png;base64,Your_Base_64_Code_Here')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接