Ruby implementation two ways to send mail _ruby topics

Source: Internet
Author: User
Tags openssl openssl library in domain mail account port number ssl connection hopy

In fact, as long as you capricious can, with Telnet is also able to send an e-mail. But the cat is not so capricious, or use the KISS principle to send mail it. This blog post only describes how to send e-mail, but does not involve the mail, the future if the opportunity to open a separate blog introduction.

Ruby has 2 ways to send mail via SMTP, one of which is sent directly with NET::SMTP, compared to the bottom. If you want to send an attachment, additional gem:mailfactory is required, and mailfactory is dependent on the package mime-types. Even so, if the mail server connection requires SSL, require file smtp-tls.rb is required, and the RB file requires OpenSSL package support! The second approach is to use the high-level gem package mail directly, including the ability to add attachments, but the gem also relies on the Mime-types package, which is designed to describe the mail file format, commonly known as the Multipurpose Internet Mail Extension type. Each of these methods is described in turn below.

Method One: Use NET::SMTP

The SMTP port number for a normal unencrypted connection is 25, and if it is encrypted it may be 587 or 465, depending on the description of the specific mail server. Started with the QQ mail server, but always unstable, so changed for Hotmail, but Hotmail needs SSL connection. The OpenSSL package is required as described above. Trouble began: Rubygems.org with gem anyway, then will openssl.gem directly download to local, with Gem install-l openssl.gem installation, found require or error, need native ext:openssl.so Dynamic Library expansion, into the OpenSSL ext source directory with make compilation prompts error: Ignore OpenSSL broken by Apple, prompted me to use other OpenSSL library path, I use hair ah! is Mac OS x not supported? But check that the system is installed with OpenSSL:

Copy Code code as follows:

/private/etc/openssl
/private/etc/openssl/cert.pem
/usr/bin/openssl
/usr/lib/pkgconfig/openssl.pc
/usr/local/cellar/openssl

Dynamic libraries also exist, but not Ruby c_ext!

Copy Code code as follows:

Apple@kissair:ruby_src$locate Libssl.dylib
/applications/xcode6-beta4.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.10.sdk/usr /lib/libssl.dylib
/applications/xcode6-beta4.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.9.sdk/usr/ Lib/libssl.dylib
/opt/com.bitaxis/lib/libssl.dylib
/opt/local/lib/libssl.dylib
/usr/lib/libssl.dylib
/usr/local/cellar/openssl/1.0.1e/lib/libssl.dylib

God horse situation? Taking into account TK's problem with Ruby (see my other blog post for Ruby using TK under Mac OS x), I suspect that the Ruby version itself does not support native OpenSSL, which I compiled and installed with the ruby-2.1.5 source code downloaded in Ruby-lang! So with RVM download its ruby-2.1.5 version, a try unexpectedly can bird! But then found Hotmail.com encrypted connection or not even, and change back to QQ mailbox, with unencrypted smtp,25 port connection. This is basically stable to send, if the 163-bit mailbox test found more stable, not refactored code:

Copy Code code as follows:

#!/usr/bin/ruby
#encoding: Utf-8

Require ' NET/SMTP '
Require './smtp-tls.rb '
Require ' mailfactory '

#Senders and Recipients
From_name = ' localhost '
From_mail = ' 12345678@qq.com '
To_name = ' KS '
To_mail = ' 88888888@qq.com '

#Servers and authentication
#smtp_host = ' smtp.qq.com '
Smtp_host = ' smtp.163.com '
Smtp_port = #465 587 25
#smtp_domain = ' qq.com '
Smtp_domain = ' Localhost.localdomain '
Smtp_user = "Wangyi@163.com"
Smtp_pwd = "xxxxxxxx"
#smtp_user = "12345678@qq.com"
#smtp_pwd = ' xxxxxxxx '

#The subject and the message
t = Time.now
SUBJ = ' 1331 thinkpad Test hopy '
Msg_body = "Send msg from ruby.\n"

#The Date/time should look something Like:thu, 2006 12:33:22-0700
Msg_date = T.strftime ("%a,%d%b%Y%h:%m:%s +0800")

#Compose the message for the email

