Send email using OpenSSL

15,586

have you tried

openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf  <<EOF
helo
auth login
$(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

Share:
15,586

Related videos on Youtube

3bdalla
Author by

3bdalla

Updated on September 18, 2022

Comments

  • 3bdalla
    3bdalla over 1 year

    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
    auth login
    (Put base64 encoded username)
    (Put base64 encoded password)
    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.

    • wurtel
      wurtel over 8 years
      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.
    • Edward Falk
      Edward Falk over 7 years
      Perhaps expect(1) will do what you want?
  • s3lph
    s3lph over 8 years
    I was just about to answer something like this...
  • Арсений Черенков
    Арсений Черенков over 8 years
    try to add -pause option ? (after -crlf but before <<EOF
  • Alessio
    Alessio over 8 years
    Don't forget to insert a blank line between the last header (Subject: in this case) and the first line of the body.
  • Yaroslav Nikitenko
    Yaroslav Nikitenko over 7 years
    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.
  • tripleee
    tripleee almost 7 years
    You should use double quotes around every string which doesn't specifically require wildcard expansion and whitespace tokenization. In particular, the password might well contain shell metacharacters.
  • Dmitry Grigoryev
    Dmitry Grigoryev almost 5 years
    Could you make it a standalone answer? "The script as above" doesn't make sense as answers can be added, removed and voted on.