简体   繁体   中英

Base64 email attachments are not uploading

I am using the following script http://stuporglue.org/recieve-e-mail-and-save-attachments-with-a-php-script/ to handle my emails that get sent to me, however it seems if a user sends an email from mail or outlook or any email client that sends base64 attachments they are not being saved in the data base, and the body text of the email is also skiped.

I am wondering if anyone sees an error in the code, as I have looked and dont see anything that sticks out.

a closer look shows the following Mail.app sends its base64 like this.

    --Apple-Mail=_9E76B10A-4086-43B8-B835-78F184FA43FC
Content-Disposition: inline;
    filename=CV-IT.pdf
Content-Type: application/pdf;
    name="CV-IT.pdf"
Content-Transfer-Encoding: base64

JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nM1aS4/jNgy+z6/wucCkFmXJNjAwkMwkBXrbdoAeip76Aopuge5l/34l
kpKol+NseygGq01sPSiS30eKynhSw+env4dxGN0ns5qTHpZJndbh06/DD18Nf9E79/fp96fL+xOM
ywmGeR5d+/7L8PVNDcoM77/9+DKqDV5G2J7Vy6i3Zx3aaXONwY/U2s31mPHj4t/hOGxWfHj27yd6
89P7t0/X96cPhRRqVqdlmM1EMsCgdJDBzwDZ8m424xbVadEooVsQ/IKuufgOr7HBZ9TpTWzp6t+l
rjcWGF7UiFP5uZWKCyjwTxV9nrBHNadCrSiLLUnY3Teo2Vlp1kvQfty58qokIdRKG1VnnMwLqi68
1iuv84btlZ/ehAT1Z9HCyKNL+U5mVKsVPuKkhOk0k5R6UMD2GaFwDm8dr2tsbDQRyMcLOYdis9jx
Qt+U+3vlIRM9cBszzkzUETstYvabf/DqXgr5Fe5ADVLqnz8+je6/z9nmvvvmaRyelev0cVDz6oCC
X/4cvq/906zujV3BeWlACXvohZzt/9R0nM1YJ721WiCd9+BgM3nH8j785ue4+uZGHogAQNfX9MK5
v1f+htDvAXry9CMWA7kYCGSCEnD0VgZAo246YRRswCMBdI6dGRkJrDymZAaCLOh93Wh1sqVuNLMg
Al5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920fxFM2fdN3/u7TOyql2pBS0F8wfj1zQ04MST
oPRrG9sdFpvfrh6ILlGU9JyiwhMmIbkfHctNSAenMp3yF0gKEmwtPDfxSnv9OU01wyG7d/JvKxwJ+Iqf
X9iuCOOPLV9Q8/ajoxtIybOHq5Yu6W7d95RqT/ZP7V+fNHPlYPJ05cb8kowfLquOe/SOopkZ+Oft
RwpKCrjT0Wm+vXXtT0wT45/H/55cYZUZ9VXeEjV/9K15C21Got5JYmKWjP8GZZb2G5R4NDMwtda6
1NVM1hk3kE1G3rlPZKL2CeZ+Hdp

while Gmail sends it like this.

Content-Type: application/pdf; name="CV-IT.pdf"
Content-Disposition: attachment; filename="CV-IT.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gx86pbon0

JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nM1aS4/jNgy+z6/wucCkFmXJNjAwkMwkBXrbdoAeip76Aopuge5l/34l
kpKol+NseygGq01sPSiS30eKynhSw+env4dxGN0ns5qTHpZJndbh06/DD18Nf9E79/fp96fL+xOM
ywmGeR5d+/7L8PVNDcoM77/9+DKqDV5G2J7Vy6i3Zx3aaXONwY/U2s31mPHj4t/hOGxWfHj27yd6
89P7t0/X96cPhRRqVqdlmM1EMsCgdJDBzwDZ8m424xbVadEooVsQ/IKuufgOr7HBZ9TpTWzp6t+l
rjcWGF7UiFP5uZWKCyjwTxV9nrBHNadCrSiLLUnY3Teo2Vlp1kvQfty58qokIdRKG1VnnMwLqi68
1iuv84btlZ/ehAT1Z9HCyKNL+U5mVKsVPuKkhOk0k5R6UMD2GaFwDm8dr2tsbDQRyMcLOYdis9jx
Qt+U+3vlIRM9cBszzkzUETstYvabf/DqXgr5Fe5ADVLqnz8+je6/z9nmvvvmaRyelev0cVDz6oCC
X/4cvq/906zujV3BeWlACXvohZzt/9R0nM1YJ721WiCd9+BgM3nH8j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P32fRE/Dv8me9Cpp/N0Ql5g0JYXkTrdCqkA54gz7tqsjyvw8GuDwtI5Z
/SP1L83NXeuaG7D7yI0HAqUdaelxS/PyB+ffsG920fxFM2fdN3/u7TOyql2pBS0F8wfj1zQ04MST
oPRrG9sdFpvfrh6ILlGU9JyiwhMmIbkfHctNSAenMp3yF0gKEmwtPDfxSnv9OU01wyG7d/JvKxwJ+Iqf
X9iuCOOPLV9Q8/ajoxtIybOHq5Yu6W7d95RqT/ZP7V+fNHPlYPJ05cb8kowfLquOe/SOopkZ+Oft
RwpKCrjT0Wm+vXXtT0wT45/H/55cYZUZ9VXeEjV/9K15C21Got5JYmKWjP8GZZb2G5R4NDMwtda6
1NVM1hk3kE1G3rlPZKL2CeZ+Hdp

