Scripting telnet mailing

若如初见. 提交于 2020-05-15 08:54:12

问题


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 ?


回答1:


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.




回答2:


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..



来源:https://stackoverflow.com/questions/40842300/scripting-telnet-mailing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!