首先当然要去下载phpmailer了。
<?php require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录 class email { public static $email; public function __construct($host,$Username,$Password,$Port=25,$charSet='UTF-8'){ self::$email = new PHPMailer(); //建立邮件发送类 self::$email->charSet=$charSet;//设置编码 self::$email->IsSMTP(); //开启smtp服务 self::$email->SMTPAuth = true; // 启用SMTP验证功能 self::$email->Host = $host; // 您的企业邮局域名 self::$email->Username=$Username; //账户 self::$email->Password=$Password; //密码 self::$email->Port=$Port;//端口,默认是25 } public function sendEmail($From,$FromName,$AddAddress,$Subject,$Body,$name='',$AltBody='',$IsHTML=true){ self::$email->IsHTML($IsHTML);//是否使用HTML格式 self::$email->From=$From; //邮件发送者email地址 ,必须和上面的一致 self::$email->FromName=$FromName; //邮件发送人的名字 self::$email->AddAddress($AddAddress,$name);//邮件接收人的邮箱地址 self::$email->Subject=$Subject;//主题 self::$email->Body=$Body;//内容 self::$email->AltBody=$AltBody;//附加信息,可以省略 if(!self::$email->Send()) { echo "邮件发送失败. <p>"; echo "错误原因: " . self::$email->ErrorInfo; exit; } echo "邮件发送成功"; } public function sendEmailAndAttachment($From,$FromName,$AddAddress,$Subject,$Body,$Attachment,$name='',$AltBody='',$IsHTML=true){ self::$email->IsHTML($IsHTML);//是否使用HTML格式 self::$email->From=$From; //邮件发送者email地址 ,必须和上面的一致 self::$email->FromName=$FromName; //邮件发送人的名字 self::$email->AddAddress($AddAddress,$name);//邮件接收人的邮箱地址 self::$email->Subject=$Subject;//主题 self::$email->Body=$Body;//内容 self::$email->AltBody=$AltBody;//附加信息,可以省略 self::$email->AddAttachment($Attachment); // 添加附件,这里填写附件地址 if(!self::$email->Send()) { echo "邮件发送失败. <p>"; echo "错误原因: " . self::$email->ErrorInfo; exit; } echo "邮件发送成功"; } } $email =new email("smtp.qq.com",'371522155@qq.com','hh94577312'); $email->sendEmailAndAttachment('371522155@qq.com','huanghui','2963584466@QQ.com','wwwww','vvvv','1.txt'); ?>