【问题标题】:Rails actionmailer open host email appRails actionmailer 打开主机电子邮件应用程序
【发布时间】:2013-02-12 19:13:24
【问题描述】:

通过一个链接,我想打开用户的默认邮件应用程序,附加一个附件,“收件人”字段为空,主题和正文中有一些预填充的文本。

使用 ActionMailer,我可以让它从我的 gmail 帐户发送,但它不会打开默认的电子邮件应用程序。这是我在 ActionMailer::Base.smtp_settings 中的设置:

ActionMailer::Base.smtp_settings = {


    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => "gmail.com",
    :user_name            => "myname",
    :password             => "mypassword",
    :authentication       => "plain",
    :enable_starttls_auto => true
}

有没有办法像 href mailto 那样动态地做到这一点?

href mailto 标签打开了默认邮件,但我不认为你可以添加附件。

【问题讨论】:

  • 网络不支持。

标签: ruby-on-rails actionmailer


【解决方案1】:

您可以通过几种方式做到这一点。一种方法是直接定义附件:

encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
                               :encoding => 'SpecialEncoding',
                               :content => encoded_content }

并用您的文件替换文件名,或者您可以使用这样的附件的内联定义:

或者在你的邮件中定义:

def welcome
  attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
end

在视图中:

<p>Hello there, this is our image</p>

<%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
                                            :class => 'photos' %>

您可以在此处找到如何创建附件的参考:http://guides.rubyonrails.org/action_mailer_basics.html

但如果你使用 Heroku,你可能会遇到一些困难 - 你可以在这里阅读:ActionMailer - How to Add an attachment?

编辑:

是的,您提出的问题和代码让我感到困惑,真正的问题是什么。我现在相信您正在寻找这个:

mail_to "me@domain.com"

寻找参考:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-mail_to

如果您尝试预填充正文,可以在此处找到一些内容 Using mail_to with a block (mail_to ... do)

在这里添加图片,也许你能找到一些有用的东西 rails: include html in a mail_to link

我刚刚从头顶上放了一个代码,我希望某种附加方式可以工作:

<%= mail_to("you@gmail.com", image_tag("email.gif")) %>

我还没有尝试过所有这些,但我希望有些东西会起作用,或者指导你朝着好的方向发展

【讨论】:

  • 谢谢,我想主要问题是,我如何打开用户的默认电子邮件应用程序,并填写一些字段,就像 href mailto 那样?谢谢
  • 谢谢,一个问题让我有点困惑,当我看到粘贴的代码时 :) 我已经更新了答案,希望对您有所帮助
猜你喜欢
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2014-12-31
  • 2011-08-11
相关资源
最近更新 更多