问题
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.
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
}
Is there a way to do this dynamically, the way href mailto does?
The href mailto tag opens up the default email, but I don't think you can add attachments.
回答1:
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:
Or define in you mailer:
def welcome
attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
end
And in the view:
<p>Hello there, this is our image</p>
<%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
:class => 'photos' %>
You can find reference how to create attachments here: http://guides.rubyonrails.org/action_mailer_basics.html
But if you are using Heroku you could have some difficulties - you can read about that here: ActionMailer - How to Add an attachment?
EDIT:
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"
Look for reference: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-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)
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
来源:https://stackoverflow.com/questions/15104313/rails-actionmailer-open-host-email-app