fix(leadmail): extract from-display via addrs.first.display_name; log payload from
Some checks failed
Lock Threads / action (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled

Previous extract_from used message[:from].display_names.first. That
method exists on Mail::Field but its behavior across Mail gem versions
is inconsistent — it sometimes returns a string, sometimes blank. The
canonical Mail gem API for getting the display name of the first
address in a multi-address field is addrs.first.display_name, which
returns the parsed display name reliably.

Without a working display name extraction, our LeadMail payload only
included {email: ...} without the name, so emails arrived showing only
the bare 'dev@leadmagnet.fi' instead of 'Niklas Pull from Lead Magnet
Dev <dev@leadmagnet.fi>'.

Also include the from payload (email + name) in the success log line so
future from-header issues are immediately visible without needing to
check LeadMail's dashboard.
This commit is contained in:
netlas
2026-04-28 13:25:00 +03:00
parent c1c05429a8
commit a134d59bbb

View File

@@ -15,7 +15,10 @@ class LeadmailDelivery
response_data = JSON.parse(response.body)
message.header['X-LeadMail-Log-ID'] = response_data.dig('data', 'log_id')
Rails.logger.info("Email sent via LeadMail: log_id=#{response_data.dig('data', 'log_id')}, to=#{message.to.inspect}")
Rails.logger.info(
"Email sent via LeadMail: log_id=#{response_data.dig('data', 'log_id')} " \
"from=#{payload[:from].inspect} to=#{message.to.inspect}"
)
response
rescue StandardError => e
Rails.logger.error("LeadMail delivery failed: #{e.message}\nPayload: #{payload.inspect}")
@@ -51,8 +54,9 @@ class LeadmailDelivery
end
def extract_from(message)
from_field = message[:from]
from_address = message.from&.first
from_display = message[:from]&.display_names&.first
from_display = from_field&.addrs&.first&.display_name
{
email: from_address,