Search code examples
emailexpecttelnetsendmultipart

Send email with shell with telnet I am receiving bad-flag error


I have my own server, which receiving telnet emails. And I aneed to test functionality. I am using expect shell script. All is working fine until I want to try multipart messages.

I tried to add qotes to multipart boundary definition, remove spaces, unescape it, combination of it.

#!/usr/bin/expect
set timeout 20
set fileName "/mnt/d/attachment.txt"
set to [lindex $argv 0]
spawn telnet localhost 25252
expect "220*"
send "ehlo Einstein\r"
#expect "250-*"
#expect "250-*"
expect "250*"
send "mail from:<[email protected]>\r"
expect "250*"
send "rcpt to:<$to>\r"
expect "250*"
send "data\r"
expect "345*"
send "from: <[email protected]>\r"
send "to: <$to>\r"
send "Subject: expect test\r"
send "Mime-Version: 1.0;\r"
send "Content-Type: Content-Type: multipart/alternative; boundary=boundary-string\r"

send "--boundary-string \r"

send "Content-Type: text/html; charset=\"ISO-8859-1\";\r"
send "Content-Transfer-Encoding: 7bit;\r"

send "\r"
send "<html>\r"
send "<body>\r"
send "<h1>This is HTML</h1> \r"
send "This is message with link <a href=\"http://www.google.sk\">Google</a> <img src=\"https://hips.hearstapps.com/ame-prod-menshealth-assets.s3.amazonaws.com/main/thumbs/34458/arnie-2.jpg?resize=768:*\" /> \r"
send "</body>\r"
send "</html>\r"

send "--boundary-string \r"
send "Content-Transfer-Encoding: base64\r"
send "Content-Disposition: attachment;filename=$fileName\r"
send "--boundary-string-- \r"

send ".\r"
expect "250*"
send "Quit"

I am getting this error:

bad flag "--'boundary-string'": must be -i, -h, -s, -null, -0, -raw, -break, > or -


Solution

  • The correct answer is

    send -- "--boundary-string \r"

    I tried before all others except this one

    Here is correct and working solution which I didn't find anywhere:

    #!/usr/bin/expect
    set timeout 20
    set separator "multikulti"
    set to [lindex $argv 0]
    spawn telnet localhost 25252
    expect "220*"
    send "ehlo Einstein\r"
    expect "250*"
    send "mail from:<[email protected]>\r"
    expect "250*"
    send "rcpt to:<$to>\r"
    expect "250*"
    send "data\r"
    expect "345*"
    send "from: <[email protected]>\r"
    send "to: <$to>\r"
    send "Subject: expect test\r"
    send "Mime-Version: 1.0;\r"
    send "Content-Type: multipart/mixed; boundary=$separator1\r"
    
    send "This is just text \r\n"
    send -- "--$separator1\r"
    send "Content-Type: text/plain; charset=us-ascii\r"
    send "This is plain text \r\n"
    
    send -- "--$separator1 \r"
    send "Content-Type: text/html; charset=ISO-8859-1\r\n"
    send "<html>\r"
    send "<body>\r"
    send "<br /><h1>This is HTML</h1> \r"
    send "<br /><br /><br /><br /><br clear=\"all\" /><img src=\"cid:0123456789\" width=\"400\" height=\"800\"/> \r"
    send "<br /><br /><br /><br /><br clear=\"all\" /> \r"
    
    send "This is message with link <a href=\"http://www.google.sk\">Google</a> <img src=\"https://hips.hearstapps.com/ame-prod-menshealth-assets.s3.amazonaws.com/main/thumbs/34458/arnie-2.jpg?resize=768:*\" /> \r"
    send "</body>\r"
    send "</html>\r\n"
    send -- "--$separator1 \r"
    
    send "Content-Type: image/png; name=\"abc.png\"\r"
    send "Content-Disposition: attachment; filename=\"abc.png\"\r"
    send "Content-Transfer-Encoding: base64\r"
    send "Content-Location: abc.png\r\n"
    send -- "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAADAFBMVEVHcEz////////////////////////////////////////////////////////////////////////////////////////////////////R590L8qdH0IYUsPvGuT9oYUfX63nBt4p9kkgx7X73OW3dnQzf30oYVgtSOhe5nmyelcbN9AauTcCOQ7b7nPfXCju/oMxE6cRBlZ4aGSPUtemfVfz7QzLYxBwuLKvfXT596U9yrGXzBtGGFzh9yQM6sGg5Pq7rfV2hmbt1AAVFpsdLlv2tddvfvn3pp5VwylhgYQvaM5sv15qnk/uLz9eu6eIZWiOtfPdzuWgKP6nLpttq8362ct//uCV5l7O2fGPXB5M+IpSRyzttfB7w97SV8qvlNVW1VeQTpajacYtNxAnldWWf/Pz57/49LnzvVeg3x01h/ApjR6fEN1nAfyGwpPtx66/UnPpdHl5+ZlL+14421HcVND36Ur+g9NGEr6nkLgUfyXOC5gZNCeD8E1lTF+RMkim9VGhgYQvK2Pt6hmjJBofnZK5wLethz6T2KDBfpjGJ6cnhqUGELeMAibfvyJyajKS7f5j1gdNDAkmbj0FhsTGzf7RpL4bQ/SYlPkrJq6NJ25pBU6eHjtvRMSiyPUp6TOnTpo0Mz3mhzOSor6fNn7cqvgA4v+Thk4LDh42LHja0HDClClTpkyZMmXKlClTpkyZMmXKlClTpkxpon8B5BxRDUYHIKkAAAAASUVORK5CYII=\r\n"
    
    send -- "--$separator1 \r"
    
    send "Content-Type: image/png; name=\"sig.png\"\r"
    send "Content-Disposition: inline; filename=\"sig.png\"\r"
    send "Content-Transfer-Encoding: base64\r"
    send "Content-ID: <0123456789>\r"
    send "Content-Location: sig.png\r\n"
    
    send -- "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAADAFBMVEVHcEz////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////lLSe6JSDlLCbkJR7mMiz//v374N+5IBvGSkbmLSfkJB64JSC6JB/kJyG6JiHudXHy19bkJh/fm5m3GxblKyW4HhjlKiS5Ih24HBflKSO5JB/hLCbBJiHkLSf//Pz++vn+9/f......HIKkAAAAASUVORK5CYII=\r\n"
    send -- "--$separator1--\r"
    
    send ".\r"
    expect "250*"
    send "Quit"