Page 1 of 1

Email Attachments

PostPosted: Tue Apr 16, 2024 2:41 pm
by alasdairs
Hi all,

I have just finished setting up emails, but am wondering how I could attach a pdf to the email. I tried using plugins.mail.createTextAttachment(filename, textdata) but it doesn't seem to work. Are you meant to just put the filepath of the file you want to send in the filename? I'm not sure what to put in the textdata field.

Thanks,
Alasdair

Re: Email Attachments

PostPosted: Tue Apr 16, 2024 3:54 pm
by rafig
Try
Code: Select all
var attachment_array = plugins.mail.createBinaryAttachment(attachment_name, attachment);
                  success = plugins.mail.sendMail(send_to,
                     from_email,
                     send_subject,
                     sendHTML,
                     send_cc,
                     send_bcc,
                     [attachment_array]);

Re: Email Attachments

PostPosted: Wed Apr 17, 2024 2:56 pm
by alasdairs
Hi Rafig,

Thanks, that seems to be mostly working but every time I try to open the attachment I have sent it's a broken file.

Here's my code:
Code: Select all
plugins.mail.createTextAttachment(foundset.order_uuid + '.pdf', scopes.global.pdfPath('Customer_Orders') + foundset.order_uuid + '.pdf')


The error message I get is:

The file "file.pdf" could not be opened.
It may be damaged or use a file format that Preview doesn’t recognise.

Thanks,
Alasdair

Re: Email Attachments

PostPosted: Wed Apr 17, 2024 3:16 pm
by mboegem
Hi Alasdair,

you need to use 'createBinaryAttachment' instead of the text variant

Re: Email Attachments

PostPosted: Wed Apr 17, 2024 4:18 pm
by rafig
Alasdair,
as Marc said (& as I put in my code above), use createBinaryAttachment...

Re: Email Attachments

PostPosted: Wed Apr 17, 2024 4:27 pm
by alasdairs
Yeah sorry, didn't read it correctly. It's working now.