简体   繁体   中英

PHPMailer not working when trying to create using a PHP Function

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?

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM