forked from nylas/nylas-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsending-messages.rb
34 lines (30 loc) · 1.5 KB
/
sending-messages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require_relative '../helpers'
# An executable specification that demonstrates how to use the Nylas Ruby SDK to send messages via the API
# See https://docs.nylas.com/reference#sending
api = Nylas::API.new(app_id: ENV['NYLAS_APP_ID'], app_secret: ENV['NYLAS_APP_SECRET'],
access_token: ENV['NYLAS_ACCESS_TOKEN'])
# Sending a message as a hash
demonstrate do
api.send!(to: [{ email: ENV.fetch('NYLAS_EXAMPLE_EMAIL', '[email protected]'),
name: "An example recipient" }],
subject: "you've got mail!",
body: "It's a really good mail!").to_h
end
# Sending a message by instantiating a message instance
demonstrate do
message = Nylas::NewMessage.new(to: [{ email: ENV.fetch('NYLAS_EXAMPLE_EMAIL', '[email protected]'),
name: "An example recipient" }],
subject: "you've got another mail!",
body: "It's a really good another mail!", api: api)
message.send!.to_h
end
# Sending a message as a mime string
#
demonstrate do
message_string = "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\n" \
"Subject: A mime email\n" \
"From: You <[email protected]>\n" \
"To: You <#{ENV.fetch('NYLAS_EXAMPLE_EMAIL', '[email protected]')}>\n\n" \
"This is the body of the message sent as a raw mime!"
api.send!(message_string)
end