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.
Related
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)
I am busy writing the last bit of an SMTP relay engine. Basically, people send mail to this SMTP address, it gets piped to files, analyzed, and then headers and footers are added to the body. My problem is, I'm not sure if my way of altering the mail will work for all cases. Basically, if I have this:
Return-path: <XXXX#YYYY>
Envelope-to: system-filter
Delivery-date: Thu, 25 Sep 2014 12:43:53 +0200
Received: from localhost ([127.0.0.1]:XXXX helo=XXXX.XXXX.com)
by XXXX.XXXX.com with XXXX(XXXX-XXXX-XXXX-XXXX:XXXX)
(Exim XXXX)
(envelope-from <XXXX#XXXX.XXXX>)
id XXXX-XXXX-67
for XXXX.XXXX#XXXX.com; Thu, 25 Sep 2014 12:43:52 +0200
Received: from XXXX.XXXX.XXXX.XXXX ([197.87.204.210]) by XXXX.XXXX.com
(Horde Framework) with HTTP; Thu, 25 Sep 2014 10:43:50 +0000
Date: Thu, 25 Sep 2014 10:43:50 +0000
Message-ID: <XXXX.Horde.XXXX#XXXX.XXXX.com>
From: XXXX#XXXX.XXXX
To: XXXX.XXXX#XXXX.com
Subject: This is the subject
User-Agent: Internet Messaging Program (IMP) H5 (XXXX)
Content-Type: text/plain; charset=UTF-8; format=flowed; DelSp=Yes
MIME-Version: 1.0
Content-Disposition: inline
X-OutGoing-Spam-Status: No, score=-2.9
And this is the message.
The idea was to either:
1. Just find the first blank line and put the header there, and then just append the footer to the end.
2. Use PHP Mime Mail Parser to alter the body
The problem with 1 is that I think there might be different formats coming in that don't follow the normal layout. The problem with 2 is, I'm limited to whatever the mail parser can parse.
So, what is the safest way to relay e-mails as I've desribed?
Just prepending or appending something to a message body will not work. It will not work because the message can by multipart (e.g. with attachments) or encoded (e.g. base64). PHP Mime Mail Parser seems to be able to handle these cases, but it doesn't have a method to save the modified message.
I would recommend using some email parsing library which can write messages, not just read. For example, Python standard library has email package which can do what you need. However, you would have to check if the message is multipart and if so add the header and the footer to the right part (i.e. to the actual content and not to an image or attachment). You will also need to check whether the message is plain text or HTML and have your header and footer formatted appropriately.
If you look at the doc for Net::IMAP::Envelope here
http://www.ruby-doc.org/stdlib/libdoc/net/imap/rdoc/classes/Net/IMAP.html
the struct includes these two fields:
from: Returns an array of Net::IMAP::Address that represents the from.
sender: Returns an array of Net::IMAP::Address that represents the sender.
What's the difference between these two fields, conceptually?
You're seeing fields that IMAP parsed from the email message itself. From Wikipedia's Email: Header fields:
From: The email address, and optionally the name of the author(s). In many email clients not changeable except through changing account settings.
Sender: Address of the actual sender acting on behalf of the author listed in the From: field (secretary, list manager, etc.).
There's also Reply-To which has to be considered when you want to reply to a message:
Reply-To: Address that should be used to reply to the message.
There is a bit more discussion on that page about spoofing and attempts by the MTA (server) to avoid and detect it, but the above is the gist of the info.
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.
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.