I am trying to send a mail with mailgun api but I always get a BAD REQUEST 400 error. The corresponding piece of code is
data = Multimap.new
data[:from] = "Excited User <postmaster#mydomain.mailgun.org"
data[:to] = "foo#bar.com"
data[:subject] = "Hello"
data[:text] = "Testing some Mailgun awesomness!"
RestClient.post "https://api:my-key"\
"#api.mailgun.net/v3/mydomain/messages", data
If I rewrite the code as
RestClient.post "https://api:my-key"\
"#api.mailgun.net/v3/mydomain/messages",
:from => "Mailgun Sandbox <postmaster#mydomain.mailgun.org>",
:to => "foo#bar.com",
:subject => "Hello",
:text => "Testing some Mailgun awesomness!"
it works but i want to add attachements, html etc . Am I missing something here?
Related
I am trying to call a SOAP api using Savon gem. I am getting the following error: "(pre:svcFault) Service Fault"
I created both the header and message for the request.
Here is the request sent from SoapUI: SoapUI request.
i am getting a true response from SoapUI.
My code is shown below:
class SoapApi
require 'savon'
def self.initialize
header = {
"ebmCID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmMID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmRTID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmSID" => "FMobile-FCUBS",
"ebmTimestamp" => "2019-06-10T12:27:46.1623586Z",
}
message = {
customerId: '00653473'
}
client = Savon.client(
:wsdl => "https://192.168.176.103:8012/tevs/pp.pm.evs.Customer_1.2?wsdl",
:ssl_verify_mode => :none
)
response = client.call(
:get_account_list,
:soap_header => header,
:message => message
)
return response
end
end
And here i am calling the above method:
#index.html.erb
<%=
SoapApi.initialize
puts #response
%>
Where you able to create a valid call using SoapUI (https://www.soapui.org.)? Try this first and make it work.
Next create a call from a plain ruby script - without Rails - which sends the same functional XML as you did in SoapUI before.
Third embed this code into your RoR application.
You can put the following in your client definition for better logging:
client = Savon.client(
:wsdl => "https://192.168.176.103:8012/tevs/pp.pm.evs.Customer_1.2?wsdl",
:ssl_verify_mode => :none,
log: true,
log_level: :debug,
pretty_print_xml: true
)
Compare the output with your working SoapUI example.
I am using postmark to send email from the application. Its working fine for normal emails, but the email attachments are not working.
It works fine on local, as on local i have the smtp+postmark settings (as to work it on local we need to have postmark along with smtp)
But on staging and production am using only SMTP settings
config/environments/staging.rb and config/environments/production.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => POSTMARK_API_KEY }
config/environments/development.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.postmarkapp.com",
:port => 25,
:domain => 'example.com',
:user_name => POSTMARK_API_KEY,
:password => POSTMARK_API_KEY,
:authentication => 'plain',
:enable_starttls_auto => true }
user_mailer.rb
class UserMailer < ActionMailer::Base
default from: DEFAULT_EMAIL
def send_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: attachment, encoding: 'base64' }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
Here attachment is the url of file saved on S3.
In development mode i receive email with attachment.
but not in staging and development mode.
Any help or suggestions would be appreciated. Thanks.
To send an email with attachment using Postmark Api
This approach is using curl command.
Require to get the remote file content
require "open-uri"
Require to get the encoding-decoding methods
require "base64"
Pass your remote file url to get the content of that
file_data = open('https://example.com/dummy.pdf').read
Encryp the bin object to send it via email
encrypt_data = Base64.encode64(file_data)
You can now send the email with attachment using postmark api and make sure to pass your API-KEY in the X-Postmark-Server-Token
system "curl -X POST \"http://api.postmarkapp.com/email\" \
-H \"Accept: application/json\" \
-H \"Content-Type: application/json\" \
-H \"X-Postmark-Server-Token: POSTMARK_API_KEY\” \
-v \
-d \"{From: 'from#example.com', To: 'to#example.com', Subject: 'Postmark test for Attachment Email', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user you have received email with attachment.</body></html>', Attachments:[{'ContentType': 'application/pdf', 'Name': 'dummy.pdf', 'Content': '#{encrypt_data}'}]}\""
Finally am able to find the actual cause of above issue.
I am using the gem postmark-rails, previously postmark was not supporting the email-attachments, so recently they had enhanced gem to support attachments and unfortunately i was using the old version of it, so i need to update gem version to latest as they have mentioned in one of their issues: attachment-issue
also i was trying to send url of file saved on S3, so instead of that i need to read that file from url and then send it as attachment
require "open-uri"
def send_refer_pt_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
url_data = open(attachment).read()
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: url_data }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
I have this code in Faye rackup script:
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
server_uri = URI.parse(BacklinkHealth::FAYE_SERVER)
faye_server.on(:subscribe) do |client_id, channel|
puts "subscribed #{channel}"
if channel.starts_with?('/pagination')
website_id = channel.split('/')[2]
page = channel.split('/')[3]
app = ActionDispatch::Integration::Session.new(Rails.application)
app.get app.pagination_website_backlinks_path(website_id, :page => page)
message = {:channel => channel, :data => app.response.body, :ext => {:auth_token => BacklinkHealth::FAYE_TOKEN}}
puts 'started HTTP post'
Net::HTTP.post_form(server_uri, :message => message.to_json)
puts 'finished HTTP post'
end
end
The problem is that the execution comes to "started HTTP post" and then it takes more than a minute for the message to be registered at the client-side javascript. The messge "finished HTTP post" is never printed :( I don't understand what is going on.
If I try to execute an identical HTTP post from Rails console, it goes through in an instant although it did happen once or twice that it took a minute.
Any ideas?
It's much better to use this:
require 'eventmachine'
EM.run {
client = Faye::Client.new(BacklinkHealth::FAYE_SERVER)
client.publish(channel, 'pagination' => pagination)
}
than Http requests... I couldn't get this to work with authentication (yet) but I will.
In rails 2.3.5 we were able to attach emails as attachments to other emails which were multipart emails using the following code:
recipients to
from from
subject subject
content_type "multipart/mixed"
part "text/html" do |p|
p.body = render_message("rampup_notification.text.html.erb", :mailbody => body)
end
part "text/plain" do |p|
p.body = render_message("rampup_notification.text.plain.erb", :mailbody => body)
end
email = enrollment_application.email
if email != nil && email.raw_email != nil
attachment :content_type => "message/rfc822", :filename => "icann.eml", :body => email.raw_email, :transfer_encoding => '7bit'
end
This was very temperamental to get to work with outlook, exchange etc along with other mailers.
How do I do this in rails 3?
I see: http://www.rubydoc.info/docs/rails/3.0.0/ActionMailer/Base:attachments
encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
:encoding => 'SpecialEncoding',
:content => encoded_content }
But I dont understand how to use this, is SpecialEncode a class I need to write that does 7bit encoding?
thanks
Joel
So it turns out rails 3 actionmailer is pretty smart, this seems to work fine:
#mailbody = body
attachments["icann.eml"] = {:content => email.raw_email }
mail(:to => to, :from => from, :subject => subject)
I have got mail message with following subject in my Gmail accout:
"400, значение, значение"
Here is the code I use to grab mail:
imap = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
imap.login(LOGIN, PASSWORD)
imap.select("INBOX")
messages = imap.search(['ALL']).map do |message_id|
msg =imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
result = {:mailbox => msg.from[0].mailbox, :host => msg.from[0].host, :subject => msg.subject, :created_at => msg.date}
imap.store(message_id, "+FLAGS", [:Deleted])
result
end
imap.expunge()
imap.logout
In msg.subject i've got following value "=?KOI8-R?B?MTAwLCDixc7ayc4sIDMwMDAgzMnU0s/X?="
It seems that IMAP haven't decoded it. Should I do I manually or IMAP library could it for me?
Mail::Encodings is really helpful here:
require 'mail'
test = "zwei plus =?ISO-8859-15?Q?zw=F6lf_ist_vierzehn?="
puts Mail::Encodings.value_decode(test)
returns
zwei plus zwölf ist vierzehn
How about using NKF?
require 'nkf'
...
result = {... :subject => NKF.nkf("-mw", msg.subject), ...}
-mw means MIME decode and utf-8 output