Sending email with Ruby through mutt - ruby

I'm trying to send an email in this way:
path_to_file="/home/username/tmp/filename.html"
subject="My Subject"
to.each do |address|
cmd = "`echo \"#{body}\" | mutt -s \"#{subject}\" #{address} -a #{path_to_file}`"
system(cmd)
end
end
This is running on a unix machine, the file has an html extenxion and contains html code.
I receive the file correctly, with the same filename and extension, the problem is that is completely empty, this is strange because when i run the same command (mutt) from the terminal, applying the same path to file:
echo "body here" | mutt -s "some subject" mamail#mail.com -a ${path_to_file}
Then it works just fine.
Any idea?

Related

How to send mail with attachment using shell

I am trying to sending mail with attachment using shell script, but I am not getting mail.
Shell Script
#!/bin/bash
$ echo "Mail Sent" | mail -s "a subject" -a "Report_by_Customer_20190614_131246.xls" test#gmail.com
Use mutt. Add list of files that you want to attach
echo "body" | mutt -s "subject" -a attachment0 attachment1 [...] -- test#gmail.com
Also here you can find another options

unix, email attachment not being attached

I am trying to send an attachment via a script and the logic works if i run manually but not as part of the script. if I add echo, the email goes out and populates with uuencode jef20.txt jef20.txt but no attachment. any ideas?
#!/bin/bash
echo Your Username?
read user
echo FIX Session?
read session
echo what client?
read client
awk '!/35=0|35=A|35=5|35=2|35=1|closed/' /company/gate/app_phoenix/logs/fix/$session >> /home/dnash/$client.txt
uuencode $client.txt $client.txt | mailx -s "Cert Logs" "$user#company.com"
Depending on your version of mailx , you can use the -a ( "attach" ) command line switch:
mailx -a "$client.txt" -s "Cert Logs" "$user#company.com"
See Also
mailx manpage
how to send an email via mailx with enclosed file

mailx: "send-mail: illegal option -- a" while attempting to send file attachment

Our production server has been retired and now we are using a hosted system running Redhat GNU/Linux.
We had many scripts using mutt to send file attachments but they're failing now since mutt is not installed on our servers (The sysadmin policy is that mutt is not secure so it will not be installed)
I have tried using mailx but to no avail. When I do
echo "this is my email body"| mailx -s "this is my email subject" "email#xyz.com" -a "filename.csv"
I get
$ send-mail: illegal option -- a
"filename.csv" exist and it is local to the directory I run the command from. Of course, when I do
mailx -s "this is my email subject" "email#xyz.com" < "filename.csv"
It works but it embeds the file attachment in the email body. Users do not want that.
What am I doing wrong?
I figured it out. I simply moved the -a flag before the email address like so
echo "this is my email body"| mailx -s "this is my email subject" -a "filename.csv" "email#xyz.com"
It worked just fine.
uuencode filename.csv filename.csv | mailx -s "this is my email subject" "email#xyz.com"
Or, if you want to clump text and an attachment together, then
echo "this is my email body" | cat -<(echo uuencode filename.csv filename.csv) | mailx -s "this is my email subject" "email#xyz.com"
I came across the same problem, use -A instead of -a. It worked for me.

Sending the mail using mutt command

I have a requirement where I need to send two files A and B . The file A's content should be displayed as in-line or body of the mail and file B as an attachment.
Is multiple attachment using mutt is possible?
The command
echo "Hello everyone " | mutt -s 'My mail ' abc#gmail.com -a myFile.txt
is writing Hello everyone as body of the mail and myFile.txt as an attachment(inline).
Both of my files A and B are dynamically generated, so I cannot have an echo statement.
It's actually very simple:
mutt -s 'My mail ' abc#gmail.com -a report1.txt < report2.txt
If you had two scripts to create the reports, you could use pipes (i.e. no files would be created on disk):
script2 | mutt -s 'My mail ' abc#gmail.com -a <(script1)
cat A | mutt -s 'My mail' abc#gmail.com -a B
If the shell script prints file A's content to standard output, like this:
script >A
then you can use tee to print both to the file A and into the pipe to mutt:
script | tee A | mutt -s 'My mail' abc#gmail.com -a B

Evaluating a variable while calling System in Ruby

I want to actually call the following in Ruby
echo "<html><body><b>BOLD</b></body></html>" | mutt -e "set content_type=text/html" -s "HTML content" -- anushka.misra2#gmail.com
I was successfully able to do it in Ruby with system() method:
system 'echo "<html><body><b>BOLD</b></body></html>" | mutt -e "set content_type=text/html" -s "HTML content" -- anushka.misra2#gmail.com'
Now, I want email to be a variable:
email = anushka.misra2#gmail.com
system 'echo "<html><body><b>BOLD</b></body></html>" | mutt -e "set content_type=text/html" -s "HTML content" -- $email'
But this fails as it doesn't evaluate email variable. How do I fix it?
system %Q{echo "<html><body><b>BOLD</b></body></html>" | mutt -e "set content_type=text/html" -s "HTML content" -- #{email}}
#{foo} is the Ruby way to add a value into a string. #{foo} is only evaluated in strings with " (not '). Where %Q{} does the same, but you do not have to escape " inside the string.

Resources