phpmailerHow can I use PHPMailer to connect to a POP3 server?
PHPMailer can be used to connect to a POP3 server using the POP3 protocol. This can be done using the getMail()
method. Here is an example code block:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
//Server settings
$mail->Host = 'pop.example.com';
$mail->Port = 110;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'secret';
//Connect to the POP3 server
try {
$mail->getMail();
echo 'Connected to POP3 server';
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
The output of this code will be: Connected to POP3 server
.
Code explanation
use PHPMailer\PHPMailer\PHPMailer;
- imports the PHPMailer class into the global namespace.use PHPMailer\PHPMailer\Exception;
- imports the PHPMailer Exception class into the global namespace.require 'vendor/autoload.php';
- loads the Composer autoloader.$mail = new PHPMailer(true);
- creates a new instance of the PHPMailer class and enables exceptions.$mail->Host = 'pop.example.com';
- sets the POP3 server hostname.$mail->Port = 110;
- sets the POP3 server port.$mail->SMTPAuth = true;
- enables SMTP authentication.$mail->Username = '[email protected]';
- sets the POP3 username.$mail->Password = 'secret';
- sets the POP3 password.$mail->getMail();
- connects to the POP3 server.
Helpful links
More of Phpmailer
- How can I use PHPMailer with React?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer to attach a ZIP file?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to support Polish characters?
- How can I use PHPMailer to send emails from my WordPress site?
- How do I view the log for my PHPMailer emails?
- How can I use PHPMailer in Yii 1?
- How do I use PHPMailer with IMAP?
- How do I check which version of PHPMailer I'm using?
See more codes...