1

I need to send an email, using abash script. I am aware of this question and answer.

I have tested ssmtp, mail, mutt, etc. Only openssl s_client seems to work.

Now, here is my script:

echo $1
echo $2
echo $(echo -ne "\0login\0pass" | base64)            # This produces the correct output

openssl s_client -connect my.mailserver.com:465  <<EOF # the server path is ok
EHLO my.mailserver.com
auth plain $(echo -ne "\0login\0pass" | base64)
mail from:my@awesome.addr
rcpt to:$1
Data
From: system@mystep.app
To: $1
Subject: mysubject

Link zum Abschlißen : my.great.url:port/mylink/?user=$2
.
EOF

If i issue the same commands one by one by hand, (with replacing $1 and $2 suitably) the email is being sent. But I want to send the two arguments to a bash script as above, and want that the script parses those arguments, and send the mail.

Executing the script above using bash mail.sh seany@mail.domain testuser leads to this output:

seany@mail.domain
testuser
base64 login output (I removed the string, but the string is exactly the same, that I can use to log in manually using AUTH PLAIN string)
CONNECTED(00000003)
depth=2 C = US, ST = New Jersey, L = Jersey City, O = The USERTRUST Network, CN = USERTrust RSA Certification Authority
verify return:1
depth=1 C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA
verify return:1
depth=0 CN = *.kasserver.com
verify return:1
---
Certificate chain
 0 s:CN = *.kasserver.com
   i:C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA
   a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA256
   v:NotBefore: Jan 11 00:00:00 2023 GMT; NotAfter: Feb 11 23:59:59 2024 GMT
 1 s:C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA
   i:C = US, ST = New Jersey, L = Jersey City, O = The USERTRUST Network, CN = USERTrust RSA Certification Authority
   a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA384
   v:NotBefore: Nov  2 00:00:00 2018 GMT; NotAfter: Dec 31 23:59:59 2030 GMT
 2 s:C = US, ST = New Jersey, L = Jersey City, O = The USERTRUST Network, CN = USERTrust RSA Certification Authority
   i:C = GB, ST = Greater Manchester, L = Salford, O = Comodo CA Limited, CN = AAA Certificate Services
   a:PKEY: rsaEncryption, 4096 (bit); sigalg: RSA-SHA384
   v:NotBefore: Mar 12 00:00:00 2019 GMT; NotAfter: Dec 31 23:59:59 2028 GMT
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIGNjCCBR6gAwIBAgIQZPD5ktHBmqF8UhtnMD8BpTANBgkqhkiG9w0BAQsFADCB
jzELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G
A1UEBxMHU2FsZm9yZDEYMBYGA1UEChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQD
Ey5TZWN0aWdvIFJTQSBEb21haW4gVmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENB
MB4XDTIzMDExMTAwMDAwMFoXDTI0MDIxMTIzNTk1OVowGjEYMBYGA1UEAwwPKi5r
YXNzZXJ2ZXIuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAucQ3
nHxI9CyL6xgUg475aCXAXAh261V9t0zpq5rRt2v8um6BHHip9Lvofu66I3P85JU5
uR2RawD+zWtrGsZHNiVl7jPgTS+Sgbe8vt7fisTHfQBRAZzGyGnSz98B4+u83Tzg
Ab8336lif8Ior4AiU4WghFWm83IfUsDed9Gmn3nJkufUpT3W6pOlQpKEDFdZzmsf
smMM6sYdY9riTdcwPLwGFuDmuimWel5AHvZ8Zxf737llPRJ4okA/GwraOrmphlWe
uP5ISC6pY02GMBitYr2bV8rfDJFt+6s96I8si2Wd6wk9Z6c7q4B5YDYF0C6gn0rr
UXJvg1PJKkFX+LjIIQIDAQABo4IDADCCAvwwHwYDVR0jBBgwFoAUjYxexFStiuF3
6Zv5mwXhuAGNYeEwHQYDVR0OBBYEFCzoJJG+cOYJP+Qvcz4rBc8/nDYgMA4GA1Ud
DwEB/wQEAwIFoDAMBgNVHRMBAf8EAj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+Yj/xKhHSqI+Ai238GX692chXJKhQijoAAZh3Xd5WKmr/YqNfBbb8IpB6
gMBK9z1sdQQjgYYWlu1IWmTz9Wt+06E7P/CBwX4D1t0TwjyK1YLJrBc/dQr5pDvJ
pVhY3p9VMG79JV+qxHwZ88AASrTZ5CY7SAbTrv1HuP1DGWr/QGjwjf9oSiDXxSUZ
F3RllbAcskKArw==
-----END CERTIFICATE-----
subject=CN = *.kasserver.com
issuer=C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 5132 bytes and written 404 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
DONE

But the email would not be sent.

I need a solution, which, if working in Bash alone, would be enough. Thank you.

Sean
  • 789
  • 6
  • 26

1 Answers1

1

Maybe the issue is that your script is sending the HERE document to quickly to the program and/or the HERE-document EOF is reached before the openssl is finished processing it.

If the 'sleep' command is also allowed besides bash, I would do this:

Using the 'sleep'-command introduce 5 seconds delay, before and after the echo command with the information. The grouped commands output is piped into openssl s_client.

Note: You can and should adjust the number of seconds to whatever works.


echo $1
echo $2
echo $(echo -ne "\0login\0pass" | base64)            # This produces the correct output

# introduce sleep 5 seconds, before and after the echo
# this should provide the openssl s_client time to setup
# the connection, process the data, and be ready for closing
(
sleep 5
echo "EHLO my.mailserver.com
auth plain $(echo -ne "\0login\0pass" | base64)
mail from:my@awesome.addr
rcpt to:$1
Data
From: system@mystep.app
To: $1
Subject: mysubject

Link zum Abschlißen : my.great.url:port/mylink/?user=$2
.
"
sleep 5
) | openssl s_client -connect my.mailserver.com:465

Good Luck

S_IROTH
  • 210
  • 1
  • 5
  • Thank you. It finally worked after setting 1 second breakpoint to every line, and individually echoing every line. – Sean Mar 17 '23 at 18:28