ruby 发送post请求_使用Ruby发送电子邮件

ruby 发送post请求

Ruby发送电子邮件 (Ruby sending email)

Sending emails and routing email among mail servers are handled by Simple Mail Transfer Protocol commonly known as SMTP. Net::SMTP class is a predefined class in Ruby’s library which is purposefully defined for Simple Mail Transfer Protocol.

在邮件服务器之间发送电子邮件和路由电子邮件由通常称为SMTP的 简单邮件传输协议处理。 Net :: SMTP类是Ruby库中的预定义类,它是为简单邮件传输协议专门定义的。

Net::SMTP has two methods namely new and start. The parameter specification goes as follows,

Net :: SMTP有两种方法,即new和start。 参数规格如下:

新: (new:)

It consumes two parameters, the first one is a server which is by default 'localhost' and the port number which is by default 25.

它使用两个参数,第一个是默认为“ localhost”的服务器,默认为25的端口号。

开始: (start:)

It consumes six parameters namely server, port, domain, account, password, and authtype. The default value of server is localhost, the port is 25, the domain is ENV["HOSTNAME"], account and password is nil and authtype is cram_md5.

它使用六个参数,即服务器,端口,域,帐户,密码和身份验证类型。 服务器的默认值为localhost,端口为25,域为ENV [“ HOSTNAME”],帐户和密码为nil,身份验证类型为cram_md5。

sendmail is a method of an SMTP object which takes three parameters namely The source, The sender and the recipient.

sendmail是SMTP对象的一种方法,它采用三个参数,即源,发件人和收件人。

Now, let us understand a very simple way to send one email using Ruby code. refer the example given below,

现在,让我们了解一种使用Ruby代码发送一封电子邮件的非常简单的方法。 请参考下面的示例,

require 'net/smtp'

message = <<MESSAGE_END
From: Includehelp <[email protected]>
To: Hrithik sinha <[email protected]>
Subject: Urgent Information

Hello there, Greeting from Includehelp.
We hope, you are doing good. Have a great day.
Read articles at Includhelp for best programming guidance.
MESSAGE_END

Net::SMTP.start('localhost') do |smtp|
  smtp.send_message message, '[email protected]', '[email protected]'
end

The code given above is a very basic way to send email using Ruby. We have integrated a very simple email message. It is required to take care of the format of headers properly. We all know that an email requires three things namely a From, To and Subject which should be identical from the body of an email with the help of a blank line.

上面给出的代码是使用Ruby发送电子邮件的一种非常基本的方法。 我们已经集成了非常简单的电子邮件。 需要正确处理标题格式。 我们都知道,电子邮件需要三项内容,即“发件人”,“收件人”和“主题”,在空白行的帮助下,它们应与电子邮件正文相同。

For sending an email, we are supposed to use Net::SMTP class which allows us to connect with the SMTP server on our machine and then make use of send_message method with the message specified, the sender’s address and the receiver’s address as parameters.

为了发送电子邮件,我们应该使用Net :: SMTP类,该类允许我们与计算机上的SMTP服务器连接,然后使用send_message方法,并将指定的消息,发送者的地址和接收者的地址作为参数。

If you want to send an HTML file using Ruby script, that file will be treated as a normal document even after including HTML tags in the documents. Net::SMTP class provides you a feature to send an HTML message with the help of an email. Refer the following code for instance.

如果要使用Ruby脚本发送HTML文件,即使在文档中包含HTML标记后,该文件也将被视为普通文档。 Net :: SMTP类为您提供了借助电子邮件发送HTML消息的功能。 例如,请参考以下代码。

require 'net/smtp'

message = <<MESSAGE_END
From: Hrihik Sinha <[email protected]>
To: Saksham Sharma <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: An exemplary email

hey bro,

嘿哥们儿,

This is an example. Read articles at Includehelp.com for better understanding of concepts.

这是一个例子。 阅读Includehelp.com上的文章,以更好地理解概念。

<h1>Write heading here</h1>
<p>This is a paragraph</p>
<hr>
MESSAGE_END

Net::SMTP.start('localhost') do |smtp|
   smtp.send_message message, '[email protected]', '[email protected]'
end

You are supposed to specify Mime version, the type of content and character set if you want to email an HTML text using Ruby script.

如果要使用Ruby脚本通过电子邮件发送HTML文本,则应指定Mime版本,内容类型和字符集。

翻译自: https://www.includehelp.com/ruby/sending-email-using-ruby.aspx

ruby 发送post请求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值