God horse situation? Taking into account TK's problem with Ruby (see my other blog post for Ruby using TK under Mac OS x), I suspect that the Ruby version itself does not support native OpenSSL, which I compiled and installed with the ruby-2.1.5 source code downloaded in Ruby-lang! So with RVM download its ruby-2.1.5 version, a try unexpectedly can bird! But then found Hotmail.com encrypted connection or not even, and change back to QQ mailbox, with unencrypted smtp,25 port connection. This is basically stable to send, if the 163-bit mailbox test found more stable, not refactored code:

Copy Code code as follows:

#!/usr/bin/ruby
#encoding: Utf-8

Require ' NET/SMTP '
Require './smtp-tls.rb '
Require ' mailfactory '

#Senders and Recipients
From_name = ' localhost '
From_mail = ' 12345678@qq.com '
To_name = ' KS '
To_mail = ' 88888888@qq.com '

#Servers and authentication
#smtp_host = ' smtp.qq.com '
Smtp_host = ' smtp.163.com '
Smtp_port = #465 587 25
#smtp_domain = ' qq.com '
Smtp_domain = ' Localhost.localdomain '
Smtp_user = "Wangyi@163.com"
Smtp_pwd = "xxxxxxxx"
#smtp_user = "12345678@qq.com"
#smtp_pwd = ' xxxxxxxx '

#The subject and the message
t = Time.now
SUBJ = ' 1331 thinkpad Test hopy '
Msg_body = "Send msg from ruby.\n"

#The Date/time should look something Like:thu, 2006 12:33:22-0700
Msg_date = T.strftime ("%a,%d%b%Y%h:%m:%s +0800")

#Compose the message for the email

Copy Code code as follows:

#如果使用mailfactory发送则实际用不着msg格式了
msg = <<end_of_message
Date: #{msg_date}
From: #{from_name} <#{from_mail}>
To: #{to_name} <#{to_mail}>
Subject: #{subj}

#{msg_body}
End_of_message

Mail = mailfactory.new
Mail.to = To_mail
Mail.from = From_mail
Mail.subject = subj
Mail.text = Msg_body
Mail.attach (File.expand_path ("./mail.rb")) #发送附件

#smtp = Net::smtp.new (smtp_host,587)
#smtp. Enable_starttls
#Net:: Smtp.start (Smtp_host, Smtp_port, Smtp_domain, Smtp_user, Smtp_pwd,:p Lain) do |smtp|
Net::smtp.start (Smtp_host,smtp_port,smtp_domain, Smtp_user, smtp_pwd,: login) do |smtp|
#smtp. Send_message msg, Smtp_user, To_mail
#mail. to = To_mail
#puts Smtp.methods
#smtp. Enable_starttls
Smtp.send_message (Mail.to_s,smtp_user,to_mail)
End

Method Two: Use Ruby Gem:mail (to Be continued)

Mail is a relatively advanced mail library that includes the ability to send attachments. However, after downloading the local installation began to error prompts refuse port 25 and so on. At first I thought it was not enough, with sudo execution, found in the implementation of require ' mail ' command and Error! found that the original installation was not installed with sudo permissions, first gem uninstall Mail.gem, and then reinstall: sudo gem install Mail.gem, at this time in sudo and normal permissions require ' mail ' are normal birds:

Copy Code code as follows:

#!/usr/bin/ruby

Require ' mail '

SMTP = {: Address => ' smtp.163.com ',:p ort =>,:d omain => ' 163.com ', \
: User_name => ' wangyi@163.com ',:p assword => ' xxxxxxxx ', \
: Enable_starttls_auto => True,: Openssl_verify_mode => ' None '}
mail.defaults {DELIVERY_METHOD:SMTP, SMTP}
Mail = mail.new do
From ' wangyi@163.com '
To ' 12345678@qq.com '
Subject ' Test mail '
Body ' body:hello send mail way 2:) '
Add_file File.expand_path ("./mail2.rb")
End
mail.deliver!

There is a small problem, that is, start SMTP user_name and from the Send mail account is not the same, resulting in always send failure, here are changed to wangyi@163.com on it. But in the method one can be different oh. There are 2 ways in domain change to ' localhost ' and other values can also send success, it seems that there is no relationship.

Finally, 2 methods are connected unencrypted, that is, the mail server allows non-SSL connection, if the mail server can only encrypt the above code can not be used. As for this time how to write code, if you know that the children's shoes please enlighten me oh.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.