Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email with attachment using Javascript for Automation

I would like use Javascript for Automation in OS X Yosemite to create a new email message in Mail.app and attach a file to the email. This is my code:

Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "[email protected]" }))
message.subject = "Test subject"
message.content = "Lorem ipsum dolor sit"

It works fine until this point. I see a new message window with the recipient, subject, and body text correctly filled in. But I can’t figure out how to add a file attachment to the message. The scripting dictionary for Mail.app indicates that the contents property (an instance of RichText) can contain attachments, but I don’t know how to add one.

I tried this but I get an error:

// This doesn't work.
attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.content.attachments = [ attachment ]
// Error: Can't convert types.

I found several examples on the web how to do this in AppleScript, for example this one:

tell application "Mail"
    ...
    set theAttachmentFile to "Macintosh HD:Users:moligaloo:Downloads:attachment.pdf"
    set msg to make new outgoing message with properties {subject: theSubject, content: theContent, visible:true}

    tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
end tell

But I can’t figure out how to convert this to Javascript.

like image 470
Ole Begemann Avatar asked Jan 02 '15 17:01

Ole Begemann


People also ask

How do I send automatic emails in JavaScript?

js, right click on the index. html file > open in the browser. Once the browser loads the page, click on the button Send Email. That's it.. the mail is sent successfully.

How can you send an email using a JavaScript function?

You can't send emails using JavaScript code alone due to the lack of support for server sockets. For this, you need a server-side language that talks to the SMTP server. You can use JS in conjunction with a server script that will send emails from the browser based on your requests.

Can email run JavaScript?

The viewing technology of a typical email client isn't as up-to-date as a web browser. Web browsers display interactive, dynamic content, and they update often. But interactive elements like Flash, JavaScript, or HTML forms won't work in most email inboxes.

How do you send an attachment in EmailJS?

Adding file attachments is possible in two ways – by passing the attachment content programmatically through the emailjs. send() call, or by allowing the user to upload a file. The latter is done by creating a form with a file input field, and sending the email via emailjs. sendForm().


2 Answers

I figured out a way to do this through trial and error, you need to use message.attachments.push(attachment) instead of attachments = [...]

Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "[email protected]" }))
message.subject = "Testing JXA"
message.content = "Foo bar baz"

attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.attachments.push(attachment)
like image 146
tlehman Avatar answered Sep 30 '22 17:09

tlehman


Building on the answer above by @tlehman, I found that his solution worked very well, except for one thing: The attachments were NOT successful. No errors, no messages, the attachment just was not made to the message. The solution is to use the Path() method.

Here is his solution + the Path() method, which is needed if there are any spaces in the path:

Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "[email protected]" }))
message.subject = "Testing JXA"
message.content = "Foo bar baz"

attachment = Mail.Attachment({ fileName: Path("/Users/myname/Desktop/if the file has spaces in it test.pdf") })
message.attachments.push(attachment)
like image 44
JMichaelTX Avatar answered Sep 30 '22 17:09

JMichaelTX