0

I am using PHPMailer for sending emails. But I have to write that long code of sending emails on every page. So I thought of trying to put the whole stuff into a function and just calling it whenever I want to make the things dry, simple and easier. But its not working when trying to send emails. I tried the following:

functions.php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$settings = $pdo->prepare("SELECT * FROM settings");
$settings-> execute();
$set = $settings->fetch();

function newMail($name, $email, $sub, $msg, $set) {
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 0;
  $mail->Host = $set['set_smtp_host'];
  $mail->Port = $set['set_smtp_port'];
  $mail->SMTPSecure = $set['set_smtp_security'];
  $mail->IsHTML(true);
  $mail->SMTPAuth = true;
  $mail->Username = $set['set_smtp_uname'];
  $mail->Password = $set['set_smtp_pass'];
  $mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
  $mail->addAddress($email, $name);
  $mail->Subject = $sub;
  $mail->Body    = $msg;
  $mail->Send();
}

Now I tried calling the function on another page (where functions.php is included) this way:

$fname = (!empty($_POST['fname']))?$_POST['fname']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;

$sub = ''.$title.' - Account Verification Link';
$msg = 'SOME BODY MESSAGE';

if(newMail($fname, $email, $sub, $msg)){
    echo alert_success("Registration successful! Please check your email and click on the activation link to activate your account. If you did not receive any email within 5 minutes then <a href='resend.php'>click here</a> to resend it.");
}else{
    echo alert_success("Registration successful! But unfortunately, we could not send you a verification email. Please <a href='resend.php'>click here</a> to resend it.");
}

Here its always returning the else message. Am I wrong coding something here?

10
  • 3
    Your function newMail() does not return 'true' or 'false', it returns nothing. Commented Apr 3, 2019 at 15:10
  • did this code work before make it inside a separated function ? Commented Apr 3, 2019 at 15:11
  • yes it works when its outside the function separately Commented Apr 3, 2019 at 15:14
  • 1
    The first problem is, as @KIKOSoftware stated, newMail() function returns nothing. The second problem is, you included PHPMailer library outside of newMail() function. Commented Apr 3, 2019 at 15:17
  • 1
    @SuperGENScript Oh sorry. You aren't sending $set variable as a parameter when you execute newMail() function. Commented Apr 3, 2019 at 15:36

1 Answer 1

2

Modify your function newMail at the end by :

return   $mail->Send();

The send method return true in case the mail is sent , so you function should return this value , if not :

if(newMail(...)){ }

Will always be false that why the else case is applied.

function newMail($name, $email, $sub, $msg, $set) {
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 0;
  $mail->Host = $set['set_smtp_host'];
  $mail->Port = $set['set_smtp_port'];
  $mail->SMTPSecure = $set['set_smtp_security'];
  $mail->IsHTML(true);
  $mail->SMTPAuth = true;
  $mail->Username = $set['set_smtp_uname'];
  $mail->Password = $set['set_smtp_pass'];
  $mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
  $mail->addAddress($email, $name);
  $mail->Subject = $sub;
  $mail->Body    = $msg;
  return $mail->Send();  // add return here 
}
Sign up to request clarification or add additional context in comments.

4 Comments

I firgured it out just seconds before you corrected. But thanks :)
And can't I put use PHPMailer thing inside the function? If placed it returns ` Parse error: syntax error, unexpected 'use' (T_USE) in`
Class definitions are global within a namespace, so there's no need to put use PHPMailer inside the function. It could be needed inside the include file.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.