How to send email via smtp with Ruby's mail gem?

前端 未结 1 1243
逝去的感伤
逝去的感伤 2020-12-04 14:21

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

How do I send an email via an smtp server? How do I specify the address and port? And wha

相关标签:
1条回答
  • 2020-12-04 15:16

    From 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:

    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.deliver do
           to 'mikel@test.lindsaar.net'
         from 'ada@test.lindsaar.net'
      subject 'testing sendmail'
         body 'testing sendmail'
    end
    
    0 讨论(0)
提交回复
热议问题