折腾了一个小时,终于将在线发送短信给折腾成功了,哇,网友能在线给webmaster发短信,真的好高深啊!其实未必,下面我就介绍相关的一些解决方法。
首先是如何通过php连接pop3发送邮件。在旧版本的phpmailer中,要连接Gmail可不是那么一件容易的事情,需要修改核心代码,但是在新版的phpmailer中,这个问题就被完美的解决了,首先请下载phpmailer,然后利用cosbeta写的这个发送邮件的函数:
include("lib/phpmailer/class.phpmailer.php");
function sendMail($to,$reply,$subject,$body,$mydisname,$att){
define(MYGMAIL,"mail@storyday.com"); //修改成你的邮箱地址
define(MYGMAILPASS,"你的gmail密码"); //
//下面不用修改了
$subject = $title = "=?UTF-8?B?".base64_encode($subject)."?=";
// utf8 for chinese
$mail = new PHPMailer();
$mail->CharSet ="utf8";
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = MYGMAIL; // GMAIL username
$mail->Password = MYGMAILPASS; // GMAIL password
if( is_array($reply) ){
$i = 0;
while( $reply[$i] !='' ){
$mail->AddReplyTo($reply[$i], " ");
$i ++;
}
}else{
$mail->AddReplyTo($reply , " ");
}
$mail->From = MYGMAIL;
$mail->FromName = SITE_NAME;
$mail->Subject = $subject;
$mail->AltBody = $body; // optional, comment out and test
$mail->WordWrap = 100; // set word wrap
$mail->MsgHTML($body);
if( is_array($to) ){
$i = 0;
while( $to[$i] !='' ){
$mail->AddAddress($to[$i], " ");
$i ++;
}
}else{
$mail->AddAddress($to, " ");
}
if( $att != '' ){
if( is_array($att) ){
$i = 0;
while( $att[$i] !='' ){
$mail->AddAttachment( $att[$i] );
$i ++;
}
}else{
$mail->AddAttachment( $att );
}
}
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
Return "Mailer Error: " . $mail->ErrorInfo;
}
else {
Return true;
}
}
函数用法:sendMail($to,$reply,$subject,$body,$mydisname,$att)
- $to 邮件接收者,若发送给多人,$to为接收者的邮件地址数组;
- $reply 邮件将被回复的地址;
- $subject邮件标题;
- $body邮件内容;
- $mydisname邮件发送者显示姓名;
- $att附件地址,若发送多个附件,请用数组
那现在就看看怎么能通过web发短信呢?
秘密在于:使用广东移动全球通(这里不是广告哈),可以通过移动邮箱发送和接收短信,所以,所谓的通过web发送短信,其实就是通过web向自己的全球通邮箱发送邮件罢了!
标签:mail, PHP
春节时也这么玩过,那时候直接借用了 WordPress 里带的 phpmailer