On Apple Mail when I send an email with Base64 encoding (from a Perl script) the Apple Mail client is not decoding it's subject line i.e. it is showing raw base64 encoded text.
So when I am sending below from my Perl script:
Subject: =?UTF-8?B?IPCfjLkgVGhlIGVuY2hhbnRpbmcgc291bmR0cmFjayB0byAnQmVhdXR5IEFuZCBUaGUgQ mVhc3Qn
IGlzIG91dCB0b2RheSEg8J+MuQ==
?=
Instead of showing this:
The enchanting soundtrack to 'Beauty And The Beast' is out today!
It is showing below in subject line:
=?UTF-8?B?IPCfjLkgVGhlIGVuY2hhbnRpbmcgc291bmR0cmFjayB0byAnQmVhdXR5IEFuZCBUaGUgQm Vhc3Qn
IGlzIG91dCB0b2RheSEg8J+MuQ==
?=
Please can you advise why this is happening.
It appears to be issue with Apple Mail client, as the same subject line is working fine on all other mail clients.
Let me know if I missed to explain anything.
Best regards,
Amar
If you are using MIME::Base64 to encode the string, it adds a trailing whitespace (for some reason!). From your example it looks like your converter function is adding a newline character:
Subject: =?UTF-8?B?IPCfjLkgVGhlIGVuY2hhbnRpbmcgc291bmR0cmFjayB0byAnQmVhdXR5IEFuZCBUaGUgQ mVhc3QnIGlzIG91dCB0b2RheSEg8J+MuQ==
?=
...should be all on one line:
Subject: =?UTF-8?B?IPCfjLkgVGhlIGVuY2hhbnRpbmcgc291bmR0cmFjayB0byAnQmVhdXR5IEFuZCBUaGUgQ mVhc3QnIGlzIG91dCB0b2RheSEg8J+MuQ==?=
Breaking the line with a newline or whitespace breaks the encoding and causes some clients to panic. This is what I use:
## ENCODE SUBJECT
use MIME::Base64;
my $subject_enc = encode_base64($subject);
$subject_enc =~ s/^\s+|\s+$//g; #TRIMS ANY SURROUNDING WHITESPACE
$subject_enc =~ s/^\n+|\n+$//g; #TRIMS ANY NEWLINE CHARACTERS
my $subject_enc = '=?UTF-8?B?' . $subject_enc . '?=';
Related
Have a program that issues the following command at the linux level
EXE1= "SH -c '/usr/lib/sendmail ":EMAIL<1,X>:' < "/thisdata/level1/VRE/&HOLD&/':PC.FILE:'.CSV':'"':"'"
Is it possible to attach a second PC.File in this instruction?
The rightest answer to this would require knowledge of OS and any mail server restrictions you might have but I have been working on an old routine of ours that maybe provide some insight.
We used to us uuencode as #RedCabbage suggested but we had problems with some servers rejecting the messages. As we have no control over other people's servers and because uuencode is almost as old as dirt we updated our solution to use MIME instead. See "Not As Easy Way"
Easy way
On our Linux system we use mailx (which in our case linked to /usr/bin/mail) to add multiple attachments.
echo "It's Wednessday, my dudes." |mail -s "foobar" -a foo.txt -a bar.txt dudes#wednessday.com
This results in an email with two attachments and message body if the files are properly pathed (if used with a real address).
Not As Easy Way
Create your own MIME (multipart/mixed) message. We create a record similar to this.
To: foo#foo.bar
From: bar#foo.bar
Subject: Not Rocket Surgery
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="SomelongBoundryMarkerYouMakeUpDoNotUseThis"
This is a multipart message in MIME format.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Content-Type: text/plain
The body of that message go here.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename="YourFileName.pdf"
Content-Base: http://www.IputOurDomainHereDunnoWhyDoNotUseThis.com
##BASE64 encoded string go here.
--SomelongBoundryMarkerYouMakeUpDoNotUseThis
Rinse and Repeat with more files, newlines are important here.
For the encoding part, assuming that your file is in /foo/bar.pdf
ENCODED = ""
FILE.LOC = "/foo/bar.pdf"
TEST = ENCODE("Base64",1,FILE.LOC,2,ENCODED,1)
IF TEST EQ 0 THEN
;*Put the text in ENCODED where it says ##BASE64 encoded string go here
END
This is much more tedious because you have to figure out the all the mime types and make sure everything is formatted just right.
Good Luck
This is how we have managed multiple attachments. For example, /home/jbloggs/attachment1.doc /home/jbloggs/attachment2.pdf.
You will (as you have done) have to build the commands and execute them with SH -c 'blah' (as you have done already.)
Create a text file with the address, subject and body in (for example) /home/jbloggs/tempemail.txt
To: recipient.email#gmail.com
subject: Your subject line here
This is the main body text
Cycle through the attachments and uuencode them to an incrementing name (ENCODEDn here):
uuencode /home/jbloggs/attachment1.doc attachment1.doc > ENCODED1
uuencode /home/jbloggs/attachment2.pdf attachment2.pdf > ENCODED2
Use cat to join all the files together to a single one:
cat /home/jbloggs/tempemail.txt ENCODED1 ENCODED2 > COMBOFILE
Use sendmail on the combo:
sendmail recipient.email#gmail.com < COMBOFILE
You can loop through as many ENCODEDn files as you like.
I am trying to send an email using mailx through Automic Workload Automation 12.0's bash jobs. The message needs to have a special character, in this case the percent sign "°".
The message should have the body This is the ° sign., for this example.
I am using this code to send the message. printf '\xB0' prints the ° sign.
(
printf 'This is the '
printf '\xB0'
printf ' sign.'
) | mailx [etc]
If I copy and paste this directly into a bash terminal, the email sends fine with the special character printed in the message body.
However, when I use the same code in Automic bash jobs, the email body is blank. There is a file attached, named ATT00001.bin. If I open ATT00001.bin using notepad.exe, the file contains the text that should have been in the body, This is the ° sign. With the characters printed exactly as they should be in the body.
The following when used in Automic results in a message being sent with the correct body. No files are attached. So it seems clear that the special character is causing this issue with Automic.
(
printf 'This is the '
printf 'placeholder'
printf ' sign.'
) | mailx [etc]
Does anyone know why this happens, or how to resolve it?
Mailx is a evolved MUA. For just sending a mail, if you use sendmail, you could build your own mail header:
/usr/sbin/sendmail destuser#desthost <<eomail
To: destuser#desthost
Subject: Any interesting thing
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
This is the ° sign
eomail
Or you could use html encoding:
/usr/sbin/sendmail destuser#desthost <<eomail
To: destuser#desthost
Subject: Any interesting thing
MIME-Version: 1.0
Content-Type: text/html; charset="ASCII"
Content-Transfer-Encoding: 8bit
<html><body>This is the ° sign</body></html>
eomail
Care, use only ASCII characters there!
I am trying to use the mailx command for sending email with attachment (zipped) and am facing two issues, below is the command I use:
(echo "$BODY"; UUENCODE $ZIP_FILE $ZIP_FILE) \
| mailx -s $SUBJECT_1 -r " " $SENDER $RECIPIENT
My email subject contains space and Japanese characters.
The variable $SUBJECT_1 has the following statement
Subject: [Budget] Subtype Error and some JAPANESE CHARECTERS
I get bet following error:
contains invalid character '\203'
Moreover for testing purpose I changed the statement of SUBJECT_1 to Test Message
SUBJECT_1="Test Message"
It worked, but I receive only Test instead of Test Message and in the mail I could see two more email ids in the To like Message#domain.com and -r#domain.com
I have not implemented the mail body yet, once subject issue fixed will implement the same in body because Body will also have Japanese characters.
Please help me with this error, how to resolve and what am i doing wrong
There's a list of things you need help with here, more than I want to to handle exhaustively on a sunny Saturday afternoon. But some hints.
Quote your variables.
"$SUBJECT_1" is a single string, whereas $SUBJECT_1 is a list of space-separated words. The second word is your email recipient, and subsequent options are also recipients.
Subject.
The basic idea is that you need to include encoding data in the subject, because email headers are only supposed to include 7-bit ASCII.
Here is a hint at how you put special characters in your Subject line.
Here is another hint.
Here is the RFC that describes in lurid detail what you need to do. Asking your favourite search engine for information about "utf8 email subject" and "rfc1522" is probably a good idea.
Email client.
Finally, rather than learning how to use MIME, consider using mutt instead of mailx to send your mail. Mutt has a -a option to add attachments, making it WAY easier than constructing your own headers and body, which I'm not even sure you'd be able to do with mailx in the first place.
Is it possible to read the emails based on the Subject line and then get the base64 attachment or directly get the attachment ?
Server : Linux System
Your question seems to presuppose that there is a single attachment and that it can be reliably extracted. In the general case, an email message can have a basically infinite amount of attachments, and the encoding could be one out of several.
But if we assume that you are dealing with a single sender which consistently uses a static message template where the first base64 attachment is always going to be the one you want, something like
case $(formail -zcxSubject: <"$message") in
"Hello, here is your report for "*)
awk 'BEGIN { h=1 }
h { if ($0 ~ /^$/) h=0 ; next } # skip headers
/^Content-Disposition: attachment/ { a=1 } # find att
a && /^$/ { p=1; next }
p && /^$/ { exit }
p' "$message" |
base64 -d ;;
esac
This will extract the Subject: header and compare it to a glob pattern. I expect this is what you mean by "based on subject" -- if we find a matching subject header, examine this message, otherwise discard.
The crude Awk script attempts to isolate the base64 data and pass it to base64 -d for extraction. This contains a number of pesky and somewhat crude assumptions about the message format, and probably requires significant additional tweaking. Briefly, we skip the headers, then look for MIME headers identifying an attachment, and print that, skipping everything else in the message. If this header is missing, or identifies the wrong MIME part, you will get no results, or (worse) incorrect results. Also, the /^Content-Disposition:/ regex could theoretically match on a line which is not a MIME header, though this seems highly unlikely (but might actually happen if you are looking e.g. at a bounce message).
A more robust approach would involve a MIME extraction tool or perhaps a custom script to actually parse the MIME structure and extract the part you want. Without details about what exactly you need, I'm not able to provide that. (This would also allow you to use the sender's specified filename; the above script simply prints the decoded payload to standard output.)
Note also that formail has no idea about RFC2047 encoding, so if the subject is not plain ASCII, you have to specify the encoded form in the script.
I'm using PHPMailer to send mails and like to append a DKIM-signature to mails. I had problems before I applied this patch. Now I'm able to send a successfull signed message to isnotspam.com.
I have successfully signed a message with less than 1500 characters in the body. If increase the character count (even with simple a's) The signature fails.
I've correctly set up a TXT domain record.
I guess it's because of the email's body cause if I use this service I always get a "wrong body hash" error.
Signature in the email header looks like this one:
DKIM-Signature: v=1; a=rsa-sha1; q=dns/txt; l=641; s=mymail;
t=1354285494; c=relaxed/simple;
h=From:To:Subject;
d=revaxarts.com;
z=From:=20"WP=203.4"=20<info#rvaxarts.com>
|To:=test#rvaxarts.com
|Subject:=20DKIM=20Test;
bh=Sx1Rj3c65v2Hk0fmg2j5XNIDi14=;
b=n4OGAwl3i[...]AOkfUglp6iiYZ6B2M3ZKlGW5gDfE=
I had the same problem here with a Perl script and wrong body hash.
I used \n for newline (example end of header line).
But you have to use \r\n. This solved it for me!
EDIT:
Thanks to ArtemGr for the comment and url to the following information (copied from http://permalink.gmane.org/gmane.mail.postfix.user/223780 to prevent link rot):
A likely cause of breakage is that the sending application generates
email that is incompatible with RFC 5322 or RFC 5321 in some respect.
Lines longer than 990.
The Postfix SMTP client keeps the line length below the SMTP
protocol limit of 1000 bytes including . Since this change
happens after signing, it will definitely break DKIM signatures.
To avoid long-line curruption problems send mail in quoted-printable
or base64 encoding, with lines of at most 80 characters long.
Malformed line endings.
SMTP requires line endings, and does not allow or
characters in any other context.
The Postfix sendmail commands expects UNIX-style <LF> [line-feed] line endings.
It will also accept lines ending in <CR><LF> [carriage-return line-feed] but you can't use
mixed line ending styles in the same message.
And so on. If you want to ensure that DKIM signatures survive, you
need to send email that is within the protocol specs of RFC 5322 or RFC 5321;
My case was unicode apostrophe and hypen chars. After replacing them with ascii ones, the DKIM validation is passed.