是的,PHP可以實現(xiàn)郵件群發(fā)功能。您可以使用PHPMailer這個流行的郵件庫來實現(xiàn)群發(fā)郵件。以下是使用PHPMailer發(fā)送群發(fā)郵件的基本步驟:
composer require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@example.com'; // SMTP username
$mail->Password = 'your_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient1@example.com', 'Recipient 1 Name'); // Add a recipient
$mail->addAddress('recipient2@example.com', 'Recipient 2 Name'); // Add another 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->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Messages have been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$recipients = [
'recipient1@example.com',
'recipient2@example.com',
// ...
];
foreach ($recipients as $recipient) {
$mail->addAddress($recipient);
}
請注意,根據(jù)您所在地區(qū)的法律法規(guī),發(fā)送商業(yè)郵件可能需要獲得收件人的明確許可,并且可能需要遵守特定的發(fā)送規(guī)定。此外,確保您的郵件服務(wù)器設(shè)置正確,并且您有權(quán)使用該服務(wù)器發(fā)送郵件。