PHPMailer发送邮件的代码如下。
<?php
/**
* PHPMailer邮件发送
* Edit www.
*/
function smtp_main_send( $to, $subject, $message, $from, $fromName )
{
$mail = new PHPMailer();
$mail->CharSet = "UTF-8"; // 设置编码
$mail->IsSMTP(); // 设置使用SMTP服务发送
$mail->Host = "smtp.mail.com";
$mail->Username = "user";
$mail->Password = "pass";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = $fromName;
if ( is_array( $to ) ) {
foreach ( $to as $address ) {
$mail->AddAddress( $address );
}
} else {
$mail->AddAddress( $to );
}
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
$mail->IsHTML( true );
return $mail->Send();
}
?>
用以上的代码发送英文邮件没有问题,但发送中文邮件时标题会有乱码。
解决方法:
需要对 class.phpmailer.php 做一些修改:
修改1,1137 行:
function EncodeHeader ($str, $position = 'text') {
将函数增加一个参数:
if ( $pl ) return "=?" . $this->CharSet . "?B?" . base64_encode($str) . "?=";
修改2,796 行:
$result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
将调用改为:
即可解决中文标题乱码的问题。
附,PHPMailer邮件发送类V5.1下载地址。
您可能感兴趣的文章:
PHPMailer发送邮件的实例分享
phpmailer发送gmail邮件的例子
phpmailer发送网易126邮箱的例子
phpmailer发送yahoo邮件的例子
phpmailer类实现邮件群发的实例代码
PHPMailer发送邮件代码实例(ubuntu系统)
PHPMailer发送带附件邮件的例子
PHPMailer收发邮件标题、发件人、内容乱码问题的终极解决方法
PHPmailer发送邮件及乱码问题的解决
PHPMailer发送邮件中文附件名乱码的解决办法
PHPMailer邮件类发送邮件举例(163邮箱)
phpmailer 发送邮件中文乱码问题的解决方法总结
phpmailer发送邮件及实现密码找回功能的代码
PHPmailer邮件群发的入门例子
PHPmailer 邮件群发的范例参考
phpmailer发邮件中文乱码问题如何解决
phpmailer 类发送邮件乱码解决方法
PHPMailer批量发送邮件的实例代码
有关phpmailer的用法
php使用phpMailer发送邮件的例子
phpmailer实现的简单openvpn用户认证的代码
PHPMailer 中文使用说明
phpmailer发送邮件的例子
1、下载PHPMailer文件包,PHPMailer邮件发送类V5.1下载地址。
2、确认服务器支持socket,查看是否支持sockets
注意: socket 是属于PHP扩展部分,编译时必须给定一个用于./configure --enable-sockets 的配置选项。
3、把文件解压到你的web服务器目录下,调用类即可。
说明:首先包含 class.phpmailer.php,然后创建对象,设置参数,调用成员函数。
代码如下:
<?php
/**
* HPPMailer邮件类发送邮件
* Edit www.
*/
require("phpmailer/class.phpmailer.php");
function smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name) {
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "200.162.244.66"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourmail"; // SMTP username 注意:普通邮件认证不需要加 @域名
$mail->Password = "mailPassword"; // SMTP password
$mail->From = "yourmail@"; // 发件人邮箱
$mail->FromName = "管理员"; // 发件人
$mail->CharSet = "GB2312"; // 这里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,"username"); // 收件人邮箱和姓名
$mail->AddReplyTo("yourmail@","");
//$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true); // send as HTML
// 邮件主题
$mail->Subject = $subject;
// 邮件内容
$mail->Body = '
<html><head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"></head>
<body>
欢迎来到<a href="http://www.">http://www.</a> <br /><br />
感谢您注册为本站会员!<br /><br />
</body>
</html>
';
$mail->AltBody ="text/html";
if(!$mail->Send())
{
echo "邮件发送有误 <p>";
echo "邮件错误信息: " . $mail->ErrorInfo;
exit;
}
else {
echo "$user_name 邮件发送成功!<br />";
}
}
// 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)
smtp_mail('yourmail@', '欢迎来到!', 'NULL', '', 'username');
?>
注意:
1、邮件字符集设置, $mail->CharSet = "GB2312"; // 指定字符集!
这里只指定为GB2312因为这样Outlook能正常显示邮件主题,我尝试过设为utf-8,但在Outlook下显示乱码。
2、发送html格式的邮件,记得指定为<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
3、群发邮件,记得修改包含文件函数,如:
require("phpmailer/class.phpmailer.php");
改为
require_once("phpmailer/class.phpmailer.php");
否则,会提示产生类的重定义。
您可能感兴趣的文章:
PHPMailer发送邮件的实例分享
phpmailer发送gmail邮件的例子
phpmailer发送网易126邮箱的例子
phpmailer发送yahoo邮件的例子
phpmailer类实现邮件群发的实例代码
PHPMailer发送邮件代码实例(ubuntu系统)
PHPMailer发送带附件邮件的例子
PHPMailer收发邮件标题、发件人、内容乱码问题的终极解决方法
PHPmailer发送邮件及乱码问题的解决
PHPMailer发送邮件中文附件名乱码的解决办法
PHPMailer邮件标题中文乱码的解决方法
phpmailer 发送邮件中文乱码问题的解决方法总结
phpmailer发送邮件及实现密码找回功能的代码
PHPmailer邮件群发的入门例子
PHPmailer 邮件群发的范例参考
phpmailer发邮件中文乱码问题如何解决
phpmailer 类发送邮件乱码解决方法
PHPMailer批量发送邮件的实例代码
有关phpmailer的用法
php使用phpMailer发送邮件的例子
phpmailer实现的简单openvpn用户认证的代码
PHPMailer 中文使用说明
phpmailer发送邮件的例子
PHP验证码类的代码。
<?php
/**
* php5验证码类
* edit www.
*/
class CCheckCodeFile
{
//验证码位数
private $mCheckCodeNum = 4;
//产生的验证码
private $mCheckCode = '';
//验证码的图片
private $mCheckImage = '';
//干扰像素
private $mDisturbColor = '';
//验证码的图片宽度
private $mCheckImageWidth = '80';
//验证码的图片宽度
private $mCheckImageHeight = '20';
/**
*
* @brief 输出头
*
*/
private function OutFileHeader()
{
header ("Content-type: image/png");
}
/**
*
* @brief 产生验证码
*
*/
private function CreateCheckCode()
{
$this->mCheckCode = strtoupper()(substr(md5(rand()),0,$this->mCheckCodeNum));
return $this->mCheckCode;
}
/**
*
* @brief 产生验证码图片
*
*/
private function CreateImage()
{
$this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);
imagecolorallocate ($this->mCheckImage, 200, 200, 200);
return $this->mCheckImage;
}
/**
*
* @brief 设置图片的干扰像素
*
*/
private function SetDisturbColor()
{
for ($i=0;$i<=128;$i++)
{
$this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
}
}
/**
*
* @brief 设置验证码图片的大小
*
* @param $width 宽
*
* @param $height 高
*
*/
public function SetCheckImageWH($width,$height)
{
if($width==''||$height=='')return false;
$this->mCheckImageWidth = $width;
$this->mCheckImageHeight = $height;
return true;
}
/**
*
* @brief 在验证码图片上逐个画上验证码
*
*/
private function WriteCheckCodeToImage()
{
for ($i=0;$i<=$this->mCheckCodeNum;$i++)
{
$bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
$x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
$y = rand(0,$this->mCheckImageHeight-15);
imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
}
}
/**
*
* @brief 输出验证码图片
*
*/
public function OutCheckImage()
{
$this ->OutFileHeader();
$this ->CreateCheckCode();
$this ->CreateImage();
$this ->SetDisturbColor();
$this ->WriteCheckCodeToImage();
imagepng($this->mCheckImage);
imagedestroy($this->mCheckImage);
}
}
$c_check_code_image = new CCheckCodeFile();
//$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸
$c_check_code_image ->OutCheckImage();
?>
您可能感兴趣的文章:
php验证码简单函数代码(附效果图)
分享一个php 验证码类及调用示例
php验证码的三个实例代码分享
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php验证码函数使用的例子
php验证码(GD库生成验证码)的例子
php点击验证码实时刷新的实现代码
php图片验证码的例子
php彩色验证码的简单例子
php验证码刷新与局部刷新的实现方法
php GD库生成验证码的实例
php生成验证码的例子
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码