在CentOS上配置Laravel郵件服務,你需要遵循以下步驟:
sudo yum install postfix
/etc/postfix/main.cf
,根據你的需求進行配置。例如,你可以設置以下參數:myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/
保存并退出文件。然后重啟Postfix服務:
sudo systemctl restart postfix
安裝Laravel 如果你還沒有安裝Laravel,請按照官方文檔的說明進行安裝:https://laravel.com/docs/8.x/installation
配置Laravel郵件服務
打開Laravel項目的 .env
文件,找到以下行:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@example.com
MAIL_FROM_NAME="${APP_NAME}"
根據你的郵件服務提供商的要求,填寫相應的配置信息。例如,如果你使用的是Mailtrap,你需要注冊一個賬戶并獲取SMTP憑據。
php artisan make:mail TestMail
這將創建一個名為 TestMail
的Mailable類。打開 app/Mail/TestMail.php
文件,編輯 build
方法,添加你想要發送的郵件內容:
public function build()
{
return $this->view('emails.test');
}
接下來,創建一個視圖文件 resources/views/emails/test.blade.php
,并添加以下內容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Email</title>
</head>
<body>
<h1>Hello, this is a test email!</h1>
</body>
</html>
現在,你可以使用以下命令發送測試郵件:
php artisan mail:send --to your_email@example.com --subject "Test Email"
如果一切配置正確,你應該會收到一封測試郵件。