Sending Emails with GMail's SMTP Server from PHP
Issue:
When attempting to send an email using GMail's SMTP server from a PHP page, an error occurs, indicating authentication failure in SMTP.
Solution:
The provided code utilizes the Pear Mail Library, which requires adjustments for GMail's SMTP:
Updated Code:
require_once "Mail.php"; $from = '<[email protected]>'; $to = '<[email protected]>'; $subject = 'Hi!'; $body = "Hi,\n\nHow are you?"; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject ); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => '[email protected]', 'password' => 'passwordxxx' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo('<p>' . $mail->getMessage() . '</p>'); } else { echo('<p>Message successfully sent!</p>'); }
By incorporating these changes, the code should now successfully send emails through GMail's SMTP server from the PHP page.
The above is the detailed content of Why is my PHP email sent via Gmail's SMTP server failing, and how can I fix it using the Pear Mail library?. For more information, please follow other related articles on the PHP Chinese website!