ThinkPHP发送邮件

本文介绍如何使用ThinkPHP框架配置并发送邮件。通过修改function.php文件中的PHPMailer配置,包括SMTP服务器、端口、加密方式等,成功实现邮件发送功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

成功发送邮件历程:
按照 http://www.thinkphp.cn/code/989.html 来做 将文件创建好并且代码粘到自己想用的位置
http://www.thinkphp.cn/code/download/id/989.html
1.以上文件放在 \ThinkPHP\Library\Vendor
2.在 \Application\Home\Common 下按照教程创建function.php
这里注意 function.php中内容有更改 官方未添加加密模式和端口所以在使用过程中一只返回false
更改内容为:
作者在下载的文件夹中阅读了一下README.md文件 发现有全部的代码:
<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // 启用详细调试输出

$mail->isSMTP(); // 设置使用SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // 指定主和备份的SMTP服务器。
$mail->SMTPAuth = true; // 使SMTP认证
$mail->Username = 'user@example.com'; // SMTP用户名
$mail->Password = 'secret'; // SMTP密码
$mail->SMTPSecure = 'tls'; // 启用TLS加密,“ssl”也被接受。
$mail->Port = 587; // 连接到TCP端口

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // 增加一个收件人
$mail->addAddress('ellen@example.com'); // 名称是可选的
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz'); // 添加附件
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // 可选的名字
$mail->isHTML(true); // 将电子邮件格式设置为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';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

参照以上代码, 添加以下代码进行端口设置, 添加之后便可以发送邮件 代码返回true
tls -----25
ssl ------465
$mail->SMTPSecure = 'tls'; // 启用TLS加密,“ssl”也被接受。
$mail->Port = 25; // 连接到TCP端口


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值