Search code examples
bashtelnet

Scripting telnet mailing


I want to create a script that will be able to send mail easily with the user choices.

I did this but it does not work

mail_sender ()
{
    echo " - FROM : "
    read from
    echo " - TO : "
    read to
    echo " - Subject : "
    read subject
    echo " - Message : "
    read message
    telnet localhost 25
    ehlo triton.itinet.fr
    mail from: $from
    rcpt to: $to
    data
    subject: $subject
    $message
    .
}

Do you have an idea ?


Solution

  • Redirect to telnet a here-document:

    mail_sender ()
    {
        echo " - FROM : "
        read from
        echo " - TO : "
        read to
        echo " - Subject : "
        read subject
        echo " - Message : "
        read message
        telnet localhost 25 << EOF
    ehlo triton.itinet.fr
    mail from: $from
    rcpt to: $to
    data
    subject: $subject
    $message
    .
    EOF
    }
    

    The content of the here-document will be redirected to telnet, effectively executing these SMTP commands in the mail server's shell.

    It's important that the lines within the here-document don't have any indentation at all (no spaces or tabs at the start of the lines). Notice that the indentation looks kind of broken in the way I wrote it above, halfway in mail_sender. It has to be this way, because that's how here-documents work.

    You can read more about here-documents and input redirection in man bash.