Net: SMTP is easy to send emails, but it does not support sending attachments directly. You can install mailfactory to implement this function.
1) install mailfactory
Run: Gem install mailfactory
2) supports Chinese
The mailfactory installed by default does not support Chinese characters. You can modify itSource codeTo solve this problem, open the downloaded mailfactory. RB, find the "def initialize ()" method, change the character encoding from UTF-8 to GBK
@ Charset = 'gbk'
In this way, the Chinese characters in the email can be correctly displayed.
3)Code(Sendmail. RB)
Require 'net/SMTP'
Require 'rubygems'
Require 'mailfactory'
Mail = mailfactory. New ()
# With join (', "), the recipient in the mailbox to see the recipient list: xx1 <xx1@qq.com>; xx2 <xx2@qq.com>
# Otherwise you will see: xx1@qq.comxx2 @ QQ.com, there is no comma separated between users
Mail. To = [xx1@qq.com ', xx2@qq.com']. Join (',')
Mail. From = "send@163.com"
Mail. Subject = "Ruby sends email attachments to test! "
Mail. Text = "can you successfully receive emails and attachments with garbled characters? "
Export mail.html = "simple test <B> Chinese </B> for garbled characters"
Mail. Attach ("D: // script // Ruby // test attachment .txt ")
Mail. Attach ("D: // script // Ruby // Chinese Normal abc.doc ")
# Declare another recipient's address list
To = ['2017 @ QQ.com ', '2017 @ QQ.com']
Net: SMTP. Start ('smtp .163.com ', 25, '192. com', 'tesge @ 163.com', 'password XXX',: Plain) {| SMTP |
# Mail. To = toaddress
SMTP. send_message (mail. to_s (), send@163.com ',['Xx1 @ QQ.com ', 'xx2 @ QQ.com'])
}
The red part indicates the address of the recipient. You cannot directly use the "mail. To" parameter here. Otherwise, the email fails to be sent and an error "550, invalid user" is generated. The "to" variable declared above can also be run successfully.