Sending Smart Invites via Email

Best practice guide to sending Smart Invites as ICS attachments

Sending Email Invites from your application can be difficult, with our guide you will be up and running in no time.

Just find the section for your programming language for our best practice examples: 

Ruby

For Ruby we recommend that you create an ActionMailer class for example:

class SmartInviteMailer < ApplicationMailer
  def invite(smart_invite)
    event_ics = smart_invite.attachments.icalendar

    attachments.inline["invite.ics"] = {
      content_type: "text/calendar; charset=UTF-8; method=REQUEST",
      encoding: '7bit',
      content: event_ics,
    }

    attachments["invite.ics"] = {
      content_type: "application/ics; charset=UTF-8; method=REQUEST",
      encoding: 'base64',
      content: Base64.encode64(event_ics),
    }

    mail(
      to: smart_invite.recipient.email,
      subject: "Smart Invite Example"
    )
  end
end

This can then be sent in the same way as any other ActionMailer using the deliver_now method. 

PHP

For PHP we recommend the https://github.com/PHPMailer/PHPMailer script is used as this wraps up most of the complexity for you. 

require '../vendor/autoload.php';

$mail = new PHPMailer();

$mail->setFrom('application@example.com');
$mail->addAddress($invite->recipient->email);
$mail->Subject = 'Smart Invite Example';
$mail->Body = 'Example message body';

$ics = $invite->attachments->icalendar;
$contentType = 'application/ics; charset=UTF-8; method=REQUEST';

$mail->addAttachment($ics, 'invite.ics', '7bit', $contentType, 'inline');
$mail->addAttachment(base64_encode($ics), 'invite.ics', 'base64', $contentType, 'attachment');

$mail->send();

Node.js

For Node.js we recommend the https://nodemailer.com/about/ library is used.

const nodemailer = require('../lib/nodemailer');

// TODO configure transport as required
let transporter = nodemailer.createTransport({});

let ics = invite.attachments.icalendar;
let contentType = 'application/ics; charset=UTF-8; method=REQUEST';

let message = {
    to: invite.recipient.email,
    subject: 'Smart Invite Example',
    text: 'Example message body',
    attachments: [
        {
            filename: 'invite.ics',
            content: ics,
            contentType: contentType,
            contentDisposition: 'inline'
        },
        {
            filename: 'invite.ics',
            content: Buffer.from(ics, 'base64'),
            contentType: contentType,
            encoding: 'base64'
        },
    ]
};

transporter.sendMail(message, (error, info) => {
    transporter.close();
}); 

CSharp

For CSharp its recommended that you use the built in MailMessage class.

 var message = new MailMessage("application@example.com", invite.Recipient.Email);
message.Subject = "Smart Invite Example";
message.Body = "Example message body";

var ics = invite.Attachments.ICalendar;

var inlineAttachment = new Attachment(
    new MemoryStream(Encoding.UTF8.GetBytes(ics)),
    "invite.ics",
    "application/ics; charset=UTF-8; method=REQUEST"
);
inlineAttachment.ContentDisposition.Inline = true;

var attachment = new Attachment(
    new MemoryStream(Encoding.UTF8.GetBytes(ics)),
    "invite.ics",
    "application/ics; charset=UTF-8; method=REQUEST"
);

message.Attachments.Add(inlineAttachment);
message.Attachments.Add(attachment);