ThinkPHP 是一个流行的 PHP 开发框架,它提供了丰富的功能和组件来简化 Web 应用的开发过程。邮件功能是 ThinkPHP 中的一个常用模块,用于发送电子邮件。
以下是一个使用 ThinkPHP 发送 SMTP 邮件的示例代码:
<?php
namespace app\index\controller;
use think\Controller;
use think\mail\Driver\Smtp;
class MailController extends Controller
{
public function send()
{
$config = [
'host' => 'smtp.example.com',
'port' => 587,
'username' => 'your_email@example.com',
'password' => 'your_password',
'from' => 'your_email@example.com',
'fromName' => 'Your Name',
];
$smtp = new Smtp($config);
$message = [
'to' => 'recipient@example.com',
'subject' => 'Test Email',
'body' => '<h1>Hello, World!</h1>',
'isHtml' => true,
];
$result = $smtp->send($message);
if ($result) {
echo '邮件发送成功';
} else {
echo '邮件发送失败';
}
}
}
From
、Reply-To
等。通过以上方法,可以有效解决 ThinkPHP 邮件发送过程中遇到的常见问题。
领取专属 10元无门槛券
手把手带您无忧上云