在Debian系统上,您可以使用PHPMailer库来发送电子邮件。以下是使用PHPMailer发送电子邮件的步骤:
安装PHPMailer: 通过Composer安装PHPMailer。首先,确保您已经安装了Composer。如果没有,请访问https://getcomposer.org/download/ 并按照说明进行安装。
在项目目录中运行以下命令来安装PHPMailer:
composer require phpmailer/phpmailer
创建一个PHP文件:
在您的项目中创建一个新的PHP文件,例如send_email.php。
编写发送邮件的代码:
在send_email.php文件中,编写以下代码来发送电子邮件:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'login'; // SMTP authentication type
$mail->Port = 587; // TCP port to connect to; use 465 for `SMTPS`
$mail->SMTPSecure = 'tls'; // Enable TLS encryption; `SMTPS` also accepted
// Sender and recipient
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', '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->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
请根据您的实际情况修改SMTP服务器设置、发件人和收件人信息以及邮件内容。
运行脚本:
在终端中,导航到包含send_email.php文件的目录,然后运行以下命令:
php send_email.php
如果一切正常,您应该会看到“Message has been sent”的输出,并且收件人将收到一封电子邮件。
注意:在实际部署之前,请确保您使用的SMTP服务器和凭据是正确的。如果您使用的是Gmail或其他第三方邮件服务提供商,请查阅其文档以获取正确的SMTP设置。