5

I made a custom class function for setting up the basic information needed by PHPMailer (so I don't need to input it in everytime). Here is the exact code of the function.

<?php

class PHPMailer {

    public static function send() {// I will just add here the addAddress
        require_once 'mail/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 587;
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth = true;
        $mail->Username = "validusername";
        $mail->Password = "validpassword";
        $mail->setFrom('validusername', 'Valid Username');
        $mail->addAddress('googol8080@gmail.com', 'Googol');

        $mail->Subject = "Subject";
        $mail->Body    = "<a href=\"www.google.com\">www.google.com</a>";
        $mail->IsHTML(true);

        if (!$mail->send()) {
            return "Error sending message" . $mail->ErrorInfo;
        } else {
            return "Message sent!";
        }
    }
}

So far it is working on my localhost, but I have questions:

  • Is this a good practice?
  • Is the code okay?
  • Is there any drawback/s about this?
  • If there is need to optimize here for performance, what do I need to do to make it happen?

I am really new in PHP and PHPMailer any small answers might help me, thanks.

1 Answer 1

3

Your Code seems fine, but a better way could be to call variables, so you dont have to configure it everytime you want to call the class.

class phpmailer {
    public function sendMail($email, $message, $subject)
    {
        require_once('../phpmailer/class.phpmailer.php');
        require_once('../phpmailer/class.smtp.php');
        require_once('../phpmailer/class.pop3.php');
        $mail = new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465;
        $mail->addAddress($email);
        $mail->Username = "email@gmail.com";
        $mail->Password = "email_password";
        $mail->setFrom('email_Sent_from@gmail.com', 'Alias');
        $mail->addReplyTo("email_to@gmail.com", "Alias");
        $mail->Subject = $subject;
        $mail->msgHTML($message);
        $mail->send();
    }
}

then you can call it like:

$email_send = new phpmailer();
$email_send->sendMail($user_email,$message,$subject);
7
  • Can I ask what is the purpose of those 3 require_once ? Thanks. Dec 13, 2016 at 8:34
  • 1
    the first is for the php mailer class, the second and third are for smtp and pop3 if you want to use them Dec 13, 2016 at 8:36
  • Ah okay, thanks for that, btw, when I tried to send a simple email (localhost) and it takes upto 2 seconds to send. Is that okay? I mean I am using a localhost and it takes upto 2 seconds, what will happen if I use that in production (shared hosting) ? Might take upto 2 - 5 secods? Dec 13, 2016 at 9:21
  • 1
    thats normal dude :) Dec 13, 2016 at 9:22
  • 1
    In this case, what's the advantage of building a class, instead of using a simple function? Apr 3, 2021 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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