当前位置: 移动技术网 > IT编程>脚本编程>Ruby > 在Ruby中利用Net::SMTP类发送电子邮件的教程

在Ruby中利用Net::SMTP类发送电子邮件的教程

2017年12月08日  | 移动技术网IT编程  | 我要评论

简单邮件传输协议(smtp)发送电子邮件及路由的e-mail邮件服务器之间的协议处理。

ruby 提供 net::smtp 类的简单邮件传输协议(smtp)客户端的连接,并提供了两个新的方法:new 和 start.

    new 带两个参数:

  1.         server name 默认为 localhost
  2.         port number  默认为熟知的 25

    start 方法带有以下这些参数:

  •         server - ip smtp服务器名称,默认为localhost
  •          port - 端口号,默认为25
  •         domain - 邮件发件人的域名,默认为 env["hostname"]
  •         account - 用户名,默认为 nil
  •         password - 用户密码,默认为 nil
  •         authtype - 授权类型,默认为 cram_md5

smtp对象有一个实例方法调用sendmail,后者通常会被用来做邮寄消息的工作。它有三个参数:

  •     source - 字符串或数组或任何同每个迭代一次返回一个字符串。
  •     sender - 一个字符串,它将会出现在电子邮件字段中。
  •     recipients - 代表收件人的地址的字符串的字符串或数组

例子:

下面是一个简单使用ruby脚本的方法来发送一个电子邮件。试一次看看吧:

require 'net/smtp'

message = <<message_end
from: private person <me@fromdomain.com>
to: a test user <test@todomain.com>
subject: smtp e-mail test

this is a test e-mail message.
message_end

net::smtp.start('localhost') do |smtp|
 smtp.send_message message, 'me@fromdomain.com', 
               'test@todomain.com'
end

在这里已经放置了一个基本的电子邮件消息,使用文件,在这里同时注意正确格式化标题。一封邮件需要发件人,收件人,主题头,从电子邮件的主体有一个空白行分隔。

随着消息要发送邮件使用net::smtp连接到本地机器上的smtp服务器,然后使用 send_message  方法,从地址,目的地址作为参数(即使地址电子邮件本身范围内,这些并非总是用来将邮件路由)。

如果还没有在机器上运行一个smtp服务器,可以使用的net::smtp远程smtp服务器进行通信。除非使用一个webmail服务(如hotmail或雅虎邮件),电子邮件提供商将提供外发邮件服务器的细节,可以提net::smtp,具体如下:

net::smtp.start('mail.your-domain.com')

这行代码连接到smtp服务器mail.your-domain.com 端口25。,而无需使用任何用户名或密码。不过,如果需要的话可以指定端口号或其他参数等。例如:

net::smtp.start('mail.your-domain.com', 
        25, 
        'localhost', 
        'username', 'password' :plain)

这个例子连接mail.your-domain.com到smtp服务器使用的用户名和密码以纯文本格式。它标识为localhost客户端的主机名。
使用ruby发送html电子邮件:

当想要发送文本消息,ruby所有内容将被视为简单的文本。即使会在短信中包含html标记,它会显示简单的文本和html标记将不会格式化html语法。但是ruby的 net::smtp 提供了实际的html邮件发送html消息的选项。

在发送电子邮件消息时,可以指定一个mime版本,内容类型和字符集发送html电子邮件。
例如:

下面的例子作为电子邮件发送html内容。试一次看看吧:

require 'net/smtp'

message = <<message_end
from: private person <me@fromdomain.com>
to: a test user <test@todomain.com>
mime-version: 1.0
content-type: text/html
subject: smtp e-mail test

this is an e-mail message to be sent in html format

<b>this is html message.</b>
<h1>this is headline.</h1>
message_end

net::smtp.start('localhost') do |smtp|
 smtp.send_message message, 'me@fromdomain.com', 
               'test@todomain.com'
end

作为电子邮件的附件发送:

混合内容发送一封电子邮件,要求设置content-type头为 multipart/mixed。然后,文本和附件部分可以指定 boundaries 范围内。

两个连字符后跟一个唯一的编号,不能出现在电子邮件的消息部分的边界开始。最后一个边界表示电子邮件的最后一节的结尾也必须有两个连字符。

附加文件使用 pack("m") 函数来base64编码传输之前进行编码。
例子:

下面的例子将文件 /tmp/test.txt 作为附件发送。试一次看看吧:

require 'net/smtp'

filename = "/tmp/test.txt"
# read a file and encode it into base64 format
filecontent = file.read(filename)
encodedcontent = [filecontent].pack("m")  # base64

marker = "auniquemarker"

body =<<eof
this is a test email to send an attachement.
eof

# define the main headers.
part1 =<<eof
from: private person <me@fromdomain.net>
to: a test user <test@todmain.com>
subject: sending attachement
mime-version: 1.0
content-type: multipart/mixed; boundary=#{marker}
--#{marker}
eof

# define the message action
part2 =<<eof
content-type: text/plain
content-transfer-encoding:8bit

#{body}
--#{marker}
eof

# define the attachment section
part3 =<<eof
content-type: multipart/mixed; name=\"#{filename}\"
content-transfer-encoding:base64
content-disposition: attachment; filename="#{filename}"

#{encodedcontent}
--#{marker}--
eof

mailtext = part1 + part2 + part3

# let's put our code in safe area
begin 
 net::smtp.start('localhost') do |smtp|
   smtp.sendmail(mailtext, 'me@fromdomain.net',
             ['test@todmain.com'])
 end
rescue exception => e 
 print "exception occured: " + e 
end 

注意:可以指定多个目的地内部数组,但他们需要用逗号分隔。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网