批量发送邮件php源码
时间: 2023-09-24 22:14:25 浏览: 40
以下是一个简单的PHP脚本,可以使用SMTP协议批量发送邮件:
```php
<?php
// 邮件发送函数
function sendMail($to, $subject, $body) {
$host = 'smtp.example.com'; // SMTP服务器地址
$username = 'your_username'; // SMTP用户名
$password = 'your_password'; // SMTP密码
$headers = array(
'From' => 'your_email@example.com',
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8'
);
$smtp = new SMTP($host, 25); // 创建SMTP对象
$smtp->setAuth($username, $password); // 设置SMTP认证信息
$smtp->setHeaders($headers); // 设置邮件头
$smtp->setBody($body); // 设置邮件内容
return $smtp->send(); // 发送邮件
}
// 要发送的邮件列表
$emails = array('email1@example.com', 'email2@example.com', 'email3@example.com');
// 发送邮件的循环
foreach ($emails as $email) {
$result = sendMail($email, '邮件主题', '邮件内容');
if ($result) {
echo '邮件发送成功:' . $email . '<br>';
} else {
echo '邮件发送失败:' . $email . '<br>';
}
}
?>
```
这个脚本使用了一个名为SMTP的自定义类,需要先定义这个类。以下是SMTP类的代码:
```php
class SMTP {
private $host; // SMTP服务器地址
private $port; // SMTP服务器端口
private $username; // SMTP用户名
private $password; // SMTP密码
private $headers; // 邮件头
private $body; // 邮件内容
public function __construct($host, $port) {
$this->host = $host;
$this->port = $port;
}
public function setAuth($username, $password) {
$this->username = $username;
$this->password = $password;
}
public function setHeaders($headers) {
$this->headers = $headers;
}
public function setBody($body) {
$this->body = $body;
}
public function send() {
$socket = fsockopen($this->host, $this->port, $errno, $errstr);
if (!$socket) {
return false;
}
// 建立连接
$response = fgets($socket);
if (substr($response, 0, 3) != '220') {
return false;
}
fputs($socket, "EHLO " . $this->host . "\r\n");
while ($response = fgets($socket)) {
if (substr($response, 3, 1) != '-') {
break;
}
}
if (substr($response, 0, 3) != '250') {
return false;
}
// 身份认证
fputs($socket, "AUTH LOGIN\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '334') {
return false;
}
fputs($socket, base64_encode($this->username) . "\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '334') {
return false;
}
fputs($socket, base64_encode($this->password) . "\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '235') {
return false;
}
// 设置邮件头和内容
$headers = '';
foreach ($this->headers as $key => $value) {
$headers .= $key . ': ' . $value . "\r\n";
}
$headers .= "\r\n" . $this->body . "\r\n";
// 发送邮件
fputs($socket, "MAIL FROM: <" . $this->headers['From'] . ">\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '250') {
return false;
}
fputs($socket, "RCPT TO: <" . $this->headers['To'] . ">\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '250') {
return false;
}
fputs($socket, "DATA\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '354') {
return false;
}
fputs($socket, $headers . "\r\n.\r\n");
$response = fgets($socket);
if (substr($response, 0, 3) != '250') {
return false;
}
// 关闭连接
fputs($socket, "QUIT\r\n");
fclose($socket);
return true;
}
}
```
请注意,这个脚本只是一个例子,需要根据实际情况进行修改和调整。特别是SMTP服务器的设置,需要根据实际情况进行调整。