Michael003
Michael003

Reputation: 442

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 ?

Upvotes: 2

Views: 3558

Answers (2)

Thufir
Thufir

Reputation: 8497

I've only used it for the simplest of testing:

http://www.jetmore.org/john/code/swaks/

but it boasts:

Completely scriptable configuration, with option specification via environment variables, configuration files, and command line

so no need to re-invent the wheel here..

Upvotes: 0

janos
janos

Reputation: 124804

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.

Upvotes: 2

Related Questions