feat(dolist): send email from API
Co-authored-by: mfo <mfo@users.noreply.github.com>
This commit is contained in:
parent
7c872be29c
commit
41c196f2ec
1 changed files with 76 additions and 8 deletions
|
@ -3,6 +3,10 @@ class Dolist::API
|
||||||
EMAIL_LOGS_URL = "https://apiv9.dolist.net/v1/statistics/email/sendings/transactional/search?AccountID=%{account_id}"
|
EMAIL_LOGS_URL = "https://apiv9.dolist.net/v1/statistics/email/sendings/transactional/search?AccountID=%{account_id}"
|
||||||
EMAIL_KEY = 7
|
EMAIL_KEY = 7
|
||||||
DOLIST_WEB_DASHBOARD = "https://campaign.dolist.net/#/%{account_id}/contacts/%{contact_id}/sendings"
|
DOLIST_WEB_DASHBOARD = "https://campaign.dolist.net/#/%{account_id}/contacts/%{contact_id}/sendings"
|
||||||
|
EMAIL_MESSAGES_ADRESSES_REPLIES = "https://apiv9.dolist.net/v1/email/messages/addresses/replies?AccountID=%{account_id}"
|
||||||
|
EMAIL_MESSAGES_ADRESSES_PACKSENDERS = "https://apiv9.dolist.net/v1/email/messages/addresses/packsenders?AccountID=%{account_id}"
|
||||||
|
EMAIL_SENDING_TRANSACTIONAL = "https://apiv9.dolist.net/v1/email/sendings/transactional?AccountID=%{account_id}"
|
||||||
|
EMAIL_SENDING_TRANSACTIONAL_SEARCH = "https://apiv9.dolist.net/v1/email/sendings/transactional/search?AccountID=%{account_id}"
|
||||||
|
|
||||||
class_attribute :limit_remaining, :limit_reset_at
|
class_attribute :limit_remaining, :limit_reset_at
|
||||||
|
|
||||||
|
@ -29,6 +33,40 @@ class Dolist::API
|
||||||
client_key.present?
|
client_key.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_email(mail)
|
||||||
|
url = format(EMAIL_SENDING_TRANSACTIONAL, account_id: account_id)
|
||||||
|
|
||||||
|
body = {
|
||||||
|
"TransactionalSending": {
|
||||||
|
"Type": "TransactionalService",
|
||||||
|
"Contact": {
|
||||||
|
"FieldList": [
|
||||||
|
{
|
||||||
|
"ID": EMAIL_KEY,
|
||||||
|
"Value": mail.to.first
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Message": {
|
||||||
|
"Name": mail['X-Dolist-Message-Name'].value,
|
||||||
|
"Subject": mail.subject,
|
||||||
|
"SenderID": sender_id,
|
||||||
|
"ForceHttp": true,
|
||||||
|
"Format": "html",
|
||||||
|
"DisableOpenTracking": true,
|
||||||
|
"IsTrackingValidated": true
|
||||||
|
},
|
||||||
|
"MessageContent": {
|
||||||
|
"SourceCode": mail_source_code(mail),
|
||||||
|
"EncodingType": "UTF8",
|
||||||
|
"EnableTrackingDetection": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post(url, body.to_json)
|
||||||
|
end
|
||||||
|
|
||||||
def sent_mails(email_address)
|
def sent_mails(email_address)
|
||||||
contact_id = fetch_contact_id(email_address)
|
contact_id = fetch_contact_id(email_address)
|
||||||
if contact_id.nil?
|
if contact_id.nil?
|
||||||
|
@ -44,8 +82,38 @@ class Dolist::API
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def senders
|
||||||
|
url = format(EMAIL_MESSAGES_ADRESSES_PACKSENDERS, account_id: account_id)
|
||||||
|
get(url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def replies
|
||||||
|
url = format(EMAIL_MESSAGES_ADRESSES_REPLIES, account_id: account_id)
|
||||||
|
get(url)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def sender_id
|
||||||
|
senders.dig("ItemList", 0, "Sender", "ID")
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(url)
|
||||||
|
response = Typhoeus.get(url, headers:).tap do
|
||||||
|
self.class.save_rate_limit_headers(_1.headers)
|
||||||
|
end
|
||||||
|
|
||||||
|
JSON.parse(response.response_body)
|
||||||
|
end
|
||||||
|
|
||||||
|
def post(url, body)
|
||||||
|
response = Typhoeus.post(url, body:, headers:).tap do
|
||||||
|
self.class.save_rate_limit_headers(_1.headers)
|
||||||
|
end
|
||||||
|
|
||||||
|
JSON.parse(response.response_body)
|
||||||
|
end
|
||||||
|
|
||||||
def headers
|
def headers
|
||||||
{
|
{
|
||||||
"Content-Type": 'application/json',
|
"Content-Type": 'application/json',
|
||||||
|
@ -82,14 +150,6 @@ class Dolist::API
|
||||||
post(url, body)["ItemList"]
|
post(url, body)["ItemList"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def post(url, body)
|
|
||||||
response = Typhoeus.post(url, body:, headers:).tap do
|
|
||||||
self.class.save_rate_limit_headers(_1.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
JSON.parse(response.response_body)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_sent_mail(email_address, contact_id, dolist_message)
|
def to_sent_mail(email_address, contact_id, dolist_message)
|
||||||
SentMail.new(
|
SentMail.new(
|
||||||
from: ENV['DOLIST_NO_REPLY_EMAIL'],
|
from: ENV['DOLIST_NO_REPLY_EMAIL'],
|
||||||
|
@ -112,4 +172,12 @@ class Dolist::API
|
||||||
status
|
status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mail_source_code(mail)
|
||||||
|
if mail.html_part.nil? && mail.text_part.nil?
|
||||||
|
mail.decoded
|
||||||
|
else
|
||||||
|
mail.html_part.body.decoded
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue