Ruby Net::Smtp - Send Email with Bcc: Recipients

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

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.

Ruby: Emails go missing for Cc addresses

I ended up dropping the use of mailfactory to try mail instead, but I hit an issue not covered in the documentation. The doc-based example below:

Mail.defaults do
delivery_method :smtp, address: 'smtp.domain.com', port: 25
end

fails with Net::SMTPServerBusy exception:

/usr/lib/ruby/2.1.0/net/smtp.rb:957:in `check_response': 450 4.7.1 <localhost.localdomain>: Helo command rejected: Service temporarily unavailable (Net::SMTPServerBusy)

For some reason, it insists in using the localhost instead of the specified server specified by the address key. This looks like a bug in mail (2.6.3) to me... though I might be missing something.

Instead, I had to add the :domain => 'domain.com' pair to get around the problem, as shown below:

options = {
:address => 'smtp.domain.com',
:port => 25,
:domain => 'domain.com' # <<-- required
}

Mail.defaults do
delivery_method :smtp, options
end

This this SO post was helpful in figuring this out.


EDIT-1: Although this did work in the testing environment, the production environment still has the problem. I will update this post when this is fully solved.

EDIT-2: It turns out that the SMTP server was causing a Net::SMTPFatalError exception with the error message Relay access denied. I'm not sure why that was happening, but it appears to be a side-effect of how the IT Dept. has configured the servers.

Instead, to solve the Net::SMTPFatalError I had to use a different internal SMTP server that had been configured to allow relaying emails to internal addresses.

After running the program in production yesterday, the previously missing emails started to show up as expected.

Cant get ruby net/smtp to send html code via email

This will solve your problem, I tested:

  message = <<-MESSAGE_END.gsub(/^\s+/,'')
From: #{from}
To: #{to}
Subject: #{subject}
Mime-Version: 1.0
Content-Type: text/html
Content-Disposition: inline

<b>This is my simple HTML message</b><br /><br />
It goes on to tell of wonderful things you can do in ruby.
MESSAGE_END

The issue is with leading empty spaces, we get rid of them with: MESSAGE_END.gsub(/^\s+/,'')


EDIT: That worked well on my email client but as @stefan pointed out it strips empty lines. If you need those empty lines I have three options:

  1. MESSAGE_END.gsub(/^ {6}/,'') # That works but you would have to update in case indentation changes.
  2. MESSAGE_END.lines.map{|l| l.gsub(/^\s+([^$])/,'\1')}.join # That works despite of indentation changes, but we kinda sacrifice readability.
  3. MESSAGE_END.gsub(/^[ ]+/,'') # That was suggested by @stefan and probably reads better.

Send email to address alternate from To:

Mail vs. Envelope

In emails as in physical mails (paper sheet in paper envelope), the recipient on the envelope may differ from the recipient on the letter sheet itself.

As the mailman would only consider the envelope's recipient, so do mail servers.

That means, one actually can tell the smtp service to send and email to a recipient different than the one listed in the To: field of the emails header.

Trying This Out Manually

You can try this out, manually, for example, by using the sendmail command of postfix.

# bash
sendmail really_send_to_this_address@example.com < mail.txt

where

# mail.txt
From: foo@example.com
To: this_will_be_seen_in_the_to_field_in_email_clients@example.com
Subject: Testmail

This is a test mail.

This will deliver the email to really_send_to_this_address@example.com, not to this_will_be_seen_in_the_to_field_in_email_clients@example.com, but the latter will be shown in the recipient field of the mail client.

Specifying the Envelope_to Field in Rails

The ActionMailer::Base internally uses the mail gem. Currently, Apr 2013, there is a pull request, allowing to specify the envelope_to field on a message before delivery.

Until this is pulled, you can specify the fork in the Gemfile.

# Gemfile 
# ...
gem 'mail', git: 'git://github.com/jeremy/mail.git'

You need to run bundle update mail after that, and, depending on your current rails version, maybe also bundle update rails in order to resolve some dependency issues.

After that, you can set the envelope recipient of a mail message like this:

# rails
message # => <Mail::Message ...>
message.to = [ "this_will_be_seen_in_the_to_field_in_email_clients@example.com" ]
message.smtp_envelope_to = [ "really_send_to_this_address@example.com" ]
message.from = ...
message.subject = ...
message.body = ...
message.deliver

Documentation

  • https://github.com/mikel/mail/pull/477
  • http://rubydoc.info/github/jeremy/mail/master/Mail/Message#smtp_envelope_to%3D-instance_method
  • https://github.com/mikel/mail

cc, bcc not working with Net::SMTP on perl, with multiple email ids

You'll need to notify the server of all the recipients first. The code should look something like this:

$smtp->mail($supportEmail);
$smtp->to($toAddress);
$smtp->cc($ccList);
$smtp->data();
$smtp->datasend("From: $supportEmail\r\n");
$smtp->datasend("To: $toAddress\r\n");
$smtp->datasend("Cc: $ccList\r\n");
$smtp->datasend("Subject: " .$subject. "\r\n");
$smtp->datasend("\r\n");

#Send the message.
$smtp->datasend("$message");
$smtp->datasend("\r\n");
$smtp->dataend();

ArgumentError: An SMTP To address is required to send a message. Set the message smtp_envelope_to, to , cc, or bcc address

The error message is not about the SMTP envelope, but about the sender:

An SMTP To address is required to send a message

the rest is just a generic message.
Something in your testmail@mymailaddress.com is not working.
Do you use a real, working address? If not, try with one.

Sendgrid for an email feature within Ruby on Rails app

This post:
Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)

Got me up and running. The key being putting the SMTP and sendgrid information in the environment.rb file.

I can't explain exactly why that made the difference, but it did.

How to send email with DKIM to BCC using Chilkat using C# .Net

Here is an example that shows how to send DKIM signed email, and also explains how to add BCC email addresses.

https://www.example-code.com/csharp/dkim_send_email.asp



Related Topics



Leave a reply



Submit