php eamil 扩展,扩展CanPHP中的Email类,支持所有发送方式

这篇博客介绍了如何扩展CanPHP内置的Email类,以支持除了SMTP通过SMTPSOCKET连接发送之外的另外两种方式:通过PHPmail函数发送和通过PHPmail函数SMTP发送。主要修改了`init`初始化方法,添加了`SMTP_TYPE`配置项,并在`send`方法中根据`SMTP_TYPE`值选择相应的发送方式。通过这些修改,用户可以根据需求选择不同的邮件发送方式。
摘要由CSDN通过智能技术生成

2e85e4b0a60c2cb293f023a54beed52e.gif

原版CanPHP内置的Email类仅仅支持 "通过SMTP SOCKET 连接 SMTP 服务器发送", 可能某些用户还有其他两种发送方式的需求 “通过PHP mail 函数发送” 和 “通过PHP mail 函数SMTP发送”, 那么就需要对着2种方式进行扩展。

其实很简单,修改文件CanPHP/ext/Email.class.php过程如下。

1、在init初始化中补充一个

self::$config['SMTP_TYPE'] = isset($config['SMTP_TYPE']) ? $config['SMTP_TYPE'] : 'smtp'; //发送方式

记得外面调用此类初始化init的时候,把发送方式传进去,有这3个值 "smtp" 、"mail"、 "psmtp"。

2、 在send方法里,找到 $mail->IsSMTP(); 然后这段需要针对3种发送方式进行mail的初始化,告知mail类以何种方式发送。

if (self::$config['SMTP_TYPE'] == 'mail') {

$mail->IsMail(); // 使用PHP MAIL方式发送

} elseif (self::$config['SMTP_TYPE'] == 'psmtp') {

$mail->IsSendmail(); // 使用SMTP方式发送

} else {

$mail->IsSMTP(); // 使用SMTP方式发送

}

就这么简单就改完毕。 现在我把这个类文件全部源码分享如下。

//邮件发送类,基于PHPMailer类

class Email

