Send an email from Ruby without an SMTP server installed / running?

On a Mac, I can send an email message from the command line using the mail command, but definitely I don't have an SMTP server installed on my MacBookPro.

So, can I send an email from Ruby without an SMTP server? I don’t care about speed, I just want to send email without additional software.

+8
ruby email smtp sendmail
source share
7 answers

You can simply call the mail command from your Ruby code. Use system or backticks or something more complex, such as open3 , to interact with system commands ... Here is a good overview on various methods: http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/

+5
source share

About mail and sendmail

I don't know much about mail , but a quick Google search seems to indicate that it uses a postfix, which is the default SMTP server installed on Mac computers. In other words, you have installed and are using the SMTP server on your Macbook Pro.

About Ruby

So, can I send email from Ruby without an SMTP server?

Yes and no. You do not need the SMTP server to run on the same computer as your Ruby process. In fact, you don’t even have to start your own SMTP server. However, you need an SMTP server to send your email.

About SMTP

This article on howstuffworks provides a good explanation of what SMTP does. Basically, you need an SMTP server somewhere that accepts your email, talks to other SMTP servers and sends your email for delivery. With Ruby, you can configure Net :: SMTP to connect to the SMTP server of your choice.

About what you are trying to do

If you want to write and execute a script that will provide a small number of email messages, create a fake email account in Gmail / Live and use your SMTP servers to send email.

If you want to create and run an application that will deliver emails to your users, use Mandrill , MailGun or SendGrid. Mandrill has a free level for you to get started.

I do not recommend starting your own SMTP server for most use cases, because your emails will most likely be marked as spam. (Comcast might also think that you have malware on your network.) Professional services such as Mandrill can help you configure SPF and DKIM records to authenticate your emails and improve your sending reputation.

(If you just want to check email in dev mode, use MailCatcher .)

Conclusion

Subscribe to Mandrill , then use Net :: SMTP in Ruby to connect to your SMTP servers. No additional software required.

+4
source share

If your mail command works, you can send mail from inside Rubin. And if your mail command is already running on your Mac, then you already have an SMTP server running on your mac, since the default is postfix , which is installed. The default mail command is /usr/sbin/sendmail , which in this case is the interface to postfix . (Try man sendmail from the terminal.)

Now, with this, you will probably experience something similar when trying to use Net :: SMTP locally:

 [3] pry(main)> smtp = Net::SMTP.start('localhost',25) Errno::ECONNREFUSED: Connection refused - connect(2) 

This means that you need to do something to tell your poppy that it can accept connections on port 25. Another alternative is to use this sendmail as a transport access method, which may actually be the best option. Access to port 25 is disabled, so no one else can use your mac as a mail relay. To go through sendmail command means that only programs on your mac can send mail (go figure).

My suggestion here is to use the mail gem (or pony if you prefer) and configure it to use sendmail . From mail README File:

Sending via sendmail can be done as follows:

 mail = Mail.new do from 'me@test.lindsaar.net' to 'you@test.lindsaar.net' subject 'Here is the image you wanted' body File.read('body.txt') add_file :filename => 'somefile.png', :content => File.read('/somefile.png') end mail.delivery_method :sendmail mail.deliver 

Similarly, if you use ActionMailer , you can configure it to use sendmail .

+2
source share

Have you tried Pony Stone ? It provides a really simple interface for sendmail that should already be installed on your Mac.

+2
source share

Is your goal to write and send email from the CLI?

There are a number of mail stones, including mail . Perhaps you can also play directly with Ruby Net :: SMTP and get something working, depending on what you are trying to do.

+1
source share

You do not need to have an SMTP server, you just need to know who you can connect to.

See Net :: SMTP . This is part of Ruby.

+1
source share

If you used the mail command or /usr/sbin/sendmail , and your mail was delivered successfully, then your computer had an MTA (mail / message transfer agent, for example postfix, sendmail, or something else).

when you use the mail command to send mail to guser@gmail.com , your mail command will send mail to the local SMTP server.

The (local) SMTP server will perform the following tasks.

  • SMTP Server (MTA) will search for MX for gmail.com
  • Connect to port 25 of the MX server and send mail using SMTP (HELO, MAIL FROM, RCPT TO, DATA commands).

The Mx server delivers mail to the appropriate mailbox (guser).

In the above answers, you are invited to use Net :: SMTP, where you will connect to some SMTP server and send mail to this SMTP server, which will again perform the above tasks (local SMTP server above) to perform delivery,

So, to send mail, you need an SMTP server. or you will have to code yourself / use some library to perform an MX search and send mail to the MX server by issuing SMTP commands in accordance with RFC 2821.

0
source share

All Articles