Metadata appears blank in postmark response to rails - ruby-on-rails

I have successfully setup the postmark API in my rails dev environment and am trying to embed metadata containing record ID so I can process the record in my webhook. I receive a response from postmark into my webhook URL upon email delivery, but the application log shows the metadata blank. I have tried entering the metadata in hash notation as well but to no avail:
Processing by WebhooksController#create as HTML
Parameters: {"MessageID"=>"b0c2673e-9628-4725-b9bb-xxxxxxxxxxxx", "Recipient"=>"nnnn#eee.ddd", "DeliveredAt"=>"2022-02-13T08:46:30Z",
"Details"=>"smtp;250 OK id=1nJAWV-001Lmg-Eu", "Tag"=>"", "ServerID"=>nnnnnnnnnn, "Metadata"=>{}, "RecordType"=>"Delivery",
"MessageStream"=>"outbound", "token"=>"xxxxxxxxxxxxx", "webhook"=>{"MessageID"=>"b0c2673e-9628-4725-b9bb-xxxxxxxxxxxx",
"Recipient"=>"nnnn#eee.ddd", "DeliveredAt"=>"2022-02-13T08:46:30Z", "Details"=>"smtp;250 OK id=1nJAWV-001Lmg-Eu", "Tag"=>"",
"ServerID"=>nnnnnnnnnn, "Metadata"=>{}, "RecordType"=>"Delivery", "MessageStream"=>"outbound"}}
My mail block is as follows:
mail(
from: #invoice.account.company_email,
to: #invoice.email,
cc: #invoice.also_email,
subject: "Invoice %s has been generated." % [#invoice.to_s],
metadata: { "invoice_id": "bd2533a8-c00b-4830-97dc-xxxxxxxxxx" },
message_stream: 'outbound',
body: email_body
) do |format|
format.html {email_body}
end
As can be seen, I'm presently hard coding the id into the metadata while I try to solve this issue.
Interestingly, when I use the Postmark test stream, and view the raw data of the email, the metadata is showing (excerpt only):
metadata: {:invoice_id=>"bd2533a8-c00b-4830-97dc-xxxxxxxxxx"}
Feedback-ID: snnnnnn-_:snnnnnn:a22nnnn:postmark
X-Complaints-To: abuse#postmarkapp.com
X-Job: 227735_860nnnn
In addition to the metadata issue, I am receiving only delivery responses. I have open tracking enabled at the server level but aren't receiving any.
Any help would be greatly appreciated and thanks in advance. Oh, and I'm using the postmark-rails gem in rails in conjunction with actionmailer.

Issue has been resolved. The metadata should be inserted outside the mail block as an array:
https://github.com/wildbit/postmark-rails/wiki/Email-sending

Related

The application goes offline when trying to send more than thousands of emails in Rails with AWS SES

I have implemented a platform using rails, and the goal is to send thousands of emails to customers with one click. The concept is that an email array runs each loop and inside each loop runs send email functionality like below.
#emails = ['abc#gmai.com', 'abc#example.com'] # More than 3 thousands
#emails.each do |email|
aws_email_sender(email, #email_subject, #email_body_html)
end
And the email function is like below:
def aws_email_sender(recipient, subject, htmlbody)
sender = "hello#example.com"
awsregion = "ap-west-1"
# The HTML body of the email.
htmlbodycontent = "#{htmlbody}"
# The email body for recipients with non-HTML email clients.
textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby."
# Specify the text encoding scheme.
encoding = "UTF-8"
# Create a new SES resource and specify a region
ses = Aws::SES::Client.new(region: awsregion)
# Try to send the email.
begin
# Provide the contents of the email.
resp = ses.send_email({
destination: {
to_addresses: [recipient]
},
message: {
body: {
html: {
charset: encoding,
data: htmlbodycontent
},
text: {
charset: encoding,
data: textbody,
},
},
subject: {
charset: encoding,
data: subject,
},
},
source: sender,
});
# If something goes wrong, display an error message.
rescue Aws::SES::Errors::ServiceError => error
puts "Email not sent. Error message: #{error}"
end
end
The email is sending well by AWS but my rails application has gone down like
A timeout occurred, error code 524
I couldn't get the breaking point, why has my application gone down every time?
Thanks in Advance
If 524 is an HTTP status code then it means...
Cloudflare was able to make a TCP connection to the website behind them, but it did not reply with an HTTP response before the connection timed out.
Meaning your Rails app is behind a Cloudflare proxy. Cloudflare received an HTTP request, forwarded it to your app, waited around for your app to respond, but your app never did. A more detailed explanation can be found here.
Probably because it's trying to send emails to 3000 people one-by-one.
There's two strategies to fix this.
Use Bulk Email
Since the content of the email is the same for everyone, use an email template to send bulk email using the #send_bulk_templated_email method.
You can send to up to 50 addresses at a time, so use #each_slice to loop through emails in slices of 50.
This will be more efficient, but your app will still be waiting around for 3000/50 = 60 AWS API calls. At worst it will still time out. At best the user will be waiting around for a form submission.
Use A Background Job
Anytime your app needs to do something that might take a lot of time, like using a service or a large database query, consider putting it into a background job. The Rails app queues up a job to send the emails, and then it can respond to the web request while the mailing is handled in the background. This has other advantages: errors calling the service won't cause an error for the user, and failed jobs due to a temporary service outage can automatically be retried.
In Rails this is done with ActiveJob and you could write a job class to send your mail.
Use ActionMailer
However, Rails also offers a class specifically for sending email in the background: ActionMailer. You can have ActionMailer use AWS with the aws-sdk-rails gem.
config.action_mailer.delivery_method = :ses

How do I get the JSON response from Dialogflow with Rails?

I understand the whole process of dialogflow and I have a working deployed bot with 2 different intents. How do I actually get the response from the bot when a user answers questions? (I set the bot on fulfillment to go to my domain). Using rails 5 app and it's deployed with Heroku.
Thanks!
If you have already set the GOOGLE_APPLICATION_CREDENTIALS path to the jso file, now you can test using a ruby script.
Create a ruby file -> ex: chatbot.rb
Write the code bellow in the file.
project_id = "Your Google Cloud project ID"
session_id = "mysession"
texts = ["hello"]
language_code = "en-US"
require "google/cloud/dialogflow"
session_client = Google::Cloud::Dialogflow::Sessions.new
session = session_client.class.session_path project_id, session_id
puts "Session path: #{session}"
texts.each do |text|
query_input = { text: { text: text, language_code: language_code } }
response = session_client.detect_intent session, query_input
query_result = response.query_result
puts "Query text: #{query_result.query_text}"
puts "Intent detected: #{query_result.intent.display_name}"
puts "Intent confidence: #{query_result.intent_detection_confidence}"
puts "Fulfillment text: #{query_result.fulfillment_text}\n"
end
Insert your project_id. You can find this information on your agent on Dialogflow. Click on the gear on the right side of the Agent's name in the left menu.
Run the ruby file in the terminal or in whatever you using to run ruby files. Then you see the bot replying to the "hello" message you have sent.
Obs: Do not forget to install the google-cloud gem:
Not Entirely familiar with Dilogflow, but if you want to receive a response when an action occurs on another app this usually mean you need to receive web-hooks from them
A WebHook is an HTTP callback: an HTTP POST that occurs when something happens; a simple event-notification via HTTP POST. A web application implementing WebHooks will POST a message to a URL when certain things happen.
I would recommend checking their fulfillment documentation for an example. Hope this helps you out.

Rails 5.0.1 - ActionMailer with MailGun, setting 'noname' for filename when directly attaching base64 string

I have read the entire official guide here and about 5 questions on SO that describe this problem for Rails 3 and Rails 4.
This question is for Rails 5, but he is using an actual file, not an encoded string.
This is my mailer code (and yes I have both views setup). The email comes through perfectly fine, with the attached PDF.
def pdf_quote(proposal)
#proposal = proposal
email_with_name = %("#{#proposal.first_name} #{#proposal.last_name}" <#{#proposal.email}>)
filename = "QD-#{#proposal.qd_number}_#{#proposal.last_name}.pdf"
attachments[filename] = {
mime_type: 'application/pdf',
encoding: 'Base64',
content: #proposal.pdf_base64
}
mail(
to: email_with_name,
from: 'Floorbook UK <email address>',
subject: 'Your Personal Flooring Quote is attached',
sent_on: Time.now
)
end
In gmail the attachment is called 'noname' and in Postbox it is called 'pb_mime_attachment.pdf'
How can I get ActionMailer to use the filename I provide?
Note that I am using MailGun (mailgun-ruby gem 1.1.4) to send the email, so it could be the gem at fault here.
It turned out to be a bug with 3rd party mailgun-ruby gem.
I raised a bug with them, here are the details:
https://github.com/mailgun/mailgun-ruby/issues/82
It is fixed (I tested it) in version 1.1.5.

Installing Mollie payments API: unpermitted_parameters

I am installing the Mollie payments API. Unfortunately, I get an unpermitted parameters: _method, authenticity_token (in the log). Here I would expect that a new window is opened that connects with Mollie.
I call the API by:
if #reservation
mollie = Mollie::API::Client.new('test_gUejkz43UkdeCauC22J6UNqqVRdpwW')
payment = mollie.payments.create(
amount: 10.00,
description: 'My first API payment',
redirectUrl: 'http://localhost:3000'
)
The reservation is made and the site does not give an active error, which makes me think the API is not called. Does anyone see what I am doing wrong? I installed the gem, I use Rails 4 and use require 'Mollie/API/Client' on top of the controller.
Any suggestionis highly appreciated! Thanks

Rails not sending (ical) attachments in emails

I'm trying to send an email with an ical attachment, I'm running rails v.3.2.13 and using the icalendar gem (to generate the ical string) see. (In development mode in case that might be a problem).
The relevant mailer code looks like this:
def mailme
ical = Icalendar::Calendar.new
...
attachments["meetings.ics"] = { mime_type: "text/calendar", content: ical.to_ical }
mail(from: email, to: recipient, ...)
end
there is also template file with the same name (mailme.html.erb)
The problem is the mail (html) is send without the attachment.
As usual any help would be much appreciated.
Thank you.
I've gotten them working with something like below:
mail.attachments['meeting.ics'] = { mime_type: 'application/ics',
content: ical.to_ical }
mail(from: email, to: recipient, ...)
So it's possible you need to call it on #attachments on the mail object instead of calling it on the current context. I'm not sure if your mime type needs to be application/ics, but that's worked fine for me in my systems.
In case someone else stumbles upon this.
If you are using delayed_job check out https://github.com/collectiveidea/delayed_job/wiki/Common-problems#wiki-Sending_emails_with_attachments
To fix this, remember to add this line to your mailer:
content_type "multipart/mixed"

Resources