Ruby Net::SMTP - Send email with bcc: recipients - ruby

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.

Related

Microsoft graph API - empty bccRecipients list

This is the Scenario:
In the same Azure tenant, I used one account (user_1_address) to send emails to the other account (user_2_address) using outlook (o365).
I sent 3 emails, one where user_2_address is BCCed, one CCed, and one when it's the TO recipient.
I'm using Microsoft graph API to get a list of emails received by user_2_address in a specific time range, using this query:
https://graph.microsoft.com/v1.0/users/{<user_2_id>}/messages?$filter=
receivedDateTime ge <some date> and receivedDateTime lt <some other date>
and isDraft eq false
and sender/emailAddress/address ne '<user_2_address>'
I'm getting all the three emails user_2_address had received from user_1_address. But in the email user_2 was BCCed the bccRecipients list is empty, when it should contain user_2_address :(
I have seen this question about sending an email from Gmail and BCC an outlook user:
Microsoft graph API: empty BCC field
In that case, also the bccRecipients list was empty, but it was resolved by saying the BCC is removed when sending the emails from an external source (Gmail in that case). When for me it's not an external source - both users are using outlook in the same tenant.
So my questions are:
Is it the desired behaviour, or is it a bug?
Now, let's say I'm using the query above where I get all emails where the sender is not the user_2_address and it's not a draft. Can I assume that every email I get where user_2_address is not in the ccRecipients and toRecipients lists - that email was BCCed to user_2_address?
Thanks!
The bcc field in a Message is an envelope (P1) recipient only so you should always expect that it will be blank (no matter the context inside a tenant really make no difference). Like the other post referenced if it wasn't blank it would break the RFC and the purpose of a BCC, the only exception is the sent item (which is just a copy of the sent message)
No there are many scenarios that would break that particular logic eg forwarded email is one the comes to mind. You could certainly refine you result set that way, one thing you might want to examine is the X-MS-Exchange-Organization-Recipient-P2-Type: mail header that should get set in your internal to internal scenario (you need to look at the PidTagTransportMessageHeaders extended property to see it)

Transmission api setting wrong To header when specify header_to with multiple recipients

I'm trying to send an email to multiple people(multiple to addresses) and have them all listed in the email clients like a regular email. When I set the header_to field on all recipients to email1#foo.com, email2#foo.com I end up with emails that have a to header set to "First Name" <email1#foo.com, email2#foo.com> which is incorrect.
This shows up as a single person with multiple email addresses in most clients and the header is wrong.
Why is the sparkpost transmission api messing with the header_to field? It's docs say that it uses this in place of generating a To header for you.
After going through every page of docs I could find to try and figure out how sparkpost's backend works I've found that you must omit the Name field on all recipients.
https://developers.sparkpost.com/api/recipient-lists/#header-recipient-object

Email subject not correctly assigned with Ruby net/smtp

I use Ruby net/smtp
require 'net/smtp'
I send mails using the following method:
Net::SMTP.start(SMTP_SERVER) do |smtp|
smtp.send_message(message,"mailman-ruby#example.de",reciepent)
end
I want to add a subject to the mails i send but couldn't find one in the documentation.
What I tried so far is putting the subject into the message like this:
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
Unfortunally that doesnt do the trick!
Anyone knows how to set subject using 'smtp/net'?
net/smtp is a very low-level library. It expects the message to be an email message, including all the headers.
From the documentation
msgstr = <<END_OF_MESSAGE
From: Your Name <your#mail.address>
To: Destination Address <someone#example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string#example.com>
This is a test message.
END_OF_MESSAGE
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message msgstr,
'your#mail.address',
'his_address#example.com'
end
In your case, make sure to add a new line between the Headers and the body. Change
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
to
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
Instead of using net/smtp, I highly encourage you to use mail. mail uses a more high level interface for sending email messages without loosing the control of the message.
If you don't really care about advanced usage, you can also switch to pony.
The responsibility of the subject is not that of the SMTP protocol. It is held in one of the SMTP commands, which is to say DATA and as such, is not relevant in responsibility to that library.
However, the DATA will contain either proper (specification indicated) headers, or even some headers that are not specified, but not disallowed. PGP
According to RFC 5321 (and as expected) "Server SMTP systems SHOULD NOT reject messages based on perceived defects in the RFC 822 or MIME (RFC 2045 [21]) message header section or message body.".
So, your answer is going to be: "You don't set the subject by using that." But you would set the DATA with that information in it.
And the documentation that you linked to has an example of doing exactly that:
msgstr = <<END_OF_MESSAGE
From: Your Name <your#mail.address>
To: Destination Address <someone#example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string#example.com>
This is a test message.
END_OF_MESSAGE
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message msgstr,
'your#mail.address',
'his_address#example.com'
end
Documentation not linked here, as you have already provided the link.

Automating email reply for specific message

Is there a way to automate reply to a particular message. The process here is that you make a request through, eg. outlook, and then reply to the response of the request you made.
Yes, you make your application periodically check for mail in the inbox using POP3 or IMAP and do something if there's a message that matches with the format of your request.
Something like (not actually a working example, more like pseudo code)
mail=Mail.login(:server => 'foo',
:user => 'john',
:password => 'john_is_the_king_pimp'
)
while 1==1
mail.new_messages do |msg|
if msg.subject.match(/^REQUEST/)
read stuff from the message and do something about it
end
end
sleep 60
end

Mail gem - how to clean up the body string

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.

Resources