Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Optimising and consolidating multiple PHPMailer functions Reply To: Optimising and consolidating multiple PHPMailer functions

#167757
__
Participant

Would it be something likeā€¦

Pretty much, yeah.

Question: where you have the variable $mail, are you actually doing anything with it beforehand? or are you just doing something like

$mail = new PHPmailer;

sendEmail( $subject,$to,$message,$mail );

? if so, you could just make the new PHPmailer object inside the sendEmail function.

public function sendEmail( $to, $subject, $message ){
        try {
            $mail = new PHPmailer;
            $mail->IsSMTP(); 
            $mail->Username           = "[email protected]"; 
            $mail->Password           = "********"; 
            $mail->SMTPAuth           = true;            
            $mail->SMTPSecure         = "tls"; 
            $mail->Host               = "smtp.gmail.com";  
            $mail->Port               = 587; 
            $mail->addAddress( $to );  
            $mail->From               = '[email protected]';
            $mail->FromName           = 'Connectd.io';
            $mail->AddReplyTo( '[email protected]', 'Contact Connectd.io' );
            $mail->isHTML(true); 
            $mail->MsgHTML( $message );
            $mail->Subject = $subject;
            $mail->Send();
        }catch(Exception $e) {
            $general = new General($db);
            $general->errorView($general, $e);
        }
    }

(also, there’s no reason to have two catch blocks if they don’t do anything differently.)