action mailer的简单实用方法(带附件邮件)

本文介绍如何使用ActionMailer在Rails应用中发送包含图片附件的电子邮件,并配置SMTP服务器进行邮件发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

action mailer的简单实用方法

看过几个博客,描述不太清楚,多次试验终于可以用了,我的actionmailer版本是4.1.0

class UserMailer < ActionMailer::Base
  def send_mail(email)
    attachments['a.jpg'] = File.read(Rails.root.join('public','uploadfile','a.jpg'))
    mail(
        :subject => '测试',#主题
          :to => email,#接收人
          :from => '******@163.com',#发送邮箱
          :date => Time.now#发送时间
      )
      # encoded_content = File.read(Rails.root.join('public','uploadfile','a.jpg'))
      # attachments['a.jpg'] = {mime_type:'application/x-gzip',
      #                           encoding:'SpecialEncoding',
      #                          content:encoded_content}
  end
end

在config/environments中需做如下配置

 ActionMailer::Base.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  #config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"
  config.action_mailer.default_url_options = { :host => '172.**.**.**:3000'}
  ActionMailer::Base.smtp_settings = {
      :address => "smtp.163.com",
      :port => 25,
      :domain => "163.com",
      :authentication => :login,
      :user_name => "******@163.com",
      :password => "****123"
  }
如此之后,可在其他控制方法中调用邮件发送方法

UserMailer.send_mail(@full_text_record.email).deliver



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值