PHPMailer Fatal Error

[B]Good morning,

I get the following error when trying to use PHPMailer. I have successfully created and added this as an external library for this project but am not sure what I am missing

Fatal error[/B]: Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use in /Applications/Scriptcase/v9/wwwroot/scriptcase/app/DEVInvSecurity/_lib/libraries/grp/emailer/PHPMailer-master/src/Exception.php on line 39

This is the code in my FORM in the onAfterInsert event

sc_include_library(“prj”, “emailer”,“PHPMailer-master/src/Exception.php”, true, true);

sc_include_library(“prj”, “emailer”,“PHPMailer-master/src/PHPmailer.php”,true, true);

sc_include_library(“prj”, “emailer”,“PHPMailer-master/src/SMTP.php”,true, true);

//PHP code for sending the email
customMail();

Any tips for the right direction to go would be appreciated.

Good afternoon,

Thank you for this.

I did delete and add the library back in as a public library and I followed the steps put forth in your blog. My code is now this:

require_once sc_url_library(“sys”, “php_mailer”,“PHPMailer-master/src/Exception.php”);

require_once sc_url_library(“sys”, “php_mailer”,“PHPMailer-master/src/PHPmailer.php”);

require_once sc_url_library(“sys”, “php_mailer”,“PHPMailer-master/src/SMTP.php”);

but I still get the same error.
What am I missing?

Thanks

Keith

I use it like this:

sc_include_library(“prj”, “PHPMailer”, “src/PHPMailer.php”, true, true);
sc_include_library(“prj”, “PHPMailer”, “src/Exception.php”, true, true);
sc_include_library(“prj”, “PHPMailer”, “src/SMTP.php”, true, true);

Good evening RIK,

In my original post that I did have it setup that way (of course with my library and path information) but was still getting this error. Could it be that I must you the exact pathing and library name that you have in yours?

Regards,

Keith

Good morning KeithK,

some of the confusion might result from different versions of PHPmailer.

In version 5.x you simply can include the libraries and use PHPmailer. You will not need Exception.php in version 5.x of PHPmailer.
From version 6.x you’ll have to take care of the namespaces. This is more complicated in Scriptcase, but you’ll bump into name confilcts not doing this.

Hope this helps out a little bit :wink:

Sincerely

Gunter Eibl

No. If your path is correct, you should work.
I do not know the whole of your code, so I’ll post my code that has been working for several years.
It is used in a blank app. Sends to several email addresses and add a * .pdf document as an attachment.

Regards,

sc_include_library(“prj”, “PHPMailer”, “src/PHPMailer.php”, true, true);
sc_include_library(“prj”, “PHPMailer”, “src/Exception.php”, true, true);
sc_include_library(“prj”, “PHPMailer”, “src/SMTP.php”, true, true);

if(!empty({loc_email})){
$l_emails = {loc_email};
}

if(!empty({usr_send_email}) && !empty($l_emails)){
$l_emails .= “;”.{usr_send_email};
}elseif(!empty({usr_send_email}) && empty($l_emails)){
$l_emails = {usr_send_email};
}

$param_message= {lang_email_body};

if(!empty(trim($l_emails))){
try {
$mail = new PHPMailer\PHPMailer\PHPMailer;
$mail->IsSMTP();
$mail->SMTPDebug=0;
$mail->SMTPAuth= TRUE;
$mail->SMTPSecure=“ssl”;
$mail->Port=465;
$mail->CharSet = ‘UTF-8’;
$mail->Username="report@my-domain.com";
$mail->Password=“xxxxxxxxxx”;
$mail->Host=“reseller03.my-servername.com”;
$mail->Mailer=“smtp”;
$mail->SetFrom(“report@my-domain.com”,“MY Portal”);
$mail->AddReplyTo(“user.name@my-domain.com”,“User Name”);

    $destinations = explode(';', $l_emails);
    $n = 0;
    foreach ($destinations as $destination){
        if ($n==0) {
            $mail->AddAddress($destination, $destination);
        } else {
            $mail->AddCC($destination, $destination);
        }
        $n++;            
    }                
    $mail->Subject= {lang_report1_name};
    $mail->WordWrap=80;
    $content = $param_message;
    $mail->MsgHTML($content);
    $mail->IsHTML(true);

    $at_path=$this->Ini->path_doc;
    $doc_name = {country_id_2}."-".{location_id}."-".[g_id];    
    $attch=$at_path."-".$doc_name.'.pdf';
    $mail->AddAttachment($attch);
    if (!$mail->Send()) {
        echo $mail->ErrorInfo;
    }

} catch (PHPMailer\PHPMailer\PHPMailerException $e) {
    echo $e->errorMessage();
} catch (Exception $e){
    echo $e->getMessage();
}

}