Using mmencode for authenticated SMTP sessions

To authorize via smtp, you must first obtain a login string using mmencode and printf. Echo does not work. mmencode is part of the Metamail package. I "think" the link above is safe if you need a copy.

You can encode and decode using mmencode.

Using mmencode to encode text

$ printf "A String" | mmencode
QSBTdHJpbmc=

Using mmencode to decode text

$ echo `printf "QSBTdHJpbmc=" | mmencode -u`
A String

Example SMTP AUTH Session using AUTH PLAIN

$ printf "user\0user\0password" | mmencode or $ printf "%s\0%s\0%s" user user password | mmencode
dXNlcgB1c2VyAHBhc3N3b3Jk
$ telnet smtpserver 25
220 smtpserver ESMTP
ehlo domainname
250-smtpserver 250-AUTH LOGIN PLAIN 250-8BITMIME 250 PIPELINING
AUTH PLAIN dXNlcgB1c2VyAHBhc3N3b3Jk
+OK
235 Authentication succeeded.
mail from: <fromme@domainname>
+OK
250 Sender accepted.
rcpt to: <tothem@otherdomain.com>
+OK
250 Recipient accepted.
data
+OK
354 End your message with a period.
To: "Other Person" <tothem@otherdomain> From: "Me" <fromme@domainname> Subject: Test Email This is the content. .
+OK
250 Accepted message qp 847 bytes 247 quit 221 Good bye.

Example SMTP AUTH Session using AUTH LOGIN

To use auth login, you will need to encode the username and password seperately. As before, you can use either one of the two printf methods:

$ printf "user" | mmencode
dXNlcg==
$ printf "password" | mmencode
cGFzc3dvcmQ=

- OR -

$ printf "%s" user | mmencode
dXNlcg==
$ printf "%s" password | mmencode
cGFzc3dvcmQ=

Part of the trick to using AUTH LOGIN mode is decyphering the server responses. To decode the responses, simply print the string back into mmencode using the -u option as shown above.

$ telnet smtpserver 25
220 smtpserver ESMTP
ehlo domainname
250-smtpserver 250-AUTH LOGIN PLAIN 250-8BITMIME 250 PIPELINING
AUTH LOGIN
334 VXNlcm5hbWU6 (This says "Username:")
dXNlcg==
334 UGFzc3dvcmQ6 (This says "Password:")
cGFzc3dvcmQ=
mail from: <fromme@domainname>
+OK
250 Sender accepted.
rcpt to: <tothem@otherdomain.com>
+OK
250 Recipient accepted.
data
+OK
354 End your message with a period.
To: "Other Person" <tothem@otherdomain> From: "Me" <fromme@domainname> Subject: Test Email This is the content. .
+OK
250 Accepted message qp 847 bytes 247 quit 221 Good bye.

Unencode a password/username with mmencode

To unencode a mmencoded string, pass the string to mmencode -u

echo dXNlcgB1c2VyAHBhc3N3b3Jk | mmencode -u | tr '\0' ' '
user user password

printf (mmencode) breaks with numeric passwords

While I was troubleshooting an issue one day, I noticed that a numeric password (or at least a password that started with a number) would parse incorrectly. For instance a password of '799pass' would unencode as '99pass'. That's because the printf command saw the numbers following the '\0' null as part of an octal sequence (except for the 9's of course since octal stops at 7). This problem is readily apparent in almost all the examples on the internet:

printf "user\0user\0password" | mmencode

A better way to do this is to provide the format and the arguments as seperate arguments to printf (like in C).

printf "%s\0%s\0%s" user user password | mmencode