I've got problem with sending calendar event that is recognized by Outlook and iOS mail app as calendar event and not ordinary email.
I'm using JavaScript in node.js env. To send email I'm using mailgun and js library mailgun-js
. I'm creating ics file and attaching it to the email.
const mailgun = require('mailgun-js')({apiKey: mailgunApiKey, domain: mailgunDomain})
const candidateEmailBody = {
from: `${companyName} <${EMAIL_FROM}>`,
to: email,
subject: companyName + ' - interview',
html: 'Html message',
attachment: [invite]
}
mailgun.messages().send(candidateEmailBody, function (error, body) {
if (error) {
console.log(error)
}
})
The invite
object is created by ics
lib and wrapped in mailgun attachement by the function below:
const prepareIcsInvite = function (startDate, companyName, firstname, lastname, email, intFirstname, intLastname, intEmail) {
const st = new Date(startDate)
const meetingEvent = {
start: [st.getFullYear(), st.getMonth() + 1, st.getDate(), st.getHours(), st.getMinutes()],
end: [st.getFullYear(), st.getMonth() + 1, st.getDate(), st.getHours()+1, st.getMinutes()],
title: companyName + ' - Interview',
description: 'description',
location: 'location',
status: 'CONFIRMED',
productId: 'myproduct',
organizer: {name: 'Admin', email: 'admin@example.com'},
attendees: [
{name: firstname + ' ' + lastname, email: email},
{name: intFirstname + ' ' + intLastname, email: intEmail}
]
}
const icsFile = ics.createEvent(meetingEvent)
const fileData = new Buffer(icsFile.value)
const invite = new mailgun.Attachment(
{
data: fileData,
filename: 'Meeting Invite.ics',
contentType: 'text/calendar'
})
console.log('ICS meeting invite created')
return invite
}
Email prepared that way is sent via mailgun API and GMail correctly recognizes it as meeting invitation:
However, other email clients (iOS, Outlook) don't recognize that this is calendar event invitation and just display it as ordinary email, with a file attachment.
What should I do to make this message Outlook and iOS compatible?
Outlook (and I believe iOS as well) use "alternatives" to store the invite.
This GitHub issue outlines how to use a MIME library to build the event message: https://github.com/bojand/mailgun-js/issues/44. You should be able to use the same code flow outlined in the issue to build your message. You will need to use the string value returned from ics.createEvent for the 'addAlternative" call.
Mailcomposer is the MIME library referenced in the Mailgun docs (https://documentation.mailgun.com/en/latest/api-sending.html#examples).