This guide assumes you've already done the following:
See email model in the reference.
Sending an email via the WorkAPI works in much the same way as sending via any other platform. As with the creation of any item via the WorkAPI, an `integraiton_id` is required.
NOTE:
The response for this endpoint is 202 Accepted. There is no guarantee that the email will have been sent by the underlying provider, but on receiving a 202 Accepted response, the email is guaranteed to have been submitted by WorkAPI to the underlying provider's API.
token = "<user's token>"
client = LivilApi::Service.new(token)
# using attribute writers
email = LivilApi::Email.new
email.integration_id = 'abc17171923def19098'
email.subject = 'This is a test email'
email.to_recpients << LivilApi::EmailAddress.new(address: 'test@livil.co')
email.body = LivilApi::EmailBody.new(plain_text: ['this is a plan text part', 'and another'], html: ['<b>this</b> is <i>HTML!</i>'])
# alternatively, using keyword args on initialization
email = LivilApi::Email.new(
integration_id: 'abc17171923def19098',
subject: 'This is a test email',
to_recpients: [{ address: 'test@livil.co' }],
body:{
plain_text: ['this is a plan text part', 'and another'],
html: ['<b>this</b> is <i>HTML!</i>']
}
)
response = client.send_email(email: email)
response
# => :accepted
Previously saved drafts can also be sent by simply including the ID of the draft in an otherwise blank email object.
If additional attributes are included on the email object, the saved draft contents will be entirely replaced on sending.
token = "<user's token>"
client = LivilApi::Service.new(token)
draft_email_id = 'ABCD....'
email = LivilApi::Email.new(id: draft_email_id)
response = client.send_email(email: email)
response
# => :accepted
Files can be attached to emails as usual. In this first example, they are transmitted to the Work-API server directly in the email POST
request.
my_file = File.open('/path/to/file.txt')
attachment = LivilApi::EmailAttachment.new
attachment.attach('a-file-name.txt', my_file)
email = LivilApi::Email.new(
integration_id: 'abc17171923def19098',
subject: 'This is an email with an attachment',
to_recpients: [{ address: 'test@livil.co' }],
body:{
plain_text: ['the file you requested is attached!'],
},
attachments: [attachment]
)
response = client.send_email(email: email)
response
# => :accepted
Attachments may also come from other sources within the WorkAPI, such as a Google Drive or OneDrive account.
This section will be updated when the file integration is in place.
my_file = service.get_file(file_id: 'abcd123') # TODO: confirm syntax
my_file # => LivilApi::File(remote_id: 'abcd123', integration_id: '12377abcd72f', filename: 'something.doc')
attachment = LivilApi::EmailAttachment.new
attachment.attach('a-file-name.txt', my_file)
email = LivilApi::Email.new(
integration_id: 'abc17171923def19098',
subject: 'This is an email with an attachment',
to_recpients: [{ address: 'test@livil.co' }],
body:{
plain_text: ['the file you requested is attached!'],
},
attachments: [attachment]
)
response = client.send_email(email: email)
response
# => :accepted