在Rails中使用ActionMailer发送带附件的邮件,可以使用attachments方法来添加附件。以下是一个示例代码:
class ExampleMailer < ActionMailer::Base
def send_email_with_attachment(email, attachment_path)
attachments["attachment.pdf"] = File.read(attachment_path)
mail(to: email, subject: 'Email with attachment')
end
end
在该示例中,send_email_with_attachment
方法接受收件人的email和附件的路径作为参数。attachments
方法用于添加附件,其中"attachment.pdf"是附件的文件名,File.read(attachment_path)
读取附件的内容。
调用示例:
ExampleMailer.send_email_with_attachment('recipient@example.com', '/path/to/attachment.pdf').deliver_now
使用上述代码示例,你可以发送带有附件的邮件。请确保替换收件人的email和附件的路径为实际的值。