I'm trying to read an email using ruby mail gem.
But mail.body.decoded returns me not just the body message. How can I clean up this body message and remove unwanted text like:
-20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-
message = $stdin.read
mail = Mail.read_from_string(message)
puts mail.body.decoded
--20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n REAL BODY TEXT \\n\n--20cf30433c9a437cc304939017ef\nContent-Type: text/html; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n<br clear=3D\"all\">--20cf30433c9a437cc304939017ef--
How can I clean up this email body mail message extracting only the REAL BODY TEXT , without ANY header ?
I'm creating a simple Ticket System based in Ruby on Rails, and a ticket is created when an email is received by ticket#mydomain.com. But when the message is in HTML format the BODY TEXT is surrounded by HEADERs text.
If you have a properly formatted email, you can use Mail helper methods:
mail = Mail.new(email_string)
mail.text_part # finds the first text/plain part
mail.html_part # finds the first text/html part
This doesn't always work if you have e.g. single part messages (text only) or receive email from the internet at large since you can't rely on formatting from every client out there. Believe me, I've learned the hard way.
looks like you've got a multipart email, so you can use
mail.parts[0].body.decoded
These will probably come in handy too:
mail.multipart?
mail.parts.length
The gem documentation at github is pretty decent
With the mail gem, you can do:
text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`
Add the mail gem and just use email body format with mail.parts[1].body.decoded.
Related
I am new to power automate. I need to read my email attachments and send each attachment to webapi as base64. I used below expression to convert email attachment to base64. but flow shows error as "Correct to include a valid reference to 'Get_Attachment_(V3)' for the input parameter(s) of action 'HTTP'.
what should be correct expression to convert attachment to base64.
expression: base64(body('Get_Attachment_(V3)')?['contentBytes'])
error:
A couple of things.
Firstly, in your JSON body, you just need to put quotes around the value of the content property.
Secondly, make sure you have a Get Attachment (V#) step prior to the HTTP action.
This step actually retrieves the contents of the attachment.
I have this Sinatra::Base code:
class Crush < Sinatra::Base
post '/upload' do
erb params.inspect
end
end
I am using Postman and its interface for uploading a file. So I send a POST request with form-data, where in the body of the request the name is hello and the value is a file test.txt which contains just a simple string hey there.
When I do params.inspect I get this long string
{"------WebKitFormBoundaryocOEEr26iZGSe75n\r\nContent-Disposition: form-data; name"=>"\"hello\"; filename=\"test.txt\"\r\nContent-Type: text/plain\r\n\r\nhey there\r\n------WebKitFormBoundaryocOEEr26iZGSe75n--\r\n"}
So basically a long has with a single key and a single value. Reading most Sinatra tutorials (where the file is accepted from a form), there's a nice way Sinatra handles this using params[:file], but this doesn't seem to be the case when the file is coming straight from the body of an HTTP request.
I tried a non-modular approach too withou Sinatra::Base, thinking it's some parsing middle-ware missing, but got the same result.
Is there something I'm missing here? Must I go and make my own custom parser to get the content of this long hash? Or is there an easier way?
I figured it's Postman issue. When I switch from 'x-www-form-urlencoded' to 'form-data' in Postman, in the Header section, the field: Content-Type => application/x-www-form-urlencoded is NOT removed. So for those who encounter this problem, make sure you remove it manually.
I have a question about net/smtp
For html emails you have to set this in the header of the email content-type: text/html . However, if you want to send an attachment you have to change it to content-type: multipart/mixed. Which would make the html email...not html anymore.
So the question is.. how do I accomplish both? HTML and attachment?
Thank you
Each attachment has its own MIME type
Each part of a multipart email has its own MIME type. So, while the email's content-type is "multipart/mixed", each attachment has its own MIME type (text, HTML, etc).
Here is an example multipart email from MIME and HTML in Email by Doug Steinwand:
To: whoever#someplace.com
Subject: MIME test
Content-type: multipart/mixed; boundary="theBoundaryString"
--theBoundaryString
Plain text message goes in this part. Notice that it
has a blank line before it starts, meaning that this
part has no additional headers.
--theBoundaryString
Content-Type: text/html
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Content-Base: "http://somewebsite.com/"
<body><font size=4>This</font> is a
<i>test</i>.
--theBoundaryString--
Here you can see that the text attachment has no explicit content type. When an attachment has no explicit content type, it is US ASCII TEXT. The HTML attachment has a content type of "text/html". There could be other attachments, each with their own MIME type.
Consider using the "mail" Gem
The mail gem makes sending and parsing multi-part emails very easy. It is stable, well maintained, and widely used.
This example from its README shows how to send a multi-part mail with a text part and an HTML part:
mail = Mail.deliver do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First multipart email sent with Mail'
text_part do
body 'This is plain text'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>'
end
end
Im using Sinatra and this gem, and Im able to send email ok. However, when I send an ERB file, it comes through as plain text and it doesnt render the ruby code. For example:
mail = Mail.deliver do
to user_email
from 'support#iconosites.com'
subject 'Your Upgrade is being processed'
body File.read('views/email.erb')
end
Is there a way to have it render the Ruby code?
Thanks!!
I haven't tested this but it looks like you're trying to render an erb template as the body of your email.
I think you'd need to do something more along the lines of:
require 'erb'
template = ERB.new(File.read('views/email.erb'))
mail = Mail.deliver do
to user_email
from 'support#iconosites.com'
subject 'Your Upgrade is being processed'
body template.result
end
There's a couple of email examples on the ruby-docs page for erb templates here also: http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html
I would like to use Ruby Net::SMTP to send email. The routine
send_message( msgstr, from_addr, *to_addrs )
works well in my code for sending email, but it is not clear from this API how to send email to a list of people that need to be blind copied (bcc:).
Am I missing something, or is it just not possible with Net::SMTP?
The to_addrs parameter of send_message specifies the envelope to addresses. Including an address in to_addrs has no effect on the to and cc addresses that get included in the message header.
To bcc a recipient, include the address in the to_addrs parameter, but don't include it in the headers in msgstr. For example:
msgstr = <<EOF
From: from#example.org
To: to#example.org
Cc: cc#example.org
Subject: Test BCC
This is a test message.
EOF
Net::SMTP.start(smtp_server, 25) do |smtp|
smtp.send_message msgstr, 'from#example.org',
'to#example.org', 'cc#example.org', 'bcc#example.org'
end
This will send an email to three recipients: to#example.org, cc#example.org and bcc#example.org. Only to#example.org and cc#example.org will be visible in the received message.
Yes it's not possible easily with Net::STMP. But there are a really great gem to manage your email sending (http://github.com/mikel/mail). I encourage you to use it.