phpmailerHow can I use PHPMailer to send emails with a Yahoo account?
To use PHPMailer to send emails with a Yahoo account, you will need to set the SMTP host, port, and authentication settings. Here is an example code block to do so:
<?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';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify Yahoo SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'YOUR_YAHOO_EMAIL_ADDRESS'; // SMTP username
$mail->Password = 'YOUR_YAHOO_EMAIL_PASSWORD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('YOUR_YAHOO_EMAIL_ADDRESS', 'Your Name');
$mail->addAddress('RECIPIENT_EMAIL_ADDRESS', 'Recipient Name'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
This example code block will set the SMTP host to smtp.mail.yahoo.com
, enable SMTP authentication, set the username and password to your Yahoo email address and password, set the encryption to TLS, and set the port to 587. The code will also set the sender and recipient email addresses, set the subject and body of the email, and send the email. If the email is successfully sent, the output will be Message has been sent
.
Code explanation
use PHPMailer\PHPMailer\PHPMailer;
anduse PHPMailer\PHPMailer\Exception;
: These two lines import the PHPMailer classes into the global namespace.require 'vendor/autoload.php';
: This line loads the Composer autoloader.$mail->SMTPDebug = 2;
: This line enables verbose debug output.$mail->isSMTP();
: This line sets the mailer to use SMTP.$mail->Host = 'smtp.mail.yahoo.com';
: This line sets the SMTP host tosmtp.mail.yahoo.com
.$mail->SMTPAuth = true;
: This line enables SMTP authentication.$mail->Username = 'YOUR_YAHOO_EMAIL_ADDRESS';
and$mail->Password = 'YOUR_YAHOO_EMAIL_PASSWORD';
: These two lines set the username and password to your Yahoo email address and password.$mail->SMTPSecure = 'tls';
: This line sets the encryption to TLS.$mail->Port = 587;
: This line sets the port to 587.$mail->setFrom('YOUR_YAHOO_EMAIL_ADDRESS', 'Your Name');
and$mail->addAddress('RECIPIENT_EMAIL_ADDRESS', 'Recipient Name');
: These two lines set the sender and recipient email addresses.$mail->isHTML(true);
: This line sets the email format to HTML.$mail->Subject = 'Here is the subject';
and$mail->Body = 'This is the HTML message body <b>in bold!</b>';
: These two lines set the subject and body of the email.$mail->send();
: This line sends the email.echo 'Message has been sent';
: This line prints the outputMessage has been sent
if the email is successfully sent.
Helpful links
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How do I set the X-Mailer header using PHPMailer?
- How do I disable STARTTLS in PHPMailer?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer with Yii2?
- How can I use PHPMailer without SMTP secure?
- How do I configure the timeout settings for PHPMailer?
- How do I configure PHPmailer to use Zoho SMTP?
See more codes...