如何使用 Ruby 的邮件 gem 通过 smtp 发送电子邮件? [英] How to send email via smtp with Ruby's mail gem?

查看:32
本文介绍了如何使用 Ruby 的邮件 gem 通过 smtp 发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mail gem for Ruby https://github.com/mikel/mail

I am using the mail gem for Ruby https://github.com/mikel/mail

如何通过 smtp 服务器发送电子邮件?如何指定地址和端口?我应该为 Gmail 使用哪些设置?

How do I send an email via an smtp server? How do I specify the address and port? And what settings should I use for Gmail?

github上的README只给出了本地服务器发送的例子.

The README on github only gives examples sending by a local server.

推荐答案

来自 http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

要通过 GMail 发送,您需要配置 Mail::SMTP 类以获得正确的值,因此要尝试此操作,请打开 IRB 并键入以下内容:

To send out via GMail, you need to configure the Mail::SMTP class to have the correct values, so to try this out, open up IRB and type the following:

require 'mail'

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => 'your.host.name',
            :user_name            => '<username>',
            :password             => '<password>',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }



Mail.defaults do
  delivery_method :smtp, options
end

最后一个块调用 Mail.defaults,它允许我们为从现在开始创建的所有邮件对象设置全局传递方法.高级用户提示,您不必使用全局方法,您可以直接在任何单独的 Mail::Message 对象上定义 delivery_method 并为每个电子邮件设置不同的传递代理,如果您是构建一个应用程序,让多个用户使用不同的服务器处理他们的电子邮件.

The last block calls Mail.defaults which allows us to set the global delivery method for all mail objects that get created from now on. Power user tip, you don’t have to use the global method, you can define the delivery_method directly on any individual Mail::Message object and have different delivery agents per email, this is useful if you are building an application that has multiple users with different servers handling their email.

Mail.deliver do
       to 'mikel@test.lindsaar.net'
     from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
     body 'testing sendmail'
end

这篇关于如何使用 Ruby 的邮件 gem 通过 smtp 发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