How to create an email file? - linux

After the fetchmail fetches mails, the new mails are stored in a file like /var/mail/user. We can open the file user by text editor like vim.
How can I create such text-based email files? Say, I want to send an email with contents:
From: sender <sender#xx.com>
To: receiver <receiver#xx.com>
Subject: test subject
Contents: ...
Attached: file1.txt, file2.png, file3.pdf
The problem is how to make these to be a formal text-based email.
Besides, If I have such an email file. How can I extract files(say, subjects, contents, attached files, etc.) by some command line tools. I know I can open it with program like mutt. Can this be done using a command line utility?

There is a bunch of standards you need to understand, but email is fundamentally text.
The file format in /var/spool/mail or /var/mail/user etc is typically Berkeley mbox. This is not formally defined anywhere, but consists of a sequence of RFC5322 (née RFC822) email messages, each preceded by a From_ line, the format of which is basically From %s %C where %s is the sender's email address (what you also see in Return-Path:) and %C is the date when the message arrived. Notice the two spaces between the format strings!
The toplevel email message is RFC5322 but on top of that, you need to understand MIME.
mbox: RFC 4155
Email message format: RFC5322
MIME: RFC2045 RFC2046 RFC2047 RFC2048
You will also stumble over (E)SMTP RFC5321 which is only tangential to your question, but good to know. Notice how 821 and 822 (and later 2821 and 2822, and now 5321 and 5322) have adjacent RFC numbers.
Furthermore, there is a wild, wild West of non-standard headers, some of which are nonetheless significant. Dan Bernstein's reference http://cr.yp.to/immhf.html is a lifesaver. As a general guideline, what spammers typically do is copy/paste headers without understanding them; therefore, an essential practice for deliverability is "don't do that". In other words, if you don't know what a header is for, don't use it.
Any modern programming language will come with libraries to create and manipulate RFC5322 and MIME, and probably mbox too. For creating a message you can send somewhere, you don't need mbox anyway, just something along the lines of (pseudocode)
message = new MIME({'Subject': 'hello', 'From': 'me#example.net',
'To': 'My Friend <you#example.com>'});
message.addbodypart('text/plain', 'Hi Fred.\nHow are you?');
message.addbodypart('image/png', {'file': '/home/you/logo.png'});
smtp = new SMTP('mail.example.net', 587, {'user': 'me', 'pass': 'xyzzy'});
smtp.send(message);
A multipart message looks something like what you describe in your question, except there is no specific header to identify "attachments" and actually conceptually no "attachments", just "body parts". Here is a simple MIME message to show what the message in your question would properly look something like.
From: sender <sender#example.com>
To: receiver <receiver#example.com>
Subject: test subject
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="so_long_eFlop"
This is a MIME multipart message. Nobody actually sees what it says here.
--so_long_eFlop
Content-type: text/plain; charset="utf-8"
Content-disposition: inline
Content-transfer-encoding: 7bit
Many mail clients will display this as the "main part" but MIME does not
define any particular hierarchy. Many mail clients will generate a
text/plain rendering and a text/html rendering of the message you type in,
and the recipient's mail client will decide -- based on user preferences
-- which one to display. Anyway, I will not create an example of that
here. This is just "a text message with a picture attached", or, more
precisely, a MIME message with two body parts.
Oh, the content-disposition: inline is usually just implied for a
text/plain part. Some clients will override or ignore the disposition
set by the sender anyway.
--so_long_eFlop
Content-type: image/png
Content-disposition: attachment
Content-transfer-encoding: base64
Iam+not/attaching+a/real00picture+here/just/a/bunch0of/binary/goo===
--so_long_eFlop--

The file format is called "mbox". There's a good article on Wikipedia (http://en.wikipedia.org/wiki/Mbox), as well as all over the Internet. Like RFC 4155. :)

telnet your.mail.server 25
helo localhost.localdomain
mail from:<sender#address.com>
rcpt to:<recipient#address.com>
data
From:Me
Subject:This is an email via Telnet
Hi,
The first line connects to the server on port 25. Replace "your.mail.server" with the name or address of the MX server for the domain.
Most servers expect the second "HELO" line to begin the session. I have seen servers that don't care, but in general they should throw an error.
You must have a "MAIL FROM:" line with the address you expect a reply to come to.
The mail is going nowhere if you don't specify the "RCPT TO:" address.
The message body begins with "DATA" line. This will usually be met with instruction on how to end the message - a single "." on a line by itself.
The "From:" and "Subject:" headers above are optional. You can add any additional headers here.
.
quit

Related

Base64 coded attachments to e-mails in Public Record Request from City Government

