Ruby 专题
您的位置:Ruby > Ruby专题 > Ruby 发送邮件 – SMTP
Ruby 发送邮件 – SMTP
作者:--    发布时间:2019-11-20

smtp(simple mail transfer protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。

ruby提供了 net::smtp 来发送邮件,并提供了两个方法 new 和 start:

  • new 方法有两个参数:
    • server name 默认为 localhost
    • port number 默认为 25
  • start 方法有以下参数:
    • server - smtp 服务器 ip, 默认为 localhost
    • port - 端口号,默认为 25
    • domain - 邮件发送者域名,默认为 env["hostname"]
    • account - 用户名,默认为 nil
    • password - 用户密码,默认为nil
    • authtype - 验证类型,默认为 cram_md5

smtp 对象实例化方法调用了 sendmail, 参数如下:

  • source - 一个字符串或数组或每个迭代器在任一时间中返回的任何东西。
  • sender -一个字符串,出现在 email 的表单字段。
  • 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

在以上实例中,你已经设置了一个基本的电子邮件消息,注意正确的标题格式。一个电子邮件要要from,to和subject,文本内容与头部信息间需要一个空行。

使用net::smtp连接到本地机器上的smtp服务器,使用send_message方法来发送邮件,方法参数为发送者邮件与接收者邮件。

如果你没有运行在本机上的smtp服务器,您可以使用net::smtp与远程smtp服务器进行通信。如果使用网络邮件服务(如hotmail或雅虎邮件),您的电子邮件提供者会为您提供发送邮件服务器的详细信息:

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

以上代码将连接主机为 mail.your-domain.com,端口号为 25的邮件服务器,如果需要填写用户名密码,则代码如下:

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

以上实例使用了指定的用户名密码连接到主机为 mail.your-domain.com,端口号为 25的邮件服务器。


使用 ruby 发送 html 邮件

net::smtp同样提供了支持发送 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。 这样就可以在邮件中添加附件内容。

附件在传输前需要使用 pack("m") 函数将其内容转为 base64 格式。

实例

以下实例将发送附件为 /tmp/test.txt 的邮件:

require 'net/smtp'

filename = "/tmp/test.txt"
# 读取文件并编码为base64格式
filecontent = file.read(filename)
encodedcontent = [filecontent].pack("m")   # base64

marker = "auniquemarker"

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

# 定义主要的头部信息
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

# 定义消息动作
part2 =<<eof
content-type: text/plain
content-transfer-encoding:8bit

#{body}
--#{marker}
eof

# 定义附件部分
part3 =<<eof
content-type: multipart/mixed; name=\"#{filename}\"
content-transfer-encoding:base64
content-disposition: attachment; filename="#{filename}"

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

mailtext = part1 + part2 + part3

# 发送邮件
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  

注意:你可以指定多个发送的地址,但需要使用逗号隔开。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册