在Ubuntu上配置PHP以发送电子邮件可以通过多种方式实现,包括使用命令行工具、配置本地邮件传输代理(如Postfix)或使用第三方服务(如SendGrid、Mailgun等)。以下是使用Postfix和PHPMailer库来配置邮件发送功能的步骤:
安装Postfix 打开终端并运行以下命令来安装Postfix:
sudo apt update
sudo apt install postfix
配置Postfix 安装完成后,Postfix会提示你选择配置类型。选择“Internet Site”并输入你的系统邮件名称(通常是你的域名)。
配置PHPMailer 安装PHPMailer库,可以使用Composer:
composer require phpmailer/phpmailer
编写PHP脚本发送邮件
创建一个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 = 'localhost'; // Set the SMTP server to send through
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Port = 587; // TCP port to connect to; use 587 if you have set `SMTPSecure = phpmailer/smtp::ENCRYPTION_STARTTLS`
$mail->SMTPSecure = PHPMailer\PHPMailer\SMTP::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption
// Recipients
$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}";
}
运行PHP脚本 在终端中运行以下命令来发送邮件:
php send_email.php
如果你不想配置本地邮件服务器,可以使用第三方邮件服务提供商,如SendGrid或Mailgun。以下是使用SendGrid的示例:
注册SendGrid账户 访问SendGrid官网并注册一个账户。
获取API密钥 在SendGrid控制台中找到你的API密钥。
安装PHPMailer库 使用Composer安装PHPMailer库:
composer require phpmailer/phpmailer
编写PHP脚本发送邮件
创建一个PHP文件(例如send_email_sendgrid.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.sendgrid.net'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Port = 587; // TCP port to connect to; use 587 if you have set `SMTPSecure = phpmailer/smtp::ENCRYPTION_STARTTLS`
$mail->SMTPSecure = PHPMailer\PHPMailer\SMTP::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption
$mail->AuthType = 'XOAUTH2'; // Use OAuth2 Authentication
// Credentials
$mail->setOAuthNewApp('YOUR_SENDGRID_API_KEY');
// Recipients
$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}";
}
运行PHP脚本 在终端中运行以下命令来发送邮件:
php send_email_sendgrid.php
通过以上步骤,你可以在Ubuntu上配置PHP以发送电子邮件。选择适合你需求的方法进行配置即可。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何配置Ubuntu PHP邮件发送功能