使用Ruby发送电子邮件 - SMTP

简单邮件传输协议(SMTP)是一种协议,用于处理在邮件服务器之间发送电子邮件和路由电子邮件.

Ruby为Simple Mail提供Net :: SMTP类传输协议(SMTP)客户端连接并提供两个类方法 new start .

  • 需要两个参数 :

    • 服务器名称默认为localhost.

    • 端口号默认为知名端口25./p>

  • 开始方法采用这些参数 :

    • 服务器 :  SMTP服务器的IP名称,默认为localhost.

    • port : 端口号,默认为25.

    • : 邮件发件人的域名,默认为ENV ["HOSTNAME"].

    • 帐户 : 用户名,默认为零.

    • 密码 : 用户密码,默认为零.

    • authtype : 授权类型,默认为 cram_md5 .

SMTP对象具有名为sendmail的实例方法,该方法通常用于执行邮件发送的工作.它需要三个参数 :

  • source : 一个字符串或数组或任何带有每个迭代器的东西,一次返回一个字符串.

  • 发件人 : 将出现在电子邮件的 from 字段中的字符串.

  • 收件人 : 表示收件人收件人的字符串或字符串数组.

示例

这是使用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方法以及消息,发件人地址和目标地址作为参数(即使from和to地址在电子邮件本身内,这些并不总是用于路由邮件).

如果你没有在你的机器上运行SMTP服务器,您可以使用Net :: SMTP与远程SMTP服务器进行通信.除非您使用的是Webmail服务(例如Hotmail或Yahoo! Mail),否则您的电子邮件提供商将为您提供可以提供给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-连接到SMTP服务器domain.com使用纯文本格式的用户名和密码.它将客户端的主机名标识为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


发送附件为电子邮件

要发送包含混合内容的电子邮件,需要将内容类型标头设置为 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 &plus; part2 &plus; 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: " &plus; e  
end


注意 : 您可以在数组中指定多个目标,但它们应以逗号分隔.