#include "mailer.ih"

void Mailer::sendmail(string const &subject, string const &txt)
{
    if (d_to.empty())
    {
        log() << "No to-address for mail subject: " << subject << 
                                        ", text: " << txt << '\n';
        return;
    }

    if (not g_options.sendMail())
    {
        log() << "Sending mail (" << subject << ") to " << d_to <<
                    " suppresssed by option\n";
        return;
    }

    string attach;
    if (not d_attachment.empty())
    {
        if (not Stat{d_attachment})
        {
            log() << "Zip to attach not found: " << d_attachment << '\n';
            return;
        }
        attach = "-a " + d_attachment;
        log() << "attachment: " << d_attachment << ": " << attach << '\n';
    }

    log() << "/usr/bin/s-nail " << g_options.fromAddress() << "-s '" <<
                d_subject << subject << "' " <<
                attach << ' ' << d_cc << ' ' << d_to << '\n';

    CinInserter mail;

    mail.execute("/usr/bin/s-nail " + g_options.fromAddress() + "-s '" + 
                d_subject + subject + "' " + attach + d_cc + ' ' + d_to);

    mail << txt << '\n';

    if (not g_options.OK())
    {
        g_tempStream.seekg(0);
        mail << g_tempStream.rdbuf() << '\n';
    }

    if (mail.stop() != 0)
        log() << "Could not send e-mail (" << subject << ") to " << d_to << 
                                                                        '\n';
    else
    {
        inspectAppend(txt);
        log() << "E-mail (" << d_subject << subject << 
                ") was sent to " << d_cc << ' ' << d_to << '\n';
    }
}




