Ruby implements two mail sending methods, and ruby implements mail sending.
In fact, as long as you are willful, you can use telnet to send emails. However, this cat is not so capricious. We should use the KISS Principle to send emails. This blog post only describes how to send emails, but does not involve email receiving. If you have the opportunity to open a separate blog post in the future.
Ruby has two methods to send mails via smtp. One is to directly use Net: SMTP to send mails, which is relatively low. If you want to send attachments, you need an additional gem: mailfactory, and mailfactory depends on the mime-types package. Even so if the mail server connection requires ssl, you also need the require file smtp-tls.rb, and this rb file needs the support of the openssl package! The second method is to directly use the high-level gem package mail, which includes the function of adding attachments. However, this gem also relies on the mime-types package, which is specially used to describe the mail file format, it is commonly known as the multi-purpose Internet Mail Extension type. The following describes each method in sequence.
Method 1: Use Net: SMTP
Normally, the smtp port number of the unencrypted connection is 25. If it is encrypted, it may be 587 or 465, depending on the description of the email server. I started to use the QQ mail server, but it was always unstable and changed to hotmail. However, hotmail requires ssl connections. Openssl package as described above. The trouble is getting started: rubygems.org cannot be connected with gem, and then openssl will be released. download the gem directly to your local device and use the gem install-l openssl. gem installation, found that require or error, requires native ext: openssl. so dynamic library extension, go to the openssl ext source code directory and use make to compile the code. An error is prompted: Ignore OpenSSL broken by Apple, prompting me to use other openssl library paths. I use Mao! Is Mac OS X not supported? But check that openssl is installed in the system:
Copy codeThe Code is as follows:
/Private/etc/openssl
/Private/etc/openssl/cert. pem
/Usr/bin/openssl
/Usr/lib/pkgconfig/openssl. pc
/Usr/local/Cellar/openssl
The dynamic library also exists, but it is not ruby's c_ext!
Copy codeThe Code is 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
What is the situation of Shenma? Considering tk's ruby problems (see my other blog post on using tk in ruby on mac OS x), I suspect that this ruby version does not support native openssl, this version is compiled and installed in the ruby-2.1.5 source code downloaded from ruby-lang! Rvm is used to download its ruby-2.1.5 version! However, it was found that the hotmail.com encrypted connection still could not be connected, and the connection was switched back to the QQ mailbox, with non-encrypted smtp and port 25 connections. This can basically be sent stably. If the mailbox test of the transposition of 163 finds that it is more stable, the Code is not restructured:
Copy codeThe Code is 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 = '2017 @ qq.com'
To_name = 'ks'
To_mail = '2017 @ qq.com'
# Servers and Authentication
# Smtp_host = 'smtp .qq.com'
Smtp_host = 'smtp .163.com'
Smtp_port = 25 #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 = '1970 thinkpad test hopy'
Msg_body = "send msg from ruby. \ n"
# The date/time shocould look something like: Thu, 03 Jan 2006 12:33:22-0700
Msg_date = t. strftime ("% a, % d % B % Y % H: % M: % S + 0800 ")
# Compose the message for the email
What is the situation of Shenma? Considering tk's ruby problems (see my other blog post on using tk in ruby on mac OS x), I suspect that this ruby version does not support native openssl, this version is compiled and installed in the ruby-2.1.5 source code downloaded from ruby-lang! Rvm is used to download its ruby-2.1.5 version! However, it was found that the hotmail.com encrypted connection still could not be connected, and the connection was switched back to the QQ mailbox, with non-encrypted smtp and port 25 connections. This can basically be sent stably. If the mailbox test of the transposition of 163 finds that it is more stable, the Code is not restructured:
Copy codeThe Code is 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 = '2017 @ qq.com'
To_name = 'ks'
To_mail = '2017 @ qq.com'
# Servers and Authentication
# Smtp_host = 'smtp .qq.com'
Smtp_host = 'smtp .163.com'
Smtp_port = 25 #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 = '1970 thinkpad test hopy'
Msg_body = "send msg from ruby. \ n"
# The date/time shocould look something like: Thu, 03 Jan 2006 12:33:22-0700
Msg_date = t. strftime ("% a, % d % B % Y % H: % M: % S + 0800 ")
# Compose the message for the email
Copy codeThe Code is as follows:
# If mailfactory is used for sending, the msg Format is not used.
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") # Send attachments
# Smtp = Net: SMTP. new (smtp_host, 587)
# Smtp. enable_starttls
# Net: SMTP. start (smtp_host, smtp_port, smtp_domain, smtp_user, smtp_pwd,: plain) 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 2: Use ruby gem: mail (to be continued)
Mail is a relatively advanced email library that includes the function of sending attachments. However, after the local installation is downloaded, an error such as refuse port 25 is reported. At first, I thought the permission was insufficient. After executing the command with sudo, I found that an error was reported when I executed the require 'mail' command! We found that the installation was not performed with sudo permission at the time of installation. First, gem uninstall mail. then reinstall: sudo gem install mail. gem. In this case, require 'mail' under sudo and general permissions is normal:
Copy codeThe Code is as follows:
#! /Usr/bin/ruby
Require 'mail'
Smtp = {: address => 'smtp .163.com ',: port => 25,: domain => '2017. com ',\
: User_name => 'wangyi @ 163.com ',: password => 'xxxxxxxx ',\
: Enable_starttls_auto => true,: openssl_verify_mode => 'none '}
Mail. defaults {delivery_method: smtp, smtp}
Mail = Mail. new do
From 'wangyi @ 163.com'
To '2017 @ 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, the start of smtp user_name and from set in the mail account is not the same, resulting in always send failure, here are changed to the wangyi@163.com can be. But it can be different in method 1. In other two ways, if domain is changed to 'localhost', other values can also be sent successfully, as if there is no relationship between them.
The last two methods are non-encrypted connections, that is, the mail server allows non-ssl connections. If the mail server can only encrypt connections to the above Code, it cannot be used. As for how to write code at this time, if you know all the children's shoes, please don't hesitate to give me some advice.