diff --git a/app/controllers/manager/users_controller.rb b/app/controllers/manager/users_controller.rb index 0dd325515..2e2698e7a 100644 --- a/app/controllers/manager/users_controller.rb +++ b/app/controllers/manager/users_controller.rb @@ -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 diff --git a/app/lib/mailjet/api.rb b/app/lib/mailjet/api.rb new file mode 100644 index 000000000..e97845cdf --- /dev/null +++ b/app/lib/mailjet/api.rb @@ -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 diff --git a/app/views/manager/users/emails.html.erb b/app/views/manager/users/emails.html.erb index 7e528e00f..7224bd62f 100644 --- a/app/views/manager/users/emails.html.erb +++ b/app/views/manager/users/emails.html.erb @@ -20,7 +20,11 @@

Historique des email

<% if @sent_mails.present? %>

- Cet historique contient les 30 derniers jours. Pour un recherche plus fine, il faut fouiller les logs de SendInblue ou de Mailjet. + Cet historique contient les 30 derniers jours. + Pour un recherche plus fine, il faut fouiller les + logs de SendInblue + ou + de Mailjet.

@@ -66,7 +70,11 @@
<% else %>

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 logs de SendInBlue ou de Mailjet.

+ Vous pouvez éventuellement fouiller les + logs de SendInBlue + ou + de Mailjet. +

<% end %>

Problèmes potentiel