如何用Ruby的邮件gem发送电子邮件?

时间:2021-01-29 18:12:25

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

我正在为Ruby https://github.com/mikel/mail使用邮件gem。

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?

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

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

github上的README只提供了由本地服务器发送的示例。

1 个解决方案

#1


88  

From http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

从http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

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:

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

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

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 .default,它允许我们为从现在开始创建的所有邮件对象设置全局传递方法。强大的用户提示,您不必使用全局方法,您可以直接在任何单个邮件::Message对象上定义delivery_method,并在每个电子邮件中具有不同的传递代理,如果您正在构建一个应用程序,该应用程序具有多个用户,且不同的服务器处理它们的电子邮件,那么这是非常有用的。

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

#1


88  

From http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

从http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

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:

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

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

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 .default,它允许我们为从现在开始创建的所有邮件对象设置全局传递方法。强大的用户提示,您不必使用全局方法,您可以直接在任何单个邮件::Message对象上定义delivery_method,并在每个电子邮件中具有不同的传递代理,如果您正在构建一个应用程序,该应用程序具有多个用户,且不同的服务器处理它们的电子邮件,那么这是非常有用的。

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