Send email by Outlook in php Mailer
1. Enable Less Secure Apps Access (Not Recommended - Security Risk):
Warning: This approach is generally not recommended due to security concerns. It allows access from any less secure app, including potentially malicious ones. Consider alternative methods (2 or 3) if possible.
- Sign in to your Microsoft account and navigate to Security Settings: https://account.microsoft.com/account/manage-my-account
- Under "Advanced Security," select "Turn on two-step verification" (recommended for overall account security).
- Then, enable "Less secure apps" access (use with caution).
2. Use an App Password (Recommended):
- Generate an app password specifically for PHPMailer in your Microsoft account security settings.
- This password will be used in place of your regular Outlook password for enhanced security.
3. Configure PHPMailer:
PHP
<?php
require 'vendor/autoload.php'; // Assuming you have Composer installed
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true); // Set to true to enable exceptions
try {
// Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output (optional)
$mail->isSMTP();
$mail->Host = 'smtp.office365.com'; // Use 'smtp-mail.outlook.com' for some regions
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
// Authentication (use app password if enabled)
$mail->Username = 'your_outlook_email@outlook.com';
$mail->Password = 'your_app_password'; // Use app password if applicable
// Sender and recipient
$mail->setFrom('your_outlook_email@outlook.com', 'Your Name');
$mail->addAddress('recipient_email@example.com', 'Recipient Name');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject of your email';
$mail->Body = 'This is the email content in HTML format.';
$mail->send();
echo 'Message has been sent successfully.';
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
?>
Comments
Post a Comment