I am sending mail from a ruby script as shown below -
body = File.new "body.html","w"
body.puts "Result body"
cmd = "mutt -e \"set content_type=text/html\" -s \"Automation Result\" -a \"Result.xls\" -- xxx#yyy.com < body.html"
system(cmd)
it is sending the mail with attachment but with a blank body.
Strange thing is when I do
mutt -e "set content_type=text/html" -s "Automation Result" -a
"Result.xls" -- xxx#yyy.com < body.html
it sends the email with the attachment and html body. Any idea what's wrong and how to fix it?
Even
mail = Mail.new do
from 'automation_root#bidstalk.com'
to 'maitreya#bidstalk.com'
subject 'Hello'
body File.read('body.html')
add_file :filename => 'Result.xls', :content => File.read('Result.xls')
charset = "UTF-8"
end
is sending mails without body.
The mistake I was doing was I was opening the file, writing to it but not closing it before sending the mail so it was basically not saved and was 'null' when I was sending the mail.
just added -
body.close before the mutt statement and it worked fine.
Related
I use the code below and it's working fine.
dl=xyz#somebody.com
mail -t $dl << EOM
Subject: Report Status $(date)
Content-Type: text/html
mail text
EOM
However, when I try to get the mail id from file dl.txt, no mail is sent.
dl='/script/user/dl.txt'
mail -t $(echo cat $dl) << EOM
subject: Report Status $(date)
content-Type: text/html
mail text
EOM
Note: text file contains mail ids.
If you try to run
dl='/script/user/dl.txt'
echo cat $dl
The output will be
cat /script/user/dl.txt
Remove echo or better change code like this
to=$(cat '/script/user/dl.txt')
mail -t $to << EOM
Subject: Report Status $(date)
Content-Type: text/html
mail text
EOM
Save an email attachment automatically to map with qmail and reformime
I'm trying to move attachment automatically to another locatie with the dot-qmail file.
My .qmail file
#------------------------------------------------------------
| condredirect pdf-junkmail headermatch 'X-Spam-Status: Yes'
| reformime -X /bin/sh -c "if [ "\${FILENAME#*.}" == "pdf" ]; then cat > /home/users/name/home/$(date +%Y%m%d)_\$FILENAME; fi"
# Forward not set
# Vacation Message not set
./Maildir/
This works for a simple mail with one attachment. My questions:
How can I also move the mailmessage that belongs to this attachment to a mailbox named "done".
Above command doesn't work with multiple attachments in one mailmessage? How can I ajust this line to work for multiple attachments?
This doesn't work if the filename contains multiple dots like "how.areyou.pdf"
Thanks for the help
Here is a featured implementation for your problem.
First save and set permissions to this bash script for your user.
You will need to call it from your .qmail file:
extract-pdf-attachments.sh
#!/usr/bin/env bash
# This script process mail message attachments from stdin MIME message
# Extract all PDF files attachments
# and return the MIME message to stdout for further processing
# Ensure all locale settings are set to C, to prevent
# reformime from failing MIME headers decode with
# [unknown character set: ANSI_X3.4-1968]
# See: https://bugs.gentoo.org/304093
export LC_ALL=C LANG=C LANGUAGE=C
# Setting the destination path for saved attachments
attachements='/home/users/name/home'
trap 'rm -f -- "$mailmessage"' EXIT # Purge temporary mail message
# Create a temporary message file
mailmessage="$(mktemp)"
# Save stdin message to tempfile
cat > "$mailmessage"
# Iterate all MIME sections from the message
while read -r mime_section; do
# Get all section info headers
section_info="$(reformime -s "$mime_section" -i <"$mailmessage")"
# Parse the Content-Type header
content_type="$(grep 'content-type' <<<"$section_info" | cut -d ' ' -f 2-)"
# Parse the Content-Name header (if available)
content_name="$(grep 'content-name' <<<"$section_info" | cut -d ' ' -f 2-)"
# Decode the value of the Content-Name header
content_name="$(reformime -c UTF-8 -h "$content_name")"
if [[ $content_type = "application/pdf" || $content_name =~ .*\.[pP][dD][fF] ]]; then
# Attachment is a PDF
if [ -z "$content_name" ]; then
# The attachment has no name, so create a random name
content_name="$(mktemp --dry-run unnamed_XXXXXXXX.pdf)"
fi
# Prepend the date to the attachment filename
filename="$(date +%Y%m%d)_$content_name"
# Save the attachment to a file
reformime -s "$mime_section" -e <"$mailmessage" >"$attachements/$filename"
fi
done < <(reformime < "$mailmessage") # reformime list all mime sections
cat <"$mailmessage" # Re-inject the message to stdout for further processing
Then in you .qmail:
#------------------------------------------------------------
| condredirect pdf-junkmail headermatch 'X-Spam-Status: Yes'
| bash /path/to/extract-pdf-attachments.sh | condredirect done true
# Forward not set
# Vacation Message not set
./Maildir/
I wish to have a portable Unix way of programmatically sending an e-mail with an attachment. The "standard" Unix way of sending an e-mail seems to be mail(1), but there seems to be no portable way of specifying an attachment. (Yes, I know that some versions of mail(1) have an -a or an -A option, but that's not standard, so I cannot rely on it.)
I'll be using ruby for this, but I want a generic solution, i.e., a special ruby configuration involving (say) using Net::SMTP and specifying details like the SMTP server &c. should not be in the application, but the application should issue a command like mail <some other stuff here> and from there the system should deliver the mail (with a suitable ~/.mailrc or whatever).
mail command will work only if it can find a SMTP host. There is a number of ways it can be done (sSMTP, Postfix, sendmail). The most "normal" way is to have sendmail running on your computer, which means localhost itself is an SMTP server. I don't know of a way of getting the SMTP configuration automatically if you don't have sendmail installed.
Therefore, assuming you know the SMTP server (localhost in this example), from Tutorials Point a pure Ruby mail attachment:
require 'net/smtp'
filename = "/tmp/test.txt"
# Read a file and encode it into base64 format
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
marker = "AUNIQUEMARKER"
body = <<EOF
This is a test email to send an attachement.
EOF
# Define the main headers.
part1 = <<EOF
From: Private Person <me#fromdomain.net>
To: A Test User <test#todmain.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary = #{marker}
--#{marker}
EOF
# Define the message action
part2 = <<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
EOF
# Define the attachment section
part3 = <<EOF
Content-Type: multipart/mixed; name = \"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename = "#{filename}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
# Let's put our code in safe area
begin
Net::SMTP.start('localhost') do |smtp|
smtp.sendmail(mailtext, 'me#fromdomain.net', ['test#todmain.com'])
end
rescue Exception => e
print "Exception occured: " + e
end
I understood how I can send a HTML message with sendmail doing:
(
echo "From: me#example.com";
echo "To: you#example.com";
echo "Subject: this is my subject";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "My <b>HTML message<\b> goes here!";
) | sendmail -t
I also manages to send an attachement with mail doing
uuencode file.pdf file.pdf | mail -s "my subject" you#example.com
but I fail to send an HTML message with an attachement (.pdf).
Note that I failed to install mutt or mpack (using homebrew) so far so I would love a solution that works with mail, mailx or sendmail. I am on Mac OS X 10.11
What you need to do is use multipart mime.
Your Content-Type should be something like:
Content-Type: multipart/mixed; boundary=multipart-boundary
Your multipart-boundary can be any string you like.
Then you output a line "--multipart-boundary" followed by headers, then body for each part.
For example:
--multipart-boundary
Content-Type: text/html
My <b>HTML message<\b> goes here!
--multipart-boundary
Content-Type: application/pdf
Content-Disposition: attachment; filename=file.pdf
Content-Transfer-Encoding: base64
**cat your base64 encoded file here **
--multipart-boundary--
The extra two dashes at the end mark the end of last part. You can add as many attachments as you like.
Is it possible to send multiple attachments with uuencode and sendmail?
In a script I have a variable containing the files that need to be attached to a single e-mail like:
$attachments=attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf
Also a $template variable like:
$template="Subject: This is the subject
From: no-reply#domain.com
To: %s
Content-Type: text/plain
This is the body.
"
I have come up with:
printf "$template" "$recipient" |
sendmail -oi -t
Somewhere within this I must attach everything in the $attachments variable?
uuencode attachemnts and send via sendmail
Sending MIME attachemnts is better.
uuencode is simpler to implement in scripts but email some clients DO NOT support it.
attachments="attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf"
recipient='john.doe#example.net'
# () sub sub-shell should generate email headers and body for sendmail to send
(
# generate email headers and begin of the body asspecified by HERE document
cat - <<END
Subject: This is the subject
From: no-reply#domain.com
To: $recipient
Content-Type: text/plain
This is the body.
END
# generate/append uuencoded attachments
for attachment in $attachments ; do
uuencode $attachment $attachment
done
) | /usr/sbin/sendmail -i -- $recipient
For what its worth, mailx also works well.
mailx -s "Subject" -a attachment1 -a attachement2 -a attachment3 email.address#domain.com < /dev/null