The gmail attachment saves while the mail.app does not.

disclaimer the problematic script in question has more issues, which I will not address, and the answer below is meant as a quick fix for the problem at hand, while hoping to enlighten some readers who were not able to diagnose the problems themselves. /disclaimer

There are two main problems.

Problem 1: split header lines

Look at these headers:

Content-Disposition: inline;
    filename=CV-IT.pdf
Content-Type: application/pdf;
    name="CV-IT.pdf"

versus

Content-Type: application/pdf; name="CV-IT.pdf"
Content-Disposition: attachment; filename="CV-IT.pdf"

Now look at the part that processes these lines:

$info = split("\n",$parts[0]);
..
foreach($info as $line)
{
    if( preg_match("/Content-Type: (.*);/",$line,$matches) )
    {
        $type = $matches[1];
    }
    if( preg_match("/Content-Disposition: attachment; filename=\"(.*)\"/",
        $line,$matches) ) {
        $name = time() . "_" . $matches[1];
    }
    ..
}

This splits the header in lines, and then tries to match every line. Now look at the two headers. The second (working) one has 2 lines, which are perfectly matched.

The first (not working) one has 4 (!) lines. None of these 4 lines matches the patterns.

There are countless ways of tackling this problem, and I'll take a quick&dirty oneliner. Add this line before $info = split("\\n",$parts[0]);

$parts[0] = preg_replace("/\r?\n\s+/"," ",$parts[0]);

it will turn the split headers into oneliners again by looking for newlines followed by whitespace, and replacing them with just one space.

Problem 2: wrong pattern

Assuming you applied the fix above, you have this pattern:

if( preg_match("/Content-Disposition: attachment; filename=\"(.*)\"/", ...

trying to match this line:

Content-Disposition: inline; filename=CV-IT.pdf

Two things go wrong here:

problem 2a: disposition inline/attachment

The pattern clearly looks for the word "attachment", while the line says "inline". This is fixed by replacing attachment by (attachment|inline) , which indicates an alternative. (note that this also captures the disposition type)

problem 2b: filename double quotes

The pattern further looks for filename="(.*)" , while the line has a filename without the quotes.

This is also no major issue, if you insert ? after the " , to indicate that the " is optional, all will work. To make it perfect, you must also ensure that the . will not match the ending " if available, so replace filename="(.*)" with:

filename="?([^"]+)"?

where [^"]+ stands for 'anything but " '.

So if you change these lines:

if( preg_match("/Content-Disposition: attachment; filename=\"(.*)\"/",
    $line,$matches) ) {
    $name = time() . "_" . $matches[1];
}

into

if( preg_match('/Content-Disposition: (attachment|inline); filename="?([^"]*)"?/',
    $line,$matches) ) {
    $disposition = $matches[1];
    $name = time() . "_" . $matches[2];
}

it should work. (note that I changed the pattern to use single quotes, so that you need not escape the double quotes, making things legible)

To make this script fool proof, you should really read the appropriate RFCs, to see what more is to be expected in email headers. This script has a lot of assumptions buried in it.

The problem is that my script is not looking for inline content, only for attached content. With the way you have attached the file, it is inline, hence the

Content-Disposition: inline; filename=CV-IT.pdf

If you attach it, you would instead see Content-Disposition: attachment; filename="CV-IT.pdf"

The Content-Disposition handling is around line 54-64 of the script on my site (linked in original question).

seems you used preg_match to get the boundary of the mail near line 166:

if (preg_match("/boundary=(.*boundary)$/",$line,$matches)){
    $boundary = $matches[1];

You used the "/" character as delimiter of pattern of regular expression and you have the "/" in your boundary content at the same time.

So this may be the reason why your code does not work.

Try this:

if (preg_match("{boundary=(.*boundary)$}",$line,$matches)){
    $boundary = $matches[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM