使用PHPMailer实现表单提交爬取多个网页局部HTML结构并发送邮件

PHPMailer类文件可到GitHub下载,地址:https://github.com/Synchro/PHPMailer;只用到STMP功能,只需解压class.phpmailer.php和class.smtp.php两个类文件到你所需要的目录即可,GITHUB里面示例请戳:https://github.com/Synchro/PHPMailer#a-simple-example。网上发送邮件类文章很多但是爬取一个网页发送带HTML邮件的示例较少。下面我简单介绍一下我在使用PHPMailer时的一些心得,不喜勿喷。

    头部引入自动加载入口文件

include_once "PHPMailer/PHPMailerAutoload.php"; 

自定义一个发送邮件的函数:

$host 使用的邮件服务器(smtp.163.com,smtp.qq.com,smtp.sina.com等);$fromMail发件人邮箱地址;$fromPass发件人邮箱密码;$fromName发件人名称;$toMail收件人地址;$toName收件人名称;$addTo邮件抄送人地址;$title邮件标题;$mailContents邮件内容(含HTML内容);$content邮件正文内容;

function sentToMail($host,$fromMail,$fromPass,$fromName,$toMail,$toName,$addTo,$title,$mailContents,$content){
	//实例化类 
	$mail = new PHPMailer; 
	//设置smtp参数 
	$mail->IsSMTP();           //设置邮件使用SMTP方式寄信
	$mail->SMTPAuth=true;      //设定SMTP需要验证
	$mail->SMTPKeepAlive=true; 
	$mail->Host=$host;        //设置stmp服务器
	$mail->Port=25;         //设置stmp端口  不同邮件服务器端口不一样,具体可以百度一下
	//填写你的email账号和密码 
	$mail->Username=$fromMail; 
$mail->CharSet = "UTF-8"; //设置邮件编码
$mail->Password=$fromPass;//设置发送方$mail->setFrom($fromMail,$fromName); $mail->addAddress($toMail,$toName); //设置邮件接收方的邮箱和姓名 $mail->addCC($addTo); //添加抄送人$mail->Subject =$title; //设置邮件主题$mail->MsgHTML($mailContents.$content); //设置邮件内容//$mail->Body = $content;邮件正文内容//$mail->altbody="this is text only alternative body."; 邮件正文不支持HTML的备用显示return $mail->send(); //发送}

以上是一个邮件发送过程最基本的参数,如果抄送多个联系人可以在写一行$mail->addCC($addTo2) //添加抄送人2;$mail->addBCC("add")添加密送人,不会显示密送者信息;$mail->addAttchment('1.jpg')添加附件;上述$mailContents是包含HTML的邮件内容,$content是邮件文本正文,二者是不一样的。

以下是我爬取多个相似网页的局部内容(包含html)并发送邮件示例:

	if($_SERVER["REQUEST_METHOD"] == "POST")
	{        //接受表单信息参数
		$ids 		= $_POST["ID"];
		$name 		= trim($_POST["name"]);
		$sent 		= trim($_POST["sent"]);
		$email 		= trim($_POST["email"]);
		$content        = htmlspecialchars(trim($_POST["content"]));
		$create_time    = date("Y-m-d H:i:s");

		if(!is_array($ids)){
		    $ids = array($ids);
		}
		foreach ($ids as $IDs) {
		    //抓取详细内容
		    $target_url = "http://xxxxxxx/li_display.php?id=".$IDs;  //被爬取的页面地址
		    $ch = curl_init();
		     
		    curl_setopt($ch, CURLOPT_URL,$target_url);
		    curl_setopt($ch, CURLOPT_FAILONERROR, true);
		    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		    $lines_string = curl_exec($ch);

		    $dom = new DOMDocument();
		    @$dom->loadHTML($lines_string);
		    $xPath = new DOMXPath($dom);
		    $elements = $xPath->query('//*[@class="w-c"]');  //匹配爬取页面局部html结构
		    $contentx.=$dom->saveXml($elements->item(0));    //用字符串将打印多个页面局部信息拼接起来
		}
		echo $contentx;
		//exit;

		if($_POST["sent"]=='ok'){
		    //发邮件sentToMail($host,$fromMail,$fromPass,$fromName,$toMail,$toName,$addTo,$title,$mailContents)
		    $mail = sentToMail('smtp.163.com','Lisi@163.com','******','Lisi','zhangsan@ibw.cn','Zhangsan','Wangwu@qq.com','test2',$contentx,$content);
		    if($mail){
			echo "<script>alert('sent successfully!');history.back(-1);</script>";
		    }
		}
	}

以上是我使用PHPMailer实现发送带HTML邮件的一些心得,经本人测试无误,如有疑问,欢迎大家前来交流学习。

转载请注明出处,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值