- Ruby 基础
- Ruby - 首页
- Ruby - 概述
- Ruby - 环境设置
- Ruby - 语法
- Ruby - 类和对象
- Ruby - 变量
- Ruby - 运算符
- Ruby - 注释
- Ruby - IF...ELSE
- Ruby - 循环
- Ruby - 方法
- Ruby - 块
- Ruby - 模块
- Ruby - 字符串
- Ruby - 数组
- Ruby - 哈希表
- Ruby - 日期和时间
- Ruby - 范围
- Ruby - 迭代器
- Ruby - 文件 I/O
- Ruby - 异常
使用 Ruby 发送电子邮件 - SMTP
简单邮件传输协议 (SMTP) 是一种协议,用于处理电子邮件的发送和邮件服务器之间的电子邮件路由。
Ruby 提供了 Net::SMTP 类用于简单邮件传输协议 (SMTP) 的客户端连接,并提供了两个类方法 new 和 start。
new 方法接受两个参数:
服务器名称,默认为 localhost。
端口号,默认为众所周知的端口 25。
start 方法接受以下参数:
服务器 - SMTP 服务器的 IP 名称,默认为 localhost。
端口 - 端口号,默认为 25。
域名 - 邮件发送者的域名,默认为 ENV["HOSTNAME"]。
账户 - 用户名,默认为 nil。
密码 - 用户密码,默认为 nil。
authtype - 授权类型,默认为 cram_md5。
SMTP 对象有一个名为 sendmail 的实例方法,通常用于执行邮件发送任务。它接受三个参数:
源 - 字符串或数组,或任何具有 each 迭代器并一次返回一个字符串的对象。
发送者 - 将出现在电子邮件 发件人 字段中的字符串。
收件人 - 表示收件人地址的字符串或字符串数组。
示例
这是一个使用 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
这里,你将基本的电子邮件放在 message 变量中,使用文档,注意正确格式化标题。电子邮件需要发件人、收件人和主题标题,并用空行与电子邮件正文分开。
要发送邮件,你可以使用 Net::SMTP 连接到本地机器上的 SMTP 服务器,然后使用 send_message 方法以及邮件、发件人地址和目标地址作为参数(即使发件人和收件人地址在电子邮件本身中,这些地址并不总是用于邮件路由)。
如果你的机器上没有运行 SMTP 服务器,你可以使用 Net::SMTP 与远程 SMTP 服务器通信。除非你使用的是网络邮件服务(例如 Hotmail 或 Yahoo!邮箱),否则你的电子邮件提供商会提供你可以提供给 Net::SMTP 的外发邮件服务器详细信息,如下所示:
Net::SMTP.start('mail.your-domain.com')
这行代码连接到 mail.your-domain.com 端口 25 上的 SMTP 服务器,不使用任何用户名或密码。但是,如果需要,你可以指定端口号和其他详细信息。例如:
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。然后可以在边界内指定文本和附件部分。
边界以两个连字符开头,后跟一个唯一的数字,该数字不能出现在电子邮件的消息部分中。表示电子邮件最终部分的最终边界也必须以两个连字符结尾。
附加文件应使用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
注意 - 你可以在数组内指定多个目标,但它们应该用逗号隔开。