$versions{'mail_smtp.pl'} = '06.6.00.0000';
#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################


#########################################################################
#                     attempt_smtp_send_mail
#
# This routine allows store to send email via SMTP as long as credentails
# exist in settings. It is triggered in 'mail-lib.pl' if $sc_agc_use_smtp_email
# is set to 'yes' and the credentials have something set.
#
# This will use local SMTP, and remote SMTP services as long as long as things
# such as OAuth2, and other technlogies are not required.
#
#########################################################################

sub attempt_smtp_send_mail {

    local ( $fromuser, $touser, $subject, $messagebody ) = @_;

    if ( $sc_agc_use_smtp_email ne 'yes' || $sc_smtp_user eq '' || $sc_smtp_pass eq '' ) { return 'error'; }

    eval "use Net::SMTPS"; # we supply module, but load it safely just in case
    use MIME::Lite; # should be a default Perl module.

    if ( $sc_email_content_type eq '' ) { $sc_email_content_type = 'text/plain'; }

    if ( $sc_email_smtp_send_option eq 'simple' ) {
        MIME::Lite->send('smtp', "$sc_smtp_mail_server");
        $MIME::Lite::VANILLA = "AgoraCart Route 66";
        my $msg = MIME::Lite ->new (
            From => "$fromuser",
            To => "$touser",
            AuthUser => "$sc_smtp_user",
            AuthPass => "$sc_smtp_pass",
            Port => $sc_smtp_port,
            Subject => "$subject",
            Data => "$messagebody",
            Type => "$sc_email_content_type"
        );

        $msg->attr('X-Mailer' => "AgoraCart Route 66");
        $msg->send; # send it & done
    }
    elsif ( $sc_email_smtp_send_option eq 'ssl' ) {
        my $smtps = Net::SMTPS->new("$sc_smtp_mail_server", Port => $sc_smtp_port,  doSSL => 'ssl');
        $smtps->auth("$sc_smtp_user","$sc_smtp_pass");

        my $msg2 = MIME::Lite ->new (
            From => "$fromuser",
            To => "$touser",
            Subject => "$subject",
            Data => "$messagebody",
            Type => "$sc_email_content_type"
        );
        $msg2->attr('X-Mailer' => "AgoraCart Route 66");

        $smtps ->mail("$fromuser");
        $smtps->to("$touser");
        $smtps->data();
        $smtps->datasend( $msg2->as_string() );
        $smtps->dataend();
        $smtps->quit; # done sending
    }

    # no error checking, assume it worked
    return '';

}

#########################################################################

1;
