Corrupted attachment when using URL in a template (Firebase Storage / Google Cloud Storage, ...)
[READ] Step 1: Are you in the right place?
Issues filed here should be about bugs for a specific extension in this repository. If you have a general question, need help debugging, or fall into some other category use one of these other channels:
- For general technical questions, post a question on StackOverflow with the firebase tag.
- For general Firebase discussion, use the firebase-talk google group.
- To file a bug against the Firebase Extensions platform, or for an issue affecting multiple extensions, please reach out to Firebase support directly.
[REQUIRED] Step 2: Describe your configuration
- Extension name: firebase/[email protected]
- Extension version: 0.1.12
- package.json.txt
- SMTP server: MailTrap
[REQUIRED] Step 3: ISSUE
pdf file are broken in the client inbox if send with template
Steps to reproduce:
What happened? How can we make the problem occur?
When attaching pdf file to mail generate with template the pdf is broken (png work fine, it is just pdf). When using the exact same public url for the pdf (stored in firebase storage) and send without the template methode it works perfecty.
Angular V12, Firebase 9.0.0
const filename = 'file.pdf';
const url = await this.storageService.store(buffer, 'receipts/'+receiptId);
const collRef = collection(this.db, "email");
const filename = 'file.pdf';
/** trigger a mail with template */
const emailData = {
to: ['[email protected]'],
template: {
name: "receipt",
data: {
filename: filename,
path: url
},
}
}
await addDoc(collRef, emailData);
/** trigger a simple mail */
const emailData2 = {
to: ['[email protected]'],
message: {
subject: 'Hello from Firebase!',
text: 'This is the plaintext section of the email body.',
html: 'This is the <code>HTML</code> section of the email body.',
attachments: [
{filename: filename,
path: url}
]
}
}
await addDoc(collRef, emailData2);
template as display in firestore:

Expected result
file.pdf, should'nt be broken when attach to a mail generate with a template. NB: it don't broke .png file, i didn't try other file extension
Actual result
file.pdf can't be open when attach to mail generate with a template
Hi @Biokimist,
We are sorry to hear about your experiences with this issue. We have tried to recreate it with this sample pdf: https://pdfgeneratorapi.com/example-documents/52272/pdf. We were able to successfully send a template email and the pdf attached file wasn't corrupted and fully worked.
Please let us know if your issue still persists. If so, please could you provide us with some public API PDF file URL that you are encountering this issue? That will certainly help diagnose it better.
Hi, Thank you I will try and keep you informed. Nicolas
Le lun. 22 nov. 2021 à 16:45, Daniel Smolinski @.***> a écrit :
Hi @Biokimist https://github.com/Biokimist,
We are sorry to hear about your experiences with this issue. We have tried to recreate it with this sample pdf: https://pdfgeneratorapi.com/example-documents/52272/pdf. We were able to successfully send a template email and the pdf attached file wasn't corrupted and fully worked.
Please let us know if your issue still persists. If so, please could you provide us with some public API PDF file URL that you are encountering this issue? That will certainly help diagnose it better.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/extensions/issues/791#issuecomment-975658570, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATG27WBS7WVMZSLXSPEELFTUNJQS5ANCNFSM5G4ICBSQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
Hello @Biokimist, @posiek07, @dackers86
I think I found the problem, or at least an avenue to explore.
The PDF file is actually corrupted because it is its metadata in JSON format and not the file itself. And this is due to the missing query parameter alt=media (More info here).
But the query parameter alt=media is present in my url, and I guess it's the same for @Biokimist. 😥
@posiek07 can you please test and confirm the issue with a file hosted on Firebase / Google Storage please.
PS: When I specify the hard URL in the template file, the file is correct, but when it is dynamic it is corrupted.
Have a nice day 😊
Hi @LefebvreIlyas , @Biokimist
Could you give an example of the URL that you are using, please? The problem might not be related to our extension but attaching dynamic link URL queries in nodemailer package (pdf might need to be downloaded first and attached as a base64). Please, if you could supply the exact URL that is generated by Google Storage that you have a problem with passing as it is a little bit hard to diagnose otherwise. It would be even greater if you could show your method of generating the URL itself, so we can recreate your case a little bit better.
Hello @posiek07,
Yes of course !
In my case, it's in a Flutter application, so I use the Dart SDK and more precisely the method getDownloadURL to generate the URL.
Here is the URL structure: https://firebasestorage.googleapis.com/v0/b/<BUCKET_NAME>/o/<PATH>?alt=media&token=<ACCESS_TOKEN>
https://firebasestorage.googleapis.com/v0/b/bug-attachment.appspot.com/o/Form%20FDA%202253.pdf?alt=media&token=340df09e-8cdf-4b2b-82ef-035baef4dd40
This URL returns the file with alt=media or the metadata in JSON format with alt=json or if the parameter is omitted.
There is my last test. This is a sample with the file from https://pdfgeneratorapi.com/example-documents/52272/pdf.

I write
nameinstead offilename. 🤦♂️

The content of the right file:
{
"name": "Form FDA 2253.pdf",
"bucket": "bug-attachment.appspot.com",
"generation": "1637674089021604",
"metageneration": "1",
"contentType": "application/pdf",
"timeCreated": "2021-11-23T13:28:09.038Z",
"updated": "2021-11-23T13:28:09.038Z",
"storageClass": "STANDARD",
"size": "362554",
"md5Hash": "1TJi0h3UskOAP8LTfey9PQ==",
"contentEncoding": "identity",
"contentDisposition": "inline; filename*=utf-8''Form%20FDA%202253.pdf",
"crc32c": "KQtLNw==",
"etag": "CKSB1rLLrvQCEAE=",
"downloadTokens": "340df09e-8cdf-4b2b-82ef-035baef4dd40"
}
Hi @LefebvreIlyas, @Biokimist
I have good news for you. I have debugged yesterday the issues and it is related simply to the Handlebars package. You are using special signs that are forbidden in HTML, Handlebars convert URL incorrectly. This link should tell you more: https://handlebarsjs.com/guide/#html-escaping
If you use triple brackets so {{{ path }}} instead of {{ path }} in your template and that should sort your problem. I have debugged it yesterday with the link you have sent and it send through with the template without issue.
Please let us know if that sorted your issue.
Hello @posiek07,
Thank you very much, I can confirm that it works properly now! 🚀
Maybe you should add a remark in the README of the extension, and rename the title of the issue so that developers will find it easier if they are confronted with it.
Wow, nice ! I was sending you an email, with the answer about the url. I’m using google cloud storage and get the url that way. You can see attach ts file.
How should i rename the issue ?
Thanks you for your help.
Maybe "Corrupted attachment when using URL in a template (Firebase Storage / Google Cloud Storage, ...)"