是的,PHP可以实现邮件群发功能。您可以使用PHPMailer这个流行的邮件库来实现群发邮件。以下是使用PHPMailer发送群发邮件的基本步骤:
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);
}
请注意,根据您所在地区的法律法规,发送商业邮件可能需要获得收件人的明确许可,并且可能需要遵守特定的发送规定。此外,确保您的邮件服务器设置正确,并且您有权使用该服务器发送邮件。