send mail with attachment ruby - ruby

I want send mail with an attachment file. I need have then format body text in html, when configure the format, the file attachment sent bad in the mail.
My code using actionMailer:
#require 'mail'
class MyMail < ActionMailer::Base
default :from => 'cristian.gonzalez#powersystem.com.ar'
def send_mail(user,pdffile_name,pdffile_name_second,pdffile_name_thir,htmlfile_name)
#sendgrid_unique_args :key1 => user.id
email_with_name = "#{user.send_user} <#{user.mail_address}>"
#Adjuntar el archivo 1
unless pdffile_name.nil?
filename = user.first_attachment.partition("/")[2]
attachments[filename] = File.open(pdffile_name, 'rb', &:read)
end
mail :to => 'cdgonzalez82#gmail.com',
:subject = user.mail_subject,
:content_type = 'text/html; charset=UTF-8',
:email.body = user.mail_body
end
end
Any help please???

Related

Display content on PM::WebScreen from BubbleWrap response

class WorkoutScreen < PM::WebScreen
title "Workouts"
def content
#response
end
def on_load
set_nav_bar_button :left, title: "Menu", action: :nav_left_button
end
def load_started
#response = ''
BubbleWrap::HTTP.get("my_url", {async: false, :headers => { "User-Agent" => "value"}}) do |response|
#response = response.body.to_str
end
end
def nav_left_button
app_delegate.menu.show(:left)
end
end
I need to send HTTP request with specific header, but content always nil . I have checked response by sniffer - everything is Ok.
If I do this way
class WorkoutScreen < PM::WebScreen
title "Workouts"
def content
#response = ''
BubbleWrap::HTTP.get("my_url", {async: false, :headers => { "User-Agent" => "value"}}) do |response|
#response = response.body.to_str
end
#response
end
I see
eb_screen_module.rb:50:in `set_content:': Is a directory - read() failed (Errno::EISDIR)
exception
NSUserDefaults.standardUserDefaults.registerDefaults({UserAgent: "value"})
Found solution myself

Receiving RequestError when using the twilio-ruby gem

I am try to use the twilio-ruby gem, but got a Twilio::REST::RequestError. What does this mean? Here's the code I'm using:
Controller
Class UserController < ApplicationController
def new
#user = User.new
end
def createUser
#user = User.new(user_params)
if #user.save
render text: "Thank you! You will receive sms notification"
account_sid = '*****'
auth_token = '*****'
#client = Twilio::REST::Client.new account_sid, auth_token
##client = Twilio::REST::Client.new account_sid, auth_token
#client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
# Create and send an SMS message
#client.account.messages.create
({
:from => '+127*****',
:to => #user.phone,
:body => "Hello"
})
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :phone)
end
end
Why is this generating an error?
A RequestError means that we couldn't send the SMS message. It may mean you don't have international permissions to send to the number in question, or you are trying to use a Caller ID for a phone number that you don't own, or you're trying to send to a landline, or any number of problems.
Here is an example of how to catch a RequestError and view the attached error message.
require 'twilio-ruby'
begin
client = Twilio::REST::Client.new account_sid, auth_token
client.account.sms.messages.create(
from => from_number,
to => to_number,
body => "Hello World"
)
rescue Twilio::REST::RequestError => e
puts e.message
end

Sending multipart mails and attachments

I am trying to send an E-Mail using the gem 'mail' with Ruby 1.9.3. It contains an text/html and an text/plain part which should be embedded as alternative parts as well an attachment.
This is my current code:
require 'mail'
mail = Mail.new
mail.delivery_method :sendmail
mail.sender = "me#example.com"
mail.to = "someguy#example.com"
mail.subject = "Multipart Test"
mail.content_type = "multipart/mixed"
html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body "<h1>HTML</h1>"
end
text_part = Mail::Part.new do
body "TEXT"
end
mail.part :content_type => "multipart/alternative" do |p|
p.html_part = html_part
p.text_part = text_part
end
mail.add_file :filename => "file.txt", :content => "FILE"
mail.deliver!
It results in an mail with working alternative parts but no attachment. I am using thunderbird 10.0.12 for testing.
I already posted this on github, but unfortunately the posts don't make me smarter. https://github.com/mikel/mail/issues/118#issuecomment-12276876. Maybe somebody is able to understand the last post a bit better than me ;)
Is somebody able to get this example working?
Thanks,
krissi
I managed to fix it like so:
html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body html
end
text_part = Mail::Part.new do
body text
end
mail.part :content_type => "multipart/alternative" do |p|
p.html_part = html_part
p.text_part = text_part
end
mail.attachments['some.xml'] = {content: Base64.encode64(theXML), transfer_encoding: :base64}
mail.attachments['some.pdf'] = thePDF
mail.content_type = mail.content_type.gsub('alternative', 'mixed')
mail.charset= 'UTF-8'
mail.content_transfer_encoding = 'quoted-printable'
Not intuitive at all, but reading the Pony source code kinda helped, as well as comparing a working .eml to whatever this gem was generating.
This seems to be a bug regarding the content type of the attachment. See https://github.com/mikel/mail/issues/522

How to POST binary data using Rest Client?

I am trying to read in an audio file of type wav or amr from a HTML form using Rest Client. I have the code to do this in PHP.
$filename = $_FILES['f1']['name'];
public function getFile($filename) {
if (file_exists($filename)) {
$file_binary = fread(fopen($filename, "r"), filesize($filename));
return $file_binary;
} else {
throw new Exception("File not found.");
}
}
I need to convert this code to Ruby and I am having trouble doing so as I am a relative novice when it comes to Ruby.
According to RestClient's repo:
def self.post(url, payload, headers={}, &block)
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
end
This snippet of code simply sends a file:
file = File.open('path/to/file.extension', 'r')
RestClient.post("your_url_to_the_endpoint", file)
So I assume all you still need to do is to set the headers:
begin
file = File.open(params[:f1], "rb")
url = "...."
response = RestClient.post url, file, {:Authorization => "Bearer #{#access_token}", :Accept => 'application/json', :Content_Type => 'audio/wav'}
rescue => e
#error = e.message
ensure
return erb :speech
end

HTTParty authentication problem

I am trying to log in with HTTParty.
I followed the instruction and still can't get it to work.
require 'rubygems'
require 'httparty'
class LAShowRoom
include HTTParty
base_uri 'www.lashowroom.com'
#debug_output
def initialize email, password
#email = email
response = self.class.get('/login.php')
response = self.class.post(
'/login.php',
:body => { :login_id => email, :login_key => password, :submit_login => 'Log In' },
:headers => {'Cookie' => response.headers['Set-Cookie']}
)
#response = response
#cookie = response.request.options[:headers]['Cookie']
end
def login_response
#response
end
def welcome_page
self.class.get("/announce.php", :headers => {'Cookie' => #cookie})
end
def logged_in?
welcome_page.include? "Kevin"
end
end
la_show_room = LAShowRoom.new('my_email', 'my_password')
puts "Logged in: #{la_show_room.logged_in?}"
As far as I know, HTTParty handles https automatically.
Am I missing something?
Thanks.
Sam
Yes, HTTParty handles HTTPS automatically, but you still need to declare HTTPS. Try
base_uri 'https://…'
How else is HTTParty supposed to know? ;-)

Resources