· 7 years ago · Jun 03, 2018, 10:28 AM
1require 'aws-sdk-ses'
2
3def init_mail(key_id, secret_key, region = 'us-east-1')
4 return Aws::SES::Client.new(
5 region: region,
6 access_key_id: key_id,
7 secret_access_key: secret_key
8 )
9end
10
11def send_mail(client, sender, recipient, subject, htmlbody, textbody)
12 begin
13 encoding = 'UTF-8'
14 client.send_email(
15 destination: {
16 to_addresses: [
17 recipient
18 ]
19 },
20 message: {
21 body: {
22 html: {
23 charset: encoding,
24 data: htmlbody
25 },
26 text: {
27 charset: encoding,
28 data: textbody
29 }
30 },
31 subject: {
32 charset: encoding,
33 data: subject
34 }
35 },
36 source: sender,
37 )
38
39 # If something goes wrong, display an error message.
40 rescue Aws::SES::Errors::ServiceError => error
41 puts "Email not sent. Error message: #{error}"
42 end
43end