In a public record request to a city government, I have gotten back a number of records that are .txt format of e-mails with attachments appearing to be base64. The e-mail attachments are jpegs, pdf, png, or doc in base64 a shorter example is below. The government official claim "The records we released to you are in the form that was available, including the file you noted. From what I have been told, it is likely garbled computer coding. We have no other version of those records."
Questions:
Does someone have to intentionally work at saving the e-mail and attachments in this way so that they are unreadable, thus making the information not public (hiding it)?
or is this something that can plausibly happen in saving "garbled computer coding"?
If it is plausible that a computer does it, how?
Is there a way of decoding it?
I have tried a number of online decoding with various settings and have been unsuccessful.
I have done a number of public record requests from this city and department in the past and have never gotten such .txt documents. The public record request is around a city contract that is problematic.
From: "Steinberg, David (DPW)" <david.steinberg#sfdpw.org>
To: "Goldberg, Jonathan (DPW)" <jonathan.goldberg#sfdpw.org>
Sent: Fri, 17 May 2019 20:40:36 +0000
Subject: Re: SOTF - Education, Outreach and Training Committee, May 21, 2019 hearing
----boundary-LibPST-iamunique-566105023_-_-
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <image002.jpg#01D50CB4.2B093D10>
Content-Disposition: attachment;
filename*=utf-8''image002.jpg;
filename="image002.jpg"
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMg
IyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wAARCABJAFEDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2N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yK6dai4tAxIW
ZEZA3fbu7/Ss/V1v0vyuqIyXAUDaygYXtgDjH0rcsta0q98PWmlandXWnvbE7JoSdjdeTj61meJd
KuNJ1JEuLo3ayxh4pyTll6DP0q4v3tRPYyKKKK2JCtSTxDeyeH00dtn2ZCCDg7sA5xnPTNZdFJpP
cDUfxDeyeH00djH9mQ5BAO4jOcE56Zo0nxFeaRFJBGIp7WQ5eCddyE+vtWXRS5I2tYd2a2reI7zV
rVbRkhtrRTkQW6bVJ9T61DqWt3WqS2kkwRHtUCRmMEcA5BPPXis+ihQS6Cuzb/4Su9GtnVRDbC5M
XlNhTtYepGetVNK1q50h7gwLHIlyhSWOUEqw+mfc/nWfRS5I7WHdm5YeKrixso7RrKxuIYs+WJos
lec9aoarq11rV59pvGUuF2qqjCovoBVKimoRTuhXYUUUVQH/2Q==
----boundary-LibPST-iamunique-566105023_-_---
I got the image by copying your base64 data to a file and
base64 -d file > image.jpeg
on my Debian/Linux.
RFC 2045 section 6 says binary data are to be encoded in US-ASCII characters
so that it's quite normal images are encoded by base64 although GUI email reader
would not show you raw data.
When you see such raw data, it means somebody might copy and paste other emails blindly, still it less likely happen. (Obviously your example is part of multipart message, but not complete.)
Decode service on web is available, for example, here.

sendmail add a second attachment to an email

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.

javamail BASE64DecoderStream decode issue

Found an issue with Base64DecoderStream in javamail. Some email content I get are like this:
Content-Type: text/plain; charset=3D"utf-8"
Content-Transfer-Encoding: base64
QmFzZTY0IGlzIGEgZ2VuZXJpYyB0ZXJtIGZvciBhIG51bWJlciBvZiBzaW1pbGFyIGVuY29kaW5=
n
IHNjaGVtZXMgdGhhdCBlbmNvZGUgYmluYXJ5IGRhdGEgYnkgdHJlYXRpbmcgaXQgbnVtZXJpY2F=
s
bHkgYW5kIHRyYW5zbGF0aW5nIGl0IGludG8gYSBiYXNlIDY0IHJlcHJlc2VudGF0aW9uLiBUaGU=
g
QmFzZTY0IHRlcm0gb3JpZ2luYXRlcyBmcm9tIGEgc3BlY2lmaWMgTUlNRSBjb250ZW50IHRyYW5=
z
ZmVyIGVuY29kaW5nLg==
Ideally the = sign should have been replaced with the single character on the following line but gsuite(Gmail) sometimes does like this. This causes Base64DecoderStream to corrupt the message. However, Outlook and many popular online base64 decoders handle this base64 content well. Can this be fixed?
Additional detail was provided privately, which allowed me to determine that the problem is that the message includes an attachment of MIME type message/rfc822 (the original message), and that attachment uses a Content-Transfer-Encoding of quoted-printable. The MIME spec does not allow the use of that encoding for MIME content of that type. This is a violation of the MIME spec that Google really needs to fix. Please provide them this additional information if they haven't figured it out themselves.
RFC 2046, section 5.2.1, says:
No encoding other than "7bit", "8bit", or "binary" is permitted for
the body of a "message/rfc822" entity.
In the mean time, you can set the JavaMail System property mail.mime.allowencodedmessages to "true" to work around this bug in GSuite.

Use Japanese characters in subject and attach file with mailx in RedHat Linux

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.

body has been altered failure on DKIM validation

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.

Resources