# Send email using OpenSSL

With OpenSSL module under openSUSE I can send an email using this list of commands

openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf
helo
mail from:<email>
rcpt to:<email>
Data
From: email
To: email1, email2, .....
Subject:
(Message body goes here)
.


Is it possible for such a list of commands to be executed at one shot? For example a script?

Note: There is an embedded-Linux system which contains the OpenSSL module, same thing will be applied to it.

• Many mail servers will disconnect if the client sends the next command before the response to the previous command has been given, as that is more often than not what a spam tool will do. – wurtel Oct 19 '15 at 10:04
• Perhaps expect(1) will do what you want? – Edward Falk Aug 2 '16 at 22:24

have you tried

openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf  <<EOF
helo
$(echo$username | base64)
$(echo$password | base64)
mail from:$email0 rcpt to:$email0
Data
From: $email0 To:$emaildest
Subject: $(echo$subject)

$(< body.txt) . EOF  • you must but message's body in body.txt (and remove after) • you must set all $var ...

On a side note, if you sign and encrypt mail, you may wish to look at this question

• I was just about to answer something like this... – s3lph Oct 18 '15 at 9:06
• I have set all env vars. It seems like it executes the first line and exits. It only shows the certificate and other related stuff and that's it. No steps shown for any other steps. – 3bdalla Oct 18 '15 at 9:29
• try to add -pause option ? (after -crlf but before <<EOF – Archemar Oct 18 '15 at 9:41
• Don't forget to insert a blank line between the last header (Subject: in this case) and the first line of the body. – cas Oct 18 '15 at 11:05
• I'd like to note that I used this solution, but it didn't work for large attachments (about 4 Mb). I hade to use a python script for that (using smtplib). OpenSSL returned "RENEGOTIATING error in s_client", but fortunately python worked fine. – Yaroslav Nikitenko Sep 23 '16 at 14:19

to 3bdalla: I had to solve a similar problem when the ssl connection was established but no data of application layer was transferred, only certificate was shown. Try to add in the line with openssl command following parameters: -key, keyfile, -cert and certfile after <port> parameter:

openssl s_client -connect <host>:<port> -key <keyfile> -cert <certfile>


(Firstly I tried command: echo "some_data" | openssl s_client -connect <host>:<port> -key <keyfile> -cert <certfile>)

• Could you make it a standalone answer? "The script as above" doesn't make sense as answers can be added, removed and voted on. – Dmitry Grigoryev Jun 21 at 8:16