Rails actionmailer打开主机电子邮件应用程序 [英] Rails actionmailer open host email app

查看:86
本文介绍了Rails actionmailer打开主机电子邮件应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From a link, I want to open up the user's default mail app, with an attachment attached, and the 'To' field empty, and some pre-populated text in the subject and body.

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

Using ActionMailer, I am able to get it to send from my gmail account, but it doesn't open up the default email app. Here are my settings in the 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可以做到吗?

Is there a way to do this dynamically, the way href mailto does?

href mailto标记会打开默认电子邮件,但我认为您不能添加附件。

The href mailto tag opens up the default email, but I don't think you can add attachments.

推荐答案

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

You can do that in few ways. One way is to define attachment directly:

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

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

And replace filename with your file, or you can use an inline defining of attachments like this:

或在您的邮件中定义:

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

You can find reference how to create attachments here: http://guides.rubyonrails.org/action_mailer_basics.html

但是如果您使用Heroku时可能会遇到一些困难-您可以在此处阅读有关内容: ActionMailer-如何添加附件?

But if you are using Heroku you could have some difficulties - you can read about that here: ActionMailer - How to Add an attachment?

编辑:

是,您的您输入的问题和代码使我感到困惑,真正的问题是什么。我现在相信您正在寻找以下内容:

Yes, your question and code you put, confused me what is the real question. I now believe you are looking for this:

mail_to "me@domain.com"

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

如果您要预填充正文文本,则可以在此处找到
使用mail_to进行阻止(mail_to ...做)

You can find something here, if you are trying to pre-populate body text Using mail_to with a block (mail_to ... do)

,然后在此处添加图像,也许您可以找到有用的东西
rails:在mail_to中包含html链接

and here to add image, maybe you can find something usefull rails: include html in a mail_to link

我刚刚从头开始放了一个代码,希望可以进行某种附加操作:

I have just put a code from top of my head, I hope some kind of attaching might work:

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

我还没有尝试过所有方法,但是我希望有什么用,或者指导您正确的方向

I haven't tried all this, but I hope something will work, or direct you in good direction

这篇关于Rails actionmailer打开主机电子邮件应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