1

PHPMAILER is working properly on my site. What I would like to do is separate the config portion into a separate function so as I create different response emails all I need to do is call the emailConfig() function inside the different response functions.

function continuedInquiry() {
    //config portion I want to separate
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';

    /**
      *rest of the phpmailer code
    */
    $mail->send();

    notify();
}

function notify() {

    //notification email
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';

    /**
      *rest of the phpmailer code
     */
}

This works as it should, but because I'm using multiple mailers, I'd like to separate the configuration portion into a separate function, emailConfig(), like this:

function emailConfig() {
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
}

and call it in other mailer functions:

function continuedInquiry() {
    emailConfig();

    /**
      *rest of the phpmailer code
    */
    $mail->send();

    notify();
}

//and so on

But I keep getting an error saying that $mail is not defined:

enter image description here

I've tried return, I've tried arguments. This would simplify things, but I can't get it to work.

1 Answer 1

3

You might find it easier to use a subclass to configure it, like this:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

class myMailer extends PHPMailer
{
    public function __construct($exceptions = null)
    {
        $this->isSMTP();
        $this->SMTPDebug = SMTP::DEBUG_OFF;
        $this->Host = 'smtp.gmail.com';
        $this->Port = 587;
        $this->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $this->SMTPAuth = true;
        $this->Username = '[email protected]';
        $this->Password = 'password';
        parent::__construct($exceptions);
    }
}

Then in your script you can just do:

$mail = new myMailer(true);

and it will have all the config done, ready to use.

That said, it's a good idea to move "secrets" such as your password out of your code into external environment variables or config files, so that you don't end up pushing passwords into your git repo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.