Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Geronimo_man's avatar

Force Laravel to use PhpMailer

Hi all .

I have a problem that breaks my head, from a controller I try to use phpmailer to send emails, several emails must be sent from SEVERAL SMTP SERVER in order to check if the smtp's of the list works, the list is sent from a classic form (.balde), each time the controller try to send an email with phpmailer sendmail i used and no phpmailer, I output the logs in a log file to see the result and I see that it is actually not possible to send emails with phpmailer, sendmail is systematically used.

here is the controller:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class SmtpCheckerController extends Controller {
public function checkSmtpList(Request $request) {
    $user = Auth::user();
    $fileName = $request->input('file');
    $uid = $user->id;

    // Get file path using $uid and $file parameters
    $filePath = base_path("smtps/{$fileName}");

    // Check if the file exists
    if (File::exists($filePath)) {
        // Lisez le fichier et effectuez le traitement nécessaire
        $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        foreach ($lines as $line) {

            $parts = explode(":", trim($line));
            // Instantiate the PHPMailer class
            $mail = new PHPMailer(true);

            // Configure SMTP settings
            $mail->Host = $parts[0];
            $mail->Username = $parts[1];
            $mail->Password = $parts[2];
            $mail->SMTPSecure = $parts[3];
            $mail->Port = $parts[4];

            // Enable SMTP debugging
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;
            $mail->Debugoutput = function($str, $level) {
				file_put_contents('journal.log', $str, FILE_APPEND);
            };

            try {
                // Configurations de l'e-mail
                $mail->setFrom("[email protected]", "xx xx");
                $mail->addAddress("[email protected]", "xx");
                $mail->addReplyTo("[email protected]", "xx");
                $mail->Subject = "testing";
                $mail->Body = "testing";
                $mail->isHTML(true);

                // Send Email
                if ($mail->send()) {
                    $notify[] = ['success', 'ok'];
                    return back()->withNotify($notify);
                } else {
                    return back()->with('error', $mail->ErrorInfo);
                }
            } catch (Exception $e) {
                return back()->with('error', $e->getMessage());
            }
        }
    } else {
        $notify = "Could not find the list '$file'";
        return back()->withNotify($notify);
    }
}
}

here is journal.log :

Sending with mail()Sendmail path: /usr/sbin/sendmail -t -iEnvelope sender: [email protected] To: xx <[email protected]>Subject: xxxxx Headers: Date: Fri, 1 Sep 2023 19:05:50 +0200
From: xx xx <[email protected]>
Reply-To: xx <[email protected]>
Message-ID: <[email protected]>
X-Mailer: PHPMailer 6.8.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/html; charset=iso-8859-1

Additional params: -f [email protected] Result: true

sendmail is used and we can see "X-Mailer: PHPMailer 6.8.1...." , I don't understand what's going on

ps : for the moment this function operates only on the first line (the first smtp) of the list, I try to solve this problem before writing all the logic of this function...

thanks for help

0 likes
1 reply

Please or to participate in this conversation.