Task

Write a function to send an email.

The function should have parameters for setting From, To and Cc addresses; the Subject, and the message text, and optionally fields for the server name and login details.

 If appropriate, explain what notifications of problems/success are given.
 Solutions using libraries or functions from the language are preferred, but failing that, external programs can be used with an explanation.
 Note how portable the solution given is between operating systems when multi-OS languages are used.


(Remember to obfuscate any sensitive data used in examples)




begin_instruct(Send email)_param({str} from, to, cc, subject, msg, {html}, htmlmsg)_opt({cred}, login)_ns(rosettacode);
    set_thread(linger);
    find_thing()_first()_email()
        ? with_found()_label(emailer);
        : exit_instruct[]_err(Sorry, no one can send emails!);
    ;
    with_[emailer]_format(html)
        ? with_[emailer]_cred[login]_email[htmlmsg]_from[from]_to[to]_cc[cc]_subject[subject];
            : exit_instruct[]_err(Something went wrong with the email);
        : with_[emailer]_cred[login]_email[msg]_from[from]_to[to]_cc[cc]_subject[subject];
            : exit_instruct[]_err(Something went wrong with the email);
    ;
    reset_thread();
end_instruct[];
 
set_namespace(rosettacode);
 
// Set up credentials
add_var({cred}, mycred)_server(esvr1)_username(bob)_password(p@$$w0rd);  // Other credentials can be added here
 
// Send email:
exec_instruct(Send email)_param()
    _from(bob@bobmail.com.invalid)  // It is at the discretion of the caller to use this from address 
    _to(fred@bobmail.com.invalid)
    _cc(jill@bobmail.com.invalid)
    _subject(Rosettacode wants me to send an email!)
    _msg(This is the body of my email.)
    _htmlmsg(<b>This is the body of my email, in bold</b>)
    _login[mycred]
_me();
 
reset_namespace();