manager: add Mailjet emails to the sent emails list

This commit is contained in:
Pierre de La Morinerie 2021-04-13 08:57:26 +00:00
parent 773c18babf
commit 16f695031b
3 changed files with 59 additions and 3 deletions

View file

@ -49,7 +49,18 @@ module Manager
def emails
@user = User.find(params[:id])
@sent_mails = Sendinblue::API.new.sent_mails(@user.email)
email_services = [
Mailjet::API.new,
Sendinblue::API.new
]
@sent_mails = email_services
.filter(&:properly_configured?)
.map { |api| api.sent_mails(@user.email) }
.flatten
.sort_by(&:delivered_at)
.reverse
end
def unblock_email

37
app/lib/mailjet/api.rb Normal file
View file

@ -0,0 +1,37 @@
class Mailjet::API
def properly_configured?
[Mailjet.config.api_key, Mailjet.config.secret_key].all?(&:present?)
end
# Get messages sent to a user through SendInBlue.
#
# Returns an array of SentMail objects.
def sent_mails(email_address)
contact = Mailjet::Contact.find(email_address)
if contact.nil?
Rails.logger.info "Mailjet::API: no contact found for email address '#{email_address}'"
return []
end
messages = Mailjet::Message.all(
contact: contact.attributes['id'],
from_ts: 30.days.ago.to_datetime.rfc3339,
show_subject: true
)
messages.map do |message|
SentMail.new(
from: nil,
to: email_address,
subject: message.attributes['subject'],
delivered_at: message.attributes['arrived_at'],
status: message.attributes['status'],
service_name: 'Mailjet',
external_url: 'https://app.mailjet.com/contacts/subscribers/contact_list'
)
end
rescue Mailjet::ApiError => e
Rails.logger.error e.message
[]
end
end

View file

@ -20,7 +20,11 @@
<h2 style="font-size: 1.3em; margin: 24px 0 8px 0">Historique des email</h2>
<% if @sent_mails.present? %>
<p>
Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut fouiller les <a href="https://app-smtp.sendinblue.com/log">logs de SendInblue</a> ou de Mailjet.
Cet historique contient les 30 derniers jours.
Pour un recherche plus fine, il faut fouiller les
<a href="https://app-smtp.sendinblue.com/log">logs de SendInblue</a>
ou
<a href="https://app.mailjet.com/contacts/subscribers/contact_list">de Mailjet</a>.
</p>
<table>
<thead>
@ -66,7 +70,11 @@
</table>
<% else %>
<p>Historique indisponible. Cette adresse email n'existe pas chez nos prestataires d'envoi, ou nous n'avons pas réussi à en charger des données.
Vous pouvez éventuellement fouiller les <a href="https://app-smtp.sendinblue.com/log">logs de SendInBlue</a> ou de Mailjet.</p>
Vous pouvez éventuellement fouiller les
<a href="https://app-smtp.sendinblue.com/log">logs de SendInBlue</a>
ou
<a href="https://app.mailjet.com/contacts/subscribers/contact_list">de Mailjet</a>.
</p>
<% end %>
<h2 style="font-size: 1.3em; margin: 24px 0 8px 0">Problèmes potentiel</h2>