Skip to content

Commit e3a00bd

Browse files
ArtieReusandypf
andauthored
feat(core): integrate Action Mailer with Limes Email API (#1537)
Co-authored-by: Andreas Pfau <[email protected]>
1 parent af24130 commit e3a00bd

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'net/http'
2+
require 'uri'
3+
require 'json'
4+
5+
class CoreApplicationMailer < ActionMailer::Base
6+
layout "mailer"
7+
8+
def send_custom_email(recipient:, subject:, body_html:)
9+
if Rails.configuration.limes_mail_server_endpoint.blank?
10+
Rails.logger.info "Skipping email: limes_mail_server_endpoint is not set."
11+
return
12+
end
13+
14+
# Ensure uri is properly formed
15+
uri = URI.parse(Rails.configuration.limes_mail_server_endpoint)
16+
uri.query = URI.encode_www_form({ from: 'elektra' })
17+
18+
# Get the token form the mail_cloud_admin instance
19+
token = mail_cloud_admin.instance_variable_get(:@api_client).token
20+
21+
# Set up the body for the request
22+
body = {
23+
recipients: Array(recipient),
24+
subject: subject,
25+
mime_type: 'text/html',
26+
mail_text: body_html
27+
}.to_json
28+
29+
# Set up headers
30+
headers = {
31+
'Content-Type' => 'application/json',
32+
'X-Auth-Token' => token
33+
}
34+
35+
# Create http client
36+
http = Net::HTTP.new(uri.host, uri.port)
37+
http.use_ssl = true
38+
# Create the HTTP request
39+
request = Net::HTTP::Post.new(uri, headers)
40+
request.body = body
41+
42+
# Make the request and handle the response
43+
response = http.request(request)
44+
45+
Rails.logger.info "Email API Response: #{response.code} - #{response.body}"
46+
end
47+
48+
private
49+
50+
def mail_cloud_admin
51+
@mail_cloud_admin ||=
52+
Core::ServiceLayer::ServicesManager.new(
53+
Core::ApiClientManager.cloud_admin_api_client,
54+
)
55+
end
56+
57+
end

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def self.parent_name
134134
end
135135

136136
# Mailer configuration for inquiries/requests
137+
config.limes_mail_server_endpoint = ENV["LIMES_MAIL_SERVER_API_ENDPOINT"]
137138
config.action_mailer.raise_delivery_errors = true
138139
config.action_mailer.delivery_method = :smtp
139140
config.action_mailer.smtp_settings = {

config/environments/development.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,9 @@
9696

9797
# Uncomment if you wish to allow Action Cable access from any origin.
9898
# config.action_cable.disable_request_forgery_protection = true
99+
100+
101+
# Uncomment this line when testing email service
102+
# When generating URLs (like admin_inquiries_url) from a background job or a mailer, you need to tell Rails what host to use from rails c.
103+
# config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
99104
end

plugins/inquiry/app/mailers/inquiry/inquiry_mailer.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Inquiry
2-
class InquiryMailer < ApplicationMailer
2+
class InquiryMailer < ::CoreApplicationMailer
3+
34
def notification_email_requester(
45
user_email,
56
user_full_name,
@@ -11,11 +12,12 @@ def notification_email_requester(
1112
@user_full_name = user_full_name
1213
@inquiry_step = inquiry_step
1314
@inquiry = inquiry
14-
mail(
15-
to: @user_email,
16-
subject:
17-
"Converged Cloud: Your resource request is in state: #{@inquiry.aasm.human_state}",
18-
content_type: "text/html",
15+
16+
email_body = render_to_string('inquiry/inquiry_mailer/notification_email_requester', layout: false)
17+
send_custom_email(
18+
recipient: @user_email,
19+
subject: "Converged Cloud: Your resource request is in state: #{@inquiry.aasm.human_state}",
20+
body_html: email_body
1921
)
2022
end
2123

@@ -39,8 +41,14 @@ def notification_email_processors(
3941
subject += "/#{@inquiry.tags["domain_name"]}"
4042
end
4143
end
44+
4245
# this is called from the model, first try with all emails at once, if a error occurs, try to send each email separately
43-
mail(to: processor_emails, subject: subject, content_type: "text/html")
46+
email_body = render_to_string('inquiry/inquiry_mailer/notification_email_processors', layout: false)
47+
send_custom_email(
48+
recipient: processor_emails,
49+
subject: subject,
50+
body_html: email_body
51+
)
4452
end
4553

4654
def notification_email_additional_recipients(
@@ -64,8 +72,14 @@ def notification_email_additional_recipients(
6472
subject += "/#{@inquiry.tags["domain_name"]}"
6573
end
6674
end
75+
6776
# this is called from the model, first try with all emails at once, if a error occurs, try to send each email separately
68-
mail(to: receiver_emails, subject: subject, content_type: "text/html")
77+
email_body = render_to_string('inquiry/inquiry_mailer/notification_email_additional_recipients', layout: false)
78+
send_custom_email(
79+
recipient: receiver_emails,
80+
subject: subject,
81+
body_html: email_body
82+
)
6983
end
7084

7185
def notification_new_project(inform_dl, inquiry, user_full_name)
@@ -81,7 +95,15 @@ def notification_new_project(inform_dl, inquiry, user_full_name)
8195
subject += "/#{@inquiry.tags["domain_name"]}"
8296
end
8397
end
84-
mail(to: inform_dl, subject: subject, content_type: "text/html")
98+
99+
# Render the email body content
100+
email_body = render_to_string('inquiry/inquiry_mailer/notification_new_project', layout: false)
101+
send_custom_email(
102+
recipient: inform_dl,
103+
subject: subject,
104+
body_html: email_body
105+
)
85106
end
107+
86108
end
87109
end

0 commit comments

Comments
 (0)