Hi everyone,
I’m experiencing an issue with embedding an image in the email body using the code provided below. Despite my efforts, the image does not appear in the email when it is sent.
Here’s a summary of the relevant code:
- Email html.erb Template:
Wycena zamówienia
<!DOCTYPE html>
<html>
<body style="background-color: #ffffff; color: #433d43; font-family: Arial, sans-serif; margin: 0; padding: 0;">
<div style="background-color: #ffffff; color: #433d43; padding: 2em; width: 50%; margin: 0 auto; text-align: center; font-size: 1rem;">
LOGO
<hr style="border: 0; border-top: 1px solid #919497; margin-top: 1em;" />
<div style="color: #433d43; font-weight: bold; margin-top: 1em;">
<div>Request for Quote:</div>
<div><a href="#{url}" style="color: #6bb2c9; text-decoration: none;"><b>#{seller.name}</b></a></div>
</div>
<hr style="border: 0; border-top: 1px solid #919497; margin-top: 1em;" />
<div style="color: #433d43;; margin-top: 1em;">
Your team #{config.product_name}
</div>
</div>
</body>
</html>
- Ruby Code for Sending Email:
require 'open-uri'
require 'base64'
class NotificationFactory::MailerExtension::Seller < NotificationFactory::Mailer
def self.seller_notification(data)
# get subject
result = NotificationFactory::Mailer.template(
template: data[:template],
locale: 'en-us',
objects: data[:objects],
standalone: data[:standalone],
)
# rebuild subject
if data[:main_object].respond_to?(:subject_build)
result[:subject] = data[:main_object].subject_build(result[:subject])
end
image_url = 'http://zammad/api/v1/system_assets/product_logo/17188780'
image_data = URI.open(image_url).read
# Encode the image in Base64
encoded_image = Base64.strict_encode64(image_data)
image_tag = "<img alt='Product Logo' src='data:image/png;base64,#{encoded_image}' />"
Rails.logger.info "XXX #{encoded_image}"
# Embed the image in the email body by replacing a placeholder
result[:body] = result[:body].gsub('LOGO', image_tag)
NotificationFactory::Mailer.deliver(
recipient: data[:seller],
subject: result[:subject],
body: result[:body],
content_type: 'text/html',
message_id: data[:message_id],
references: data[:references],
attachments: data[:attachments],
)
end
end
Issue:
The LOGO
placeholder in the HTML email template is not being replaced with the actual image. The image appears to be properly encoded in Base64, but it does not show up in the email body.
I would appreciate any guidance on why this might be happening and how I can resolve it.
Thank you!