Sending mail with PHPMailer... reloaded (issues with mail() too)


  • | 2043 points

    Re: how send emails with PHPMailer?

    Hello,
    I read the topic above and I'd like to reply for a similar problem, but the post was too old and I decided to open another thread

    I'm using PHPMailer 6.* in a generic PHP app (not Laravel), so I loaded it via composer.
    With the following very basic code the mail catcher works (the mail is created and I can read it) but the error message Mailer Error: Could not instantiate mail function. is still printed out:

    use PHPMailer\PHPMailer\PHPMailer;
    
    require_once('../vendor/autoload.php');
    $mail = new PHPMailer();
    $mail->CharSet = 'UTF-8';
    $mail->setFrom('myrealmail@gmail.com', 'Sender');
    $mail->Subject = 'This is a test';
    $mail->MsgHTML('Hello, this is a test for sending mail');
    $mail->AddAddress('myrealmail@gmail.com', 'Receiver');
    if(!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message sent successfully!';
    }
    

    I use my real Gmail address instead of myrealmail@gmail.com
    I did not try in production environment, only locally with Laragon

    The Mail Analyzer (Menu > Preference > Mail Sender > Test Sending Email) is successfully setup - but it shouldn't be an issue since I'm relying on simple PHP mail() function, right? But, strangely, the error.log file says Authentication Required. Learn more at<EOL> https://support.google.com/mail/?p=WantAuthError v10-v6sm22495927wrm.18 - gsmtp<EOL>

    While I write, though, I tested also a simple mail() (so no PHPMailer bloat):

    $headers = 'From: myrealmail@gmail.com';
    mail('myrealmail@gmail.com', 'TEST', 'This is a test', $headers);
    

    Still, the mail catcher works (the mail is created, this time in a text format obviously) but the error.log shows the same message: Authentication Required. Learn more at<EOL> https://support.google.com/mail/?p=WantAuthError l15-v6sm12355859wrt.67 - gsmtp<EOL>; this seems very strange because mail() shouldn't require any authentication...

    Please, any clue? Can we use the native PHP mail() function or is it forbidden for some strange reason (maybe is it an issue on PHP 7.* ?)
    Thank you :-)


  • administrators
    | 93974 points


  • | 2043 points

    @leokhoa : thanks, I'll try that (at the moment I used mailtrap.io with Laravel)


Log in to reply
 

Looks like your connection to Laragon was lost, please wait while we try to reconnect.