简体   繁体   中英

Unable to Send Email Via PHP using SMTP

This is my email.php file:

 <?php

    $to = "someemail@gmail.com";
    $from = "myemail@gmail.com";

    $subject="Email";
      $body="hi";  
    $headers = array(
        'From' => $from,
        'To' =>  $to,
        'Subject' => $subject
        );

             $smtp = Mail::factory('smtp', array(
                    'host' => 'ssl://smtp.gmail.com',
                    'port' => '465',
                    'auth' => true,
                    'username' => 'myemail@gmail.com',
                    'password' => 'password'
                ));
            $mail = $smtp->send( $to, $headers, $body);

            if (PEAR::isError($mail)) {
                echo('<p>' . $mail->getMessage() . '</p>');
            } else {
                echo('<p>Message successfully sent!</p>');
            }

?>      

I have enabled access to less secure apps, DisplayUnlockCaptcha as well as IMAP. I have not enabled 2-step verification in the account. However, I still get the following error.

authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 i28sm32905934pfk.17 - gsmtp)]

I am using XAMPP in Windows and this is what I've put in my php.ini file:

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = localhost
smtp_port = 465
; For Win32 only.
sendmail_from = myemail@gmail.com

If someone could help me resolve this issue I'd be extremely grateful!

I suggest a different approach using SwiftMailer.

You can easily get the component with a json file and see how that goes. http://swiftmailer.org/docs/introduction.html#basic-usage

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