Can you send an email with a PDF attachment that is stored in a variable?

I am trying to send a email using a script with a PDF attachment from a variable without much luck. Before I waste too much more time on this I was hoping someone might be able to tell me if it can be done or not.

I have confirmed that the script is able to send a email without the attachment and if I save the PDF attachment to the desktop first that works. But when I save to variable and attach that variable it sens the email without the attachment.

I can be wrong but email attachments have to be files, thing is the contents of a PDF in a variable is not a PDF file. You would need to save the contents of the variable to a file. My guess is if you have FMPA 18, the using the new file script steps could be used to save the PDF to a file.

1 Like

Thank you for the response. I will look into the new File script steps. I am looking for a way to do it that will work on all platforms ideally without more code.

I am curious to learn how come the PDF document is in a variable ?

well I used to set a variable $path get(TemporaryPath) & $FileName & ".pdf"
then save as PDF with that path.

I then could attach to email, open it, etc.

I was hoping to skip the $path and instead just move the PDF into a $file variable. I feel this would be cleaner if it worked. It seems it dose not work so I am going back to the old way.

I don't think FileMaker can produce a file in memory, it always has to be written to the disk at some stage
If you want to avoid that try a plugin, I use MBS in combo with dynapdf for PDF production, than you can write to memory or into a container field with global storage or...
Holger

Variables contain text, no pdf etc.

by defining a path, You reference the document on Your machine and that document can be sent via email (that function gets the file from its location and attaches that to the email)

Theoretically, You can base64 an attachment (so it's coded plain text) - but the receiver needs to decode that - much more complicated

So, referencing a file for using as an attachment is the easiest way - without any 3rd party stuff

Maybe @anon45965781 chimes in with another method...

sorry but no, variables contain the data type you feed them with
I use variables to unzip whole directories into repetitions in memory, one $variable storing 200-300 xml files which get parsed out of the variable via xslt

takes FM to another speed level (thanks to Christians MBS of course)

Holger

1 Like

I did not see Your answer before mine was sent... so the answer was to the op who wants to add a pdf directly into a variable

sure - we are dealing with variables that contain xml-data et all - but you can not put a pdf from Your harddrive without changing anything (binhex, whatever,) into a variable. Yes, we are getting pdf's that are embedded into those xml files - but not in the way that one selects a pdf (not the path) and attaches to an email - and it's our job to make such embedded pdf's visible for the user.. (we also use the MBS..)

with MBS you can even do that, no problem

1 Like

If you use MBS FileMaker Plugin, please check SendMail.AddAttachmentContainer function to create attachment from container without file.

Use SendMail.AddAttachmentText to make attachment from text data or SendMail.AddAttachmentFile for a file on disk.

1 Like

Your old approach is totally fine.

With 18's file functions, this works now without plugin:

Open Data File [ “$sourcepath” ; Target: $sourcefile ]
Read from Data File [ File ID: $sourcefile ; Target: $temp ; Read as: Bytes ]
Close Data File [ File ID: $sourcefile ]

to write it out again, just use:

Create Data File [ “$targetpath” ; Create folders: On ]
Open Data File [ “$targetpath” ; Target: $targetfile ]
Write to Data File [ File ID: $targetfile ; Data source: $temp ; Write as: UTF-8 ]
Close Data File [ File ID: $targetfile ]

if the variable contains a byte-stream, the format parameter (UTF-8) is ignored.
This does not solve the mailing problem though, but any file can be in a variable...

Otmar

2 Likes

good to know, not a solution for my runtimes, but for future projects...

Holger

For FMP-ONLY.....I'm not sure why would you want to send an email from a file variable Wouldn't your PDF be a file in a container or on the disk somewhere?

It's not difficult, however, to use as Markus suggested, to Base64Encode a PDF and send that to, say, wait for it...., yes, a micro-service. On the micro-service side (code you control), it's a one-liner to reconstruct that variable (decode it) back to a byte array. Then you can use whatever API to do whatever you want.

Something like this from a Base64Encode POST Request would do it:
byte[] decodedBodyData = java.util.Base64.getMimeDecoder().decode(bodyData);

Send an attachment from a variable (or from a file path)? No problem-o.

Send multiple attachments in a single email from FMP (FMP 12 or later). Sure.

Let me know if I can help.

Happy Coding! :slight_smile:

1 Like

Just use Base64Encode and you can pass binary data outside FMP. I do that all the time. :slight_smile:

1 Like

Thanks for all the feedback lots of good information there. I try to avoid add-ons by default so will likely not use one for this.

To answer why I would want it in a variable is simply it just seemed like the natural thing to do. I am saving a layout as PDF and then attaching it to an email for sending. There is no reason to keep it longer then I need to.

@ villegld I am still learning the rules on the forums. Was this the correct place to post this question? why exactly will it be deleted?

1 Like

A micro-service isn't really an "add-on". Instead, it's a totally independent application (API) you create, maintain, and share among all your applications that can issue HTTP verbs. A service is free and enjoys all the capabilities of the modern programming language you used. You can run the service anywhere: locally, local file server, or anywhere accessible on the Internet. You can run it from the command line or from an application server. So cool!

I've posted over 100 examples of things I've done with services that FileMaker can not do, does not do well, or does extremely slowly by comparison.

Going forward, if there's something you need to do from FileMaker, like emailing from a variable (no container file required!), a service can do it either directly or after you import a library.

Happy Coding! :slight_smile:

You are referring to Claris Connect right? I know I need to put this higher on my list of things to do but it seems overwhelming. I also expect there to be cost but I am not sure if there is or not.

If I used this situation as an access to learn more about it can you give me a good starting point? Important my use is in Alaska and many of my customers have very limited internet access. All of them run local servers.

Sorry for any confusion. A micro-service is independent of any company or technology other than the language it's written in. The language and micro-service frameworks are all FREE. You just add code and stir.

I have two videos posted on getting started with micro-services right here on this forum.

Check them out and please ask any questions.

Happy Coding! :slight_smile:

2 Likes