代码之家  ›  专栏  ›  技术社区  ›  Max Williams

从rails控制台发送电子邮件

  •  50
  • Max Williams  · 技术社区  · 14 年前

    我正试图从我的生产服务器上的控制台发送一些邮件,但它们没有发出。我不明白为什么。我刚刚用sendmail设置了你的标准电子邮件。当我调用Mailer.deliver方法时,我得到了以下结果:

    #<TMail::Mail port=#<TMail::StringPort:id=0x3fe1c205dbcc> bodyport=#<TMail::StringPort:id=0x3fe1c2059e00>>
    

    编辑:添加了更多信息:

     Mailer.deliver_signup(@user, request.host_with_port, params[:user][:password])
    

    这个很好用。我想我应该可以在控制台上做同样的事情

    user = User.find(1)
    Mailer.deliver_signup(user, "mydomainname.com", "password")
    

    我在ubuntu服务器上以防万一。谢谢-麦克斯

    4 回复  |  直到 14 年前
        1
  •  37
  •   firedev    8 年前

    为了从Rails控制台发送电子邮件,首先我们必须在Console For action mailer设置中执行这个设置。

    ActionMailer::Base.delivery_method = :smtp 
    ActionMailer::Base.smtp_settings = {
      address: 'smtp.gmail.com', 
      port: 587, 
      domain: 'gmail.com',
      authentication: 'plain', 
      enable_starttls_auto: true, 
      user_name: 'your@gmail.com',
      password: 'yourpassword'
    }
    

    之后,如果我们执行电子邮件发送代码,它将发送电子邮件。

    UserMailer.activation_instructions(@user).deliver_now
    
        2
  •  125
  •   jmgarnier    6 年前

    ActionMailer::Base.mail(
      from: "test@example.co", 
      to: "valid.recipient@domain.com", 
      subject: "Test", 
      body: "Test"
    ).deliver_now
    
        3
  •  47
  •   sscirrus    13 年前

    今天早上,我在Rails 3应用程序上遇到了类似的问题,我打电话给:

    UserMailer.activation_instructions(@user)
    

    UserMailer.activation_instructions(@user).deliver
    

    这就成功了。希望这对你也有用!

        4
  •  0
  •   txwikinger    14 年前

    如果我理解你的意图,我就不是100%。

    如果您试图将电子邮件发送到Internet,则必须将sendmail配置为将这些电子邮件转发到适当的电子邮件服务器。根据你使用的Ubuntu版本,你可以重新配置包来完成这个任务。

    您可以使用重新配置电子邮件配置

    dpkg-reconfigure sendmail
    

    当然,如果你使用procmail包的话,就应该使用procmail。配置对话框提供了一些选项,您可以将其配置为将所有邮件转发到相应的电子邮件服务器。但是,您需要考虑是否需要身份验证,或者服务器是否只接受来自您服务器的电子邮件。

        5
  •  0
  •   Yoav Epstein    4 年前

    如果要发送附件

    mailer = ActionMailer::Base.new
    mailer.attachments["file.jpg"] = File.read("/dir/file.jpg")
    mailer.attachments["file.txt"] = "some text"
    mailer.mail(from: "me@example.com",
                to: "you@example.com",
                subject: "Email with attachments",
                body: "included the documents below\n\n")
    mailer.message.deliver
    

    mail 必须在附件之后,因为它会创建标题。