【问题标题】:How do I send mail from a Ruby program?如何从 Ruby 程序发送邮件?
【发布时间】:2023-03-03 21:29:01
【问题描述】:

我想从 Ruby 应用程序发送电子邮件。是否有核心语言调用来执行此操作,或者是否有我应该使用的库?最好的方法是什么?

【问题讨论】:

    标签: ruby email


    【解决方案1】:

    如果您不想使用ActionMailer,您可以使用Net::SMTP(用于实际发送)和tmail 一起轻松创建电子邮件(包含多个部分等)。

    【讨论】:

      【解决方案2】:

      我知道这是一个迟到的答案,但这是刚刚发布的:

      http://adam.blog.heroku.com/past/2008/11/2/pony_the_express_way_to_send_email_from_ruby/

      可能有用。

      【讨论】:

        【解决方案3】:

        RubyMail 是 Ruby 的电子邮件处理库。

        【讨论】:

          【解决方案4】:
          require 'net/smtp'
          SMTP_SERVER = 'mailserver01' #change to your server
          
          def send_emails(sender_address, recipients, subject, message_body)
              recipients.each do |recipient_address|
                  message_header =''
                  message_header << "From: <#{sender_address}>\r\n"
                  message_header << "To: <#{recipient_address}>\r\n"
                  message_header << "Subject: #{subject}\r\n"
                  message_header << "Date: " + Time.now.to_s + "\r\n"
                  message = message_header + "\r\n" + message_body + "\r\n"
                  Net::SMTP.start(SMTP_SERVER, 25) do |smtp|
                      smtp.send_message message, sender_address, recipient_address
                  end
              end
          end
          send_emails('sender@domain.com',['recip1@test.com', 'recip2@other.com'],'test Email',"Hi there this is a test email hope you like it")
          

          【讨论】:

            【解决方案5】:

            您也可以考虑查看 ActionMailer 组件,它是 Rails 的一部分,但不依赖于 Rails。

            【讨论】:

              【解决方案6】:

              我使用Net::SMTP

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-11-06
                • 2010-09-23
                • 2020-01-20
                • 2015-06-28
                • 1970-01-01
                • 2023-03-30
                • 2013-03-13
                • 2011-04-21
                相关资源
                最近更新 更多