Ruby's instance program code for sending mail

Source: Internet
Author: User
Tags require sendfile

Example 1

Require ' NET/SMTP '

Require ' iconv '


Def send_email (from, From_alias, to, To_alias, subject, content)

Subject_n = Iconv.conv (' utf-8 ', ' GBK ', subject)

msg = <<message_end

From: #{from_alias} <#{from}>

To: #{to_alias} <#{to}>

mime-version:1.0

Content-type:text/html;charset=utf-8

Subject: #{subject_n}


#{content}

Message_end

Net::smtp.start (' smtp.qq.com ',, ' qq.com ', #{qq_num}, #{passwd},: Login) do |smtp| #此处配置发送服

Service and Account

Smtp.send_message msg, from, to

End

End

Example 2

Ruby sends messages with NET::SMTP, but it does not support sending attachments directly. may be achieved by mailfactory this gem.

Install Mailfactory

Gem Install Mailfactory
Using the Mailfactory sample


Mail=mailfactory.new

mail.to=[' a@rubyer.me ', ' B@rubyer.me].join (', ') #多个收件人

Mail.from= ' from@rubyer.me '

Mail.subject= ' This is the subject '

Mail.html= ' &lt;/font color= ' red ' &gt; Here is the HTML conternt&lt;/font&gt; '

Mail.text= ' Please use HTML view '

Mail.attach ('/usr/local/test.file ')

Net::smtp.start (@smtp_host) do |smtp|

Smtp.send_message (mail.to_s (), from,to)

End

If found to have Chinese garbled


Create a new sendfile.rb file that enables you to send mail under the shell.


#!/usr/bin/env Ruby

Require ' NET/SMTP '

Require ' RubyGems '

Require ' mailfactory '

Def sendmail (to, subject, text, file)

Mail = mailfactory.new

mail.from= "localhost"

Mail.subject=subject

Mail.text=text

Mail.attach (file);

Mail.to = To

Net::smtp.start ("localhost") do |smtp|

Smtp.send_mail (mail.to_s (), "localhost", to)

End

End

&nbsp;

if (argv.length &lt; 4)

Puts "You should to use like this:sendFile.rb ' to_addr ' subject '" ' Text ' filepath ' "

Else

If File.file? ARGV[3]

SendMail (Argv[0], argv[1], argv[2], argv[3]

Puts "SendMail to #{argv[0]}, #{argv[1]}, #{argv[2]}, #{argv[3]}";

Else

Puts "File not exist:" + argv[3]

End

End

To add executable permissions to a file in a Linux environment

chmod +x sendfile.rb
Execute when sending mail

Sendfile.rb "To@rubyer.me" "title of the Mail" "Hello World" "/home/oldsong/test.file"

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.