Use the ruby mailfactory class to send attachments

Source: Internet
Author: User

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.

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.