Amazon SES là một dịch vụ rất quen thuộc đối với chúng ta khi gửi email marketing. Đối với công việc gửi thư thì đôi khi yêu cầu ta cần gửi tệp đính kèm trong email. Tuy nhiên, khi mình mới làm việc với yêu cầu này lần đầu thì mình thấy rằng doc của Amazone chưa đủ công cụ để có thể giúp mình có thể đọc doc và hoàn thành luôn, ngay cả khi tìm kiếm vấn đề này mình cũng . Vì vậy, mình sẽ viết phần giới thiệu này cho những ai tiếp xúc với vấn đề này lần đầu tiên có thể nhanh chóng nắm bắt được cách gửi thư có thêm tệp đính kèm đối với email SES
Cài đặt
Để có thể sử dụng SES thì ta cần có
resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
6 và resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
7 , để có thể lấy chúng, thì bạn có thể tham khảo những bài viết về hướng dẫn gửi mail trong Amazone SES nhé, trên Viblo đã có khá nhiều rồi. Ta could start as after with 2 key onclient = Aws::SES::Client.new {
access_key_id: "********************************",
secret_access_key: "********************************",
region: "us-west-2"
}
Gửi thư
Ta sẽ dùng
resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
0 để gửi thư, cùng xem cú pháp và ví dụ cụ thể trong tài liệu của Amazone như sau. cú phápresp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
Ví dụ
# The following example sends an email with an attachment:
resp = client.send_raw_email[{
destinations: [
],
from_arn: "",
raw_message: {
data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--",
},
return_path_arn: "",
source: "",
source_arn: "",
}]
# resp.to_h outputs the following:
{
message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000",
}
in which
1. Nhận định dạng của địa chỉ email. Nếu không đặt giá trị này thì bắt buộc ta phải đặt giá trị địa chỉ củaresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
2 trong phần dữ liệu củaresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
3[Bạn có thể đặt cả 2 phần này. ] Nếu thiết lập giá trị choresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
4 và bật chức năng chuyển tiếp phản hồi thì khi phát sinh những mailresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
5 hoặcresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
6 sẽ được gửi về địa chỉ mail này. Việc này được ưu tiên hơn cả việc bạn đặt giá trị choresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
7 in Header in raw textresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
8. Danh sách địa chỉ nhận thư, bao gồm cả thưresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
9 vàresp = client.send_raw_email[{ source: "Address", destinations: ["Address"], raw_message: { # required data: "data", # required }, from_arn: "AmazonResourceName", source_arn: "AmazonResourceName", return_path_arn: "AmazonResourceName", tags: [ { name: "MessageTagName", # required value: "MessageTagValue", # required }, ], configuration_set_name: "ConfigurationSetName", }]
0# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
1. Dưới dạng văn bản thô của email, chúng ta cần đảm bảo những thông tin sau# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
- Thư phải có phần đầu và phần nội dung, ngăn cách nhau bởi dòng trắng
- Tất cả các trường yêu cầu của Header bắt buộc phải có. 3. Mỗi phần của
2 phải được định dạng đúng. 4. Loại nội dung MIME phải nằm trong số những loại được Amazone SES hỗ trợ, xem thêm tại. Hướng dẫn dành cho nhà phát triển Amazon SES. 5. You must at base64 code code. 6. Mỗi RFC 5321, chiều dài tối đa của mỗi dòng bao gồm cả# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
3 là 1000# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
- ________ 34 Tham số dùng để gửi ________ 35. Thay vì sử dụng tham số này, ta có thể sử dụng
6 trong phần tin nhắn thô. Nếu sử dụng cả ________ 37 và _________ 36 thì Amazon SES sẽ sử dụng giá trị từ# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
7.# The following example sends an email with an attachment: resp = client.send_raw_email[{ destinations: [ ], from_arn: "", raw_message: { data: "From: sender@example.com\\nTo: recipient@example.com\\nSubject: Test email [contains an attachment]\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"attachment.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--", }, return_path_arn: "", source: "", source_arn: "", }] # resp.to_h outputs the following: { message_id: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000", }
Áp dụng
Phần raw_message là phần quan trọng nhất để có thể gửi thư có tệp đính kèm, tuy nhiên nếu đọc ví dụ của SES mà bạn không thể tự viết được nội dung cho phần raw_message thì hãy tham khảo đoạn mã sau của mình
resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
3Lưu ý rằng nội dung của
resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
30 phải được mã hóa dưới dạng Base64 và các thông tin cũng như nội dung của tệp đính kèm cũng phải đầy đủ như thông tin trong hàm resp = client.send_raw_email[{
source: "Address",
destinations: ["Address"],
raw_message: { # required
data: "data", # required
},
from_arn: "AmazonResourceName",
source_arn: "AmazonResourceName",
return_path_arn: "AmazonResourceName",
tags: [
{
name: "MessageTagName", # required
value: "MessageTagValue", # required
},
],
configuration_set_name: "ConfigurationSetName",
}]
31