{

static public $config;//存储配置的静态变量

//设定邮件参数

static public function init($config = array())

{

self::$config['SMTP_HOST']=isset($config['SMTP_HOST'])?$config['SMTP_HOST']:'smtp.qq.com';//smtp服务器地址

self::$config['SMTP_PORT']=isset($config['SMTP_PORT'])?$config['SMTP_PORT']:25;//smtp服务器端口

self::$config['SMTP_SSL']=isset($config['SMTP_SSL'])?$config['SMTP_SSL']:false;//是否启用SSL安全连接,gmail需要启用sll安全连接

self::$config['SMTP_USERNAME']=isset($config['SMTP_USERNAME'])?$config['SMTP_USERNAME']:'404352772@qq.com';//smtp服务器帐号,如:你的qq邮箱

self::$config['SMTP_PASSWORD']=isset($config['SMTP_PASSWORD'])?$config['SMTP_PASSWORD']:'123456';//smtp服务器帐号密码,如你的qq邮箱密码

self::$config['SMTP_AUTH']=isset($config['SMTP_AUTH'])?$config['SMTP_AUTH']:true;//启用SMTP验证功能,一般需要开启

self::$config['SMTP_CHARSET']=isset($config['SMTP_CHARSET'])?$config['SMTP_CHARSET']:'utf-8';//发送的邮件内容编码

self::$config['SMTP_FROM_TO']=isset($config['SMTP_FROM_TO'])?$config['SMTP_FROM_TO']:'404352772@qq.com';//发件人邮件地址

self::$config['SMTP_FROM_NAME']=isset($config['SMTP_FROM_NAME'])?$config['SMTP_FROM_NAME']:'CanPHP官方';//发件人姓名

self::$config['SMTP_DEBUG']=isset($config['SMTP_DEBUG'])?$config['SMTP_DEBUG']:false;//是否显示调试信息

self::$config['SMTP_TYPE'] = isset($config['SMTP_TYPE']) ? $config['SMTP_TYPE'] : 'smtp'; //发送方式

}

//发送邮件

static public function send($mail_to,$mail_subject,$mail_body,$mail_attach=NULL)

{

@error_reporting(E_ERROR | E_WARNING | E_PARSE);//屏蔽出错信息

require_once(dirname(__FILE__).'/phpmailer/class.phpmailer.php');

$mail = new PHPMailer();

//没有调用配置方法,则调用一次config方法

if(!isset(self::$config)||empty(self::$config))

{

self::config();

}

if (self::$config['SMTP_TYPE'] == 'mail') {

$mail->IsMail(); // 使用PHP MAIL方式发送

} elseif (self::$config['SMTP_TYPE'] == 'psmtp') {

$mail->IsSendmail(); // 使用SMTP方式发送

} else {

$mail->IsSMTP(); // 使用SMTP方式发送

}

$mail->Host = self::$config['SMTP_HOST']; //smtp服务器地址

$mail->Port = self::$config['SMTP_PORT']; //smtp服务器端口

$mail->Username = self::$config['SMTP_USERNAME']; //smtp服务器帐号,

$mail->Password = self::$config['SMTP_PASSWORD']; // smtp服务器帐号密码

$mail->SMTPAuth = self::$config['SMTP_AUTH'];//启用SMTP验证功能,一般需要开启

$mail->CharSet = self::$config['SMTP_CHARSET'];//发送的邮件内容编码

$mail->SetFrom(self::$config['SMTP_FROM_TO'], self::$config['SMTP_FROM_NAME']);// 发件人的邮箱和姓名

$mail->AddReplyTo(self::$config['SMTP_FROM_TO'],self::$config['SMTP_FROM_NAME']);// 回复时的邮箱和姓名,一般跟发件人一样

//是否启用SSL安全连接

if(self::$config['SMTP_SSL'])

{

$mail->SMTPSecure = "ssl"; //gmail需要启用sll安全连接

}

//开启调试信息

if(self::$config['SMTP_DEBUG'])

{

$mail->SMTPDebug = 1;

}

$mail->Subject = $mail_subject;//邮件标题

$mail->MsgHTML($mail_body);//邮件内容,支持html代码

//发送邮件

if(is_array($mail_to))

{

//同时发送给多个人

foreach($mail_to as $key=>$value)

{

$mail->AddAddress($value,""); // 收件人邮箱和姓名

}

}

else

{//只发送给一个人

$mail->AddAddress($mail_to,""); // 收件人邮箱和姓名

}

//发送多个附件

if(is_array($mail_attach))

{

foreach($mail_attach as $value)

{

if(file_exists($value))//附件必须存在,才会发送

{

$mail->AddAttachment($value); // attachment

}

}

}

//发送一个附件

if(!empty($mail_attach)&&is_string($mail_attach))

{

if(file_exists($mail_attach))//附件必须存在,才会发送

{

$mail->AddAttachment($mail_attach); //发送附件

}

}

if(!$mail->Send())

{

if(self::$config['SMTP_DEBUG'])

{

echo "Mailer Error: " . $mail->ErrorInfo;

}

return false;

}

else

{

return true;

}

}

}

?>

Spring Boot是一个流行的Java框架,它简化了构建独立的、可运行的Spring应用程序的过程。Spring Boot Email是Spring Boot生态系统的一个扩展模块,它提供了集成的电子邮件服务,使得在Spring Boot应用发送电子邮件变得更加容易。 主要特点包括: 1. **自动配置**:Spring Boot Email通过自动配置功能,你可以通过简单的配置就能启用邮件发送功能,无需手动配置邮件服务器。 2. **支持多个邮件服务**:它支持使用JavaMail API连接到各种邮件服务器,如SMTP、IMAP等。 3. **模板引擎**:可以使用Thymeleaf、Velocity等模板引擎创建动态邮件内容。 4. **集成邮件验证**:有时用于用户激活或密码重置的邮件确认过程。 使用Spring Boot Email的步骤大致如下: - 添加依赖:在你的`pom.xml`文件添加Spring Boot Email的Maven或Gradle依赖。 ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- Gradle --> implementation 'org.springframework.boot:spring-boot-starter-mail' ``` - 配置邮箱属性:在`application.properties`或`application.yml`设置邮件服务器的相关信息,如主机名、端口、用户名和密码等。 ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=myusername spring.mail.password=mypassword spring.mail.protocol=smtp ``` - 发送邮件:通过`JavaMailSender`接口或`MessageSource`发送自定义邮件。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String body) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(body); mailSender.send(message); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值