在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以發送電子郵件。選擇適合你需求的方法進行配置即可。