存档

文章标签 ‘函数’

ubuntu或者linux下安装sendmail让php支持mail()函数发送邮件

2017年3月4日 没有评论

ubuntu或者linux下安装sendmail让php支持mail()函数发送邮件,详细方法如下:
sudo apt-get install sendmail

sudo hostname
获取本机hostname

sudo vim /etc/hosts
在底部加入:127.0.0.1 刚才获取的hostname

使用测试代码发送:
<?php
$headers = ‘From: support@sender.com’ . “\r\n” .’Reply-To: support@sender.com’ . “\r\n” .’X-Mailer: PHP/’ . phpversion();
$send = mail(‘jieshou@163.com’, ‘My Subject’, ‘The test mail’,$headers);
if($send){echo ‘true’;}
else{echo ‘false’;}
?>

发送的时候,可能会进入垃圾箱,如果hostname和From: support@sender.com中的sender.com不一致,会出现由”***”代发的字样。
可以修改hostname

sudo vim /etc/hostname
再修改/etc/hosts中刚才加入的一行

重启服务器

就可以不会再出现由”***”代发的字样。

很全很牛逼的java日期函数

2015年11月27日 没有评论
/**
  * 获取现在时间
  *
  * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  */
public static Date getNowDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
}
/**
  * 获取现在时间
  *
  * @return返回短时间格式 yyyy-MM-dd
  */
public static Date getNowDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
}
/**
  * 获取现在时间
  *
  * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  */
public static String getStringDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  return dateString;
}
/**
  * 获取现在时间
  *
  * @return 返回短时间字符串格式yyyy-MM-dd
  */
public static String getStringDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  return dateString;
}
/**
  * 获取时间 小时:分;秒 HH:mm:ss
  *
  * @return
  */
public static String getTimeShort() {
  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  Date currentTime = new Date();
  String dateString = formatter.format(currentTime);
  return dateString;
}
/**
  * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  *
  * @param strDate
  * @return
  */
public static Date strToDateLong(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
}
/**  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  *   * @param dateDate  * @return  */
public static String dateToStrLong(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(dateDate);
  return dateString;
}
/**
  * 将短时间格式时间转换为字符串 yyyy-MM-dd
  *
  * @param dateDate
  * @param k
  * @return
  */
public static String dateToStr(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(dateDate);
  return dateString;
}
/**
  * 将短时间格式字符串转换为时间 yyyy-MM-dd
  *
  * @param strDate
  * @return
  */
public static Date strToDate(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
}
/**
  * 得到现在时间
  *
  * @return
  */
public static Date getNow() {
  Date currentTime = new Date();
  return currentTime;
}
/**
  * 提取一个月中的最后一天
  *
  * @param day
  * @return
  */
public static Date getLastDate(long day) {
  Date date = new Date();
  long date_3_hm = date.getTime() - 3600000 * 34 * day;
  Date date_3_hm_date = new Date(date_3_hm);
  return date_3_hm_date;
}
/**
  * 得到现在时间
  *
  * @return 字符串 yyyyMMdd HHmmss
  */
public static String getStringToday() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  String dateString = formatter.format(currentTime);
  return dateString;
}
/**
  * 得到现在小时
  */
public static String getHour() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  String hour;
  hour = dateString.substring(11, 13);
  return hour;
}
/**
  * 得到现在分钟
  *
  * @return
  */
public static String getTime() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  String min;
  min = dateString.substring(14, 16);
  return min;
}
/**
  * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  *
  * @param sformat
  *            yyyyMMddhhmmss
  * @return
  */
public static String getUserDate(String sformat) {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  String dateString = formatter.format(currentTime);
  return dateString;
}
/**
  * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  */
public static String getTwoHour(String st1, String st2) {
  String[] kk = null;
  String[] jj = null;
  kk = st1.split(":");
  jj = st2.split(":");
  if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
   return "0";
  else {
   double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
   double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
   if ((y - u) > 0)
    return y - u + "";
   else
    return "0";
  }
}
/**
  * 得到二个日期间的间隔天数
  */
public static String getTwoDay(String sj1, String sj2) {
  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  long day = 0;
  try {
   java.util.Date date = myFormatter.parse(sj1);
   java.util.Date mydate = myFormatter.parse(sj2);
   day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  } catch (Exception e) {
   return "";
  }
  return day + "";
}
/**
  * 时间前推或后推分钟,其中JJ表示分钟.
  */
public static String getPreTime(String sj1, String jj) {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String mydate1 = "";
  try {
   Date date1 = format.parse(sj1);
   long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
   date1.setTime(Time * 1000);
   mydate1 = format.format(date1);
  } catch (Exception e) {
  }
  return mydate1;
}
/**
  * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  */
public static String getNextDay(String nowdate, String delay) {
  try{
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  String mdate = "";
  Date d = strToDate(nowdate);
  long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  d.setTime(myTime * 1000);
  mdate = format.format(d);
  return mdate;
  }catch(Exception e){
   return "";
  }
}
/**
  * 判断是否润年
  *
  * @param ddate
  * @return
  */
public static boolean isLeapYear(String ddate) {
  /**
   * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
   * 3.能被4整除同时能被100整除则不是闰年
   */
  Date d = strToDate(ddate);
  GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  gc.setTime(d);
  int year = gc.get(Calendar.YEAR);
  if ((year % 400) == 0)
   return true;
  else if ((year % 4) == 0) {
   if ((year % 100) == 0)
    return false;
   else
    return true;
  } else
   return false;
}
/**
  * 返回美国时间格式 26 Apr 2006
  *
  * @param str
  * @return
  */
public static String getEDate(String str) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(str, pos);
  String j = strtodate.toString();
  String[] k = j.split(" ");
  return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
}
/**
  * 获取一个月的最后一天
  *
  * @param dat
  * @return
  */
public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  String str = dat.substring(0, 8);
  String month = dat.substring(5, 7);
  int mon = Integer.parseInt(month);
  if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
   str += "31";
  } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
   str += "30";
  } else {
   if (isLeapYear(dat)) {
    str += "29";
   } else {
    str += "28";
   }
  }
  return str;
}
/**
  * 判断二个时间是否在同一个周
  *
  * @param date1
  * @param date2
  * @return
  */
public static boolean isSameWeekDates(Date date1, Date date2) {
  Calendar cal1 = Calendar.getInstance();
  Calendar cal2 = Calendar.getInstance();
  cal1.setTime(date1);
  cal2.setTime(date2);
  int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  if (0 == subYear) {
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
   // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  }
  return false;
}
/**
  * 产生周序列,即得到当前时间所在的年度是第几周
  *
  * @return
  */
public static String getSeqWeek() {
  Calendar c = Calendar.getInstance(Locale.CHINA);
  String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  if (week.length() == 1)
   week = "0" + week;
  String year = Integer.toString(c.get(Calendar.YEAR));
  return year + week;
}
/**
  * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  *
  * @param sdate
  * @param num
  * @return
  */
public static String getWeek(String sdate, String num) {
  // 再转换为时间
  Date dd = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(dd);
  if (num.equals("1")) // 返回星期一所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  else if (num.equals("2")) // 返回星期二所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  else if (num.equals("3")) // 返回星期三所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  else if (num.equals("4")) // 返回星期四所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  else if (num.equals("5")) // 返回星期五所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  else if (num.equals("6")) // 返回星期六所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  else if (num.equals("0")) // 返回星期日所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
/**
  * 根据一个日期,返回是星期几的字符串
  *
  * @param sdate
  * @return
  */
public static String getWeek(String sdate) {
  // 再转换为时间
  Date date = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(date);
  // int hour=c.get(Calendar.DAY_OF_WEEK);
  // hour中存的就是星期几了,其范围 1~7
  // 1=星期日 7=星期六,其他类推
  return new SimpleDateFormat("EEEE").format(c.getTime());
}
public static String getWeekStr(String sdate){
  String str = "";
  str = VeDate.getWeek(sdate);
  if("1".equals(str)){
   str = "星期日";
  }else if("2".equals(str)){
   str = "星期一";
  }else if("3".equals(str)){
   str = "星期二";
  }else if("4".equals(str)){
   str = "星期三";
  }else if("5".equals(str)){
   str = "星期四";
  }else if("6".equals(str)){
   str = "星期五";
  }else if("7".equals(str)){
   str = "星期六";
  }
  return str;
}
/**
  * 两个时间之间的天数
  *
  * @param date1
  * @param date2
  * @return
  */
public static long getDays(String date1, String date2) {
  if (date1 == null || date1.equals(""))
   return 0;
  if (date2 == null || date2.equals(""))
   return 0;
  // 转换为标准时间
  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  java.util.Date date = null;
  java.util.Date mydate = null;
  try {
   date = myFormatter.parse(date1);
   mydate = myFormatter.parse(date2);
  } catch (Exception e) {
  }
  long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  return day;
}
/**
  * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  * 此函数返回该日历第一行星期日所在的日期
  *
  * @param sdate
  * @return
  */
public static String getNowMonth(String sdate) {
  // 取该时间所在月的一号
  sdate = sdate.substring(0, 8) + "01";
  // 得到这个月的1号是星期几
  Date date = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(date);
  int u = c.get(Calendar.DAY_OF_WEEK);
  String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  return newday;
}
/**
  * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  *
  * @param k
  *            表示是取几位随机数,可以自己定
  */
public static String getNo(int k) {
  return getUserDate("yyyyMMddhhmmss") + getRandom(k);
}
/**
  * 返回一个随机数
  *
  * @param i
  * @return
  */
public static String getRandom(int i) {
  Random jjj = new Random();
  // int suiJiShu = jjj.nextInt(9);
  if (i == 0)
   return "";
  String jj = "";
  for (int k = 0; k < i; k++) {
   jj = jj + jjj.nextInt(9);
  }
  return jj;
}
/***************************************************************************
  * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  **************************************************************************/
public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  String s_nd = dateString.substring(0, 4); // 年份
  String s_yf = dateString.substring(5, 7); // 月份
  String s_rq = dateString.substring(8, 10); // 日期
  String sreturn = "";
  roc.util.MyChar mc = new roc.util.MyChar();
  if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
   if (nd.equals("1")) {
    sreturn = s_nd;
    // 处理间隔符
    if (format.equals("1"))
     sreturn = sreturn + "年";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理月份
   if (yf.equals("1")) {
    sreturn = sreturn + s_yf;
    if (format.equals("1"))
     sreturn = sreturn + "月";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理日期
   if (rq.equals("1")) {
    sreturn = sreturn + s_rq;
    if (format.equals("1"))
     sreturn = sreturn + "日";
   }
  } else {
   // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
   sdate = roc.util.RocDate.getOKDate(sdate);
   s_nd = sdate.substring(0, 4); // 年份
   s_yf = sdate.substring(5, 7); // 月份
   s_rq = sdate.substring(8, 10); // 日期
   if (nd.equals("1")) {
    sreturn = s_nd;
    // 处理间隔符
    if (format.equals("1"))
     sreturn = sreturn + "年";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理月份
   if (yf.equals("1")) {
    sreturn = sreturn + s_yf;
    if (format.equals("1"))
     sreturn = sreturn + "月";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理日期
   if (rq.equals("1")) {
    sreturn = sreturn + s_rq;
    if (format.equals("1"))
     sreturn = sreturn + "日";
   }
  }
  return sreturn;
}
分类: android 标签: ,

还是比较牛逼的php增加水印类

2014年11月12日 没有评论

<?php
class water{
public $waterType=1;//水印类型:0为文字水印、1为图片水印
public $position=9;//-1随机,1上左,2上中,3上右,4中左,5中中,6中右,7下左,8下中,9下右,默认9
public $transparent=100;//水印透明度
public $waterImg=”;//水印图片
public $waterStr=”;//水印文字
public $fontSize=12;//文字字体大小,默认为12
public $fontColor=array(0,0,0);//水印文字颜色(RGB),默认为黑色:array(0,0,0)
public $fontFile=”;//字体文件,建议使用绝对路径,默认可以为空
public $srcImg=”;//需要添加水印的图片
private $im=”;//图片句柄
private $water_im=”;//水印图片句柄
private $srcImg_info=”;//源图片信息
private $waterImg_info=”;//水印图片信息
private $str_w=”;//水印文字宽度
private $str_h=”;//水印文字高度
private $x=”;//水印X坐标
private $y=”;//水印y坐标
function __construct(){}
private function imginfo(){//获取需要添加水印的图片的信息,并载入图片。
$this->srcImg_info=getimagesize($this->srcImg);
switch($this->srcImg_info[2]){
case 3:$this->im=imagecreatefrompng($this->srcImg);break;
case 2:$this->im=imagecreatefromjpeg($this->srcImg);break;
case 1:$this->im=imagecreatefromgif($this->srcImg);break;
default:die(‘源图片格式不对,只支持png/jpeg/gif/jpg.’);break;
}
}
private function waterimginfo(){//获取水印图片的信息,并载入图片。
$this->waterImg_info=getimagesize($this->waterImg);
switch($this->waterImg_info[2]){
case 3:$this->water_im=imagecreatefrompng($this->waterImg);break;
case 2:$this->water_im=imagecreatefromjpeg($this->waterImg);break;
case 1:$this->water_im=imagecreatefromgif($this->waterImg);break;
default:die(‘水印图片格式不对,只支持png/jpeg/gif/jpg.’);break;
}
}
private function waterpos(){//-1随机,1上左,2上中,3上右,4中左,5中中,6中右,7下左,8下中,9下右
switch ($this->position){
case -1:$this->x=rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);$this->y=rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);break;
case 1:$this->x=0;$this->y=0;break;
case 2:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=0;break;
case 3:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=0;break;
case 4:$this->x=0;$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 5:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 6:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 7:$this->x=0;$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
case 8:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
default:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
}
}
private function waterimg(){
if($this->srcImg_info[0]<=$this->waterImg_info[0] || $this->srcImg_info[1]<=$this->waterImg_info[1]){return false;}
$this->waterpos();
$cut=imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$this->transparent);
}
private function waterstr(){
$rect=imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
$w=abs($rect[2]-$rect[6]);
$h=abs($rect[3]-$rect[7]);
$fontHeight=$this->fontSize;
$this->water_im=imagecreatetruecolor($w,$h);
imagealphablending($this->water_im,false);
imagesavealpha($this->water_im,true);
$white_alpha=imagecolorallocatealpha($this->water_im,255,255,255,127);
imagefill($this->water_im,0,0,$white_alpha);
$color=imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
$this->waterImg_info=array(0=>$w,1=>$h);$this->waterimg();
}
function initImg($c_srcimg,$c_waterimg,$c_position,$c_transparent){//图片水印效果
$this->srcImg=file_exists($c_srcimg)?$c_srcimg:die(‘源文件不存在!’);
if($c_waterimg) $this->waterImg=$c_waterimg;
else die(‘请指定水印文件!’);
$this->waterType=1;
if($c_position) $this->position=$c_position;
if($c_transparent) $this->transparent=$c_transparent;
$this->outputWater();
}
function initText($c_srcimg,$c_fontText,$c_fontSize,$c_fontColor,$c_fontFamilyPath,$c_positioin,$c_transparent){//文字水印效果
$this->srcImg=file_exists($c_srcimg)?$c_srcimg:die(‘源文件不存在!’);
if($c_fontText) $this->waterStr=$c_fontText;
else die(‘请指定水印文字内容!’);
if($fc_ontSize) $this->fontSize=$fc_ontSize;
if($c_fontColor) $this->fontColor=$c_fontColor;
if($c_fontFamilyPath) $this->fontFile=$c_fontFamilyPath;
if($c_positioin) $this->position=$c_positioin;
if($c_transparent) $this->transparent=$c_transparent;
$this->waterType=0;$this->outputWater();
}
function outputWater(){
$this->imginfo();
if($this->waterType==0){$this->waterstr();}
else{$this->waterimginfo();$this->waterimg();}
switch($this->srcImg_info[2]){
case 3:imagepng($this->im,$this->srcImg);break;
case 2:imagejpeg($this->im,$this->srcImg);break;
case 1:imagegif($this->im,$this->srcImg);break;
default:die(‘添加水印失败!’);break;
}
imagedestroy($this->im);imagedestroy($this->water_im);
}
}

使用方法如下:
$water=new water();
//原图,水印文件,位置,水印图片透明度
$water->initImg(‘test.jpg’,’water2.png’,9,100);
//原图,文字,文字大小,文字颜色,字体位置(使用绝对路径,不可为空),文字水印位置,透明度
$water->initText(‘test.jpg’,’Hello,world!’,16,array(0,0,0),’1.ttf’,2,100);
?>

分类: php学习 标签: , ,

整理的比较实用的php发送email函数类

2014年11月12日 没有评论

<code>

<?php
class SMTP{
var $SMTP_PORT=25;var $CRLF=”\r\n”;var $smtp_conn;
var $error;var $helo_rply;var $do_debug;
function SMTP(){$this->smtp_conn=0;$this->error=null;$this->helo_rply=null;$this->do_debug=0;}
function Connect($host,$port=0,$tval=30){
$this->error=null;
if($this->connected()){$this->error=array(“error”=>”Already connected to a server”);return false;}
if(empty($port)){$port=$this->SMTP_PORT;}
$this->smtp_conn=fsockopen($host,$port,$errno,$errstr,$tval);
if(empty($this->smtp_conn)){
$this->error=array(“error”=>”Failed to connect to server”,”errno”=>$errno,”errstr”=>$errstr);
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: $errstr ($errno)”.$this->CRLF;}
return false;
}
if(substr(PHP_OS, 0, 3)!=”WIN”) socket_set_timeout($this->smtp_conn, 1, 0);$announce=$this->get_lines();
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$announce;}
return true;
}
function Authenticate($username,$password){
fputs($this->smtp_conn, “AUTH LOGIN”.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($code!=334){
$this->error=array(“error”=>”AUTH not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
fputs($this->smtp_conn, base64_encode($username).$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($code!=334){
$this->error=array(“error”=>”Username not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
fputs($this->smtp_conn, base64_encode($password).$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($code!=235){
$this->error=array(“error”=>”Password not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Connected(){
if(!empty($this->smtp_conn)){
$sock_status=socket_get_status($this->smtp_conn);
if($sock_status[“eof”]){
if($this->do_debug>=1){echo “SMTP -> NOTICE:”.$this->CRLF.”EOF caught while checking if connected”;}
$this->Close();return false;
}return true;
}return false;
}
function Close(){
$this->error=null;$this->helo_rply=null;
if(!empty($this->smtp_conn)){fclose($this->smtp_conn);$this->smtp_conn=0;}
}
function Data($msg_data){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Data() without being connected”);return false;}
fputs($this->smtp_conn, “DATA”.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=354){
$this->error=array(“error”=>”DATA command not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
$msg_data=str_replace(“\r\n”, “\n”,$msg_data);$msg_data=str_replace(“\r”, “\n”,$msg_data);$lines=explode(“\n”,$msg_data);$field=substr($lines[0], 0, strpos($lines[0], “:”));$in_headers=false;
if(!empty($field) && !strstr($field, ” “)){$in_headers=true;}
$max_line_length=998;
while(list(,$line)=@each($lines)){
$lines_out=null;
if($line==”” && $in_headers){$in_headers=false;}
while(strlen($line)>$max_line_length){
$pos=strrpos(substr($line, 0,$max_line_length), ” “);$lines_out[]=substr($line, 0,$pos);$line=substr($line,$pos + 1);
if($in_headers){$line=”\t”.$line;}
}
$lines_out[]=$line;
while(list(,$line_out)=@each($lines_out)){
if(strlen($line_out)>0){if(substr($line_out, 0, 1)==”.”){$line_out=”.”.$line_out;}}
fputs($this->smtp_conn,$line_out.$this->CRLF);
}
}
fputs($this->smtp_conn,$this->CRLF.”.”.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”DATA not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Expand($name){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Expand() without being connected”);return false;}
fputs($this->smtp_conn, “EXPN “.$name.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”EXPN not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
$entries=explode($this->CRLF,$rply);
while(list(,$l)=@each($entries)){$list[]=substr($l, 4);}
return $rval;
}
function Hello($host=””){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Hello() without being connected”);return false;}
if(empty($host)){$host=”localhost”;}
fputs($this->smtp_conn, “HELO “.$host.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER: “.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”HELO not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
$this->helo_rply=$rply;return true;
}
function Help($keyword=””){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Help() without being connected”);return false;}
$extra=””;
if(!empty($keyword)){$extra=” “.$keyword;}
fputs($this->smtp_conn, “HELP”.$extra.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=211 && $code!=214){
$this->error=array(“error”=>”HELP not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return $rply;
}
function Mail($from){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Mail() without being connected”);return false;}
fputs($this->smtp_conn, “MAIL FROM:”.$from.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”MAIL not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Noop(){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Noop() without being connected”);return false;}
fputs($this->smtp_conn, “NOOP”.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”NOOP not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Quit($close_on_error=true){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Quit() without being connected”);return false;}
fputs($this->smtp_conn, “quit”.$this->CRLF);$byemsg=$this->get_lines();
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$byemsg;}
$rval=true;$e=null;$code=substr($byemsg, 0, 3);
if($code!=221){
$e=array(“error”=>”SMTP server rejected quit command”,”smtp_code”=>$code,”smtp_rply”=>substr($byemsg, 4));$rval=false;
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$e[“error”].”: “.$byemsg.$this->CRLF;}
}
if(empty($e) || $close_on_error){$this->Close();}
return $rval;
}
function Recipient($to){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Recipient() without being connected”);return false;}
fputs($this->smtp_conn, “RCPT TO:”.$to.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250 && $code!=251){
$this->error=array(“error”=>”RCPT not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Reset(){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Reset() without being connected”);return false;}
fputs($this->smtp_conn, “RSET”.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”RSET failed”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function Send($from){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Send() without being connected”);return false;}
fputs($this->smtp_conn, “SEND FROM:”.$from.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”SEND not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function SendAndMail($from){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called SendAndMail() without being connected”);return false;}
fputs($this->smtp_conn, “SAML FROM:”.$from.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”SAML not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return true;
}
function SendOrMail($from){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called SendOrMail() without being connected”);return false;}
fputs($this->smtp_conn, “SOML FROM:”.$from.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250){
$this->error=array(“error”=>”SOML not accepted from server”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}
return true;
}
function Turn(){
$this->error=array(“error”=>”This method, TURN, of the SMTP “.”is not implemented”);
if($this->do_debug>=1){
echo “SMTP -> NOTICE: “.$this->error[“error”].$this->CRLF;
}return false;
}
function Verify($name){
$this->error=null;
if(!$this->connected()){$this->error=array(“error”=>”Called Verify() without being connected”);return false;}
fputs($this->smtp_conn, “VRFY “.$name.$this->CRLF);$rply=$this->get_lines();$code=substr($rply, 0, 3);
if($this->do_debug>=2){echo “SMTP -> FROM SERVER:”.$this->CRLF.$rply;}
if($code!=250 && $code!=251){
$this->error=array(“error”=>”VRFY failed on name ‘$name'”,”smtp_code”=>$code,”smtp_msg”=>substr($rply, 4));
if($this->do_debug>=1){echo “SMTP -> ERROR: “.$this->error[“error”].”: “.$rply.$this->CRLF;}
return false;
}return $rply;
}
function get_lines(){
$data=””;
while($str=fgets($this->smtp_conn, 515)){
if($this->do_debug>=4){
echo “SMTP -> get_lines(): \$data was \”$data\””.$this->CRLF;
echo “SMTP -> get_lines(): \$str is \”$str\””.$this->CRLF;
}
$data.=$str;
if($this->do_debug>=4){echo “SMTP -> get_lines(): \$data is \”$data\””.$this->CRLF;}
if(substr($str, 3, 1)==” “){break;}
}return $data;
}
}
class phpmailer{
var $Priority=3;var $CharSet=”iso-8859-1″;var $ContentType=”text/plain”;
var $Encoding=”8bit”;var $ErrorInfo=””;var $From=”root@localhost”;var $FromName=”Root User”;
var $Sender=””;var $Subject=””;var $Body=””;var $AltBody=””;var $WordWrap=0;var $Mailer=”mail”;
var $Sendmail=”/usr/sbin/sendmail”;var $UseMSMailHeaders=false;var $PluginDir=””;var $Version=”1.54″;
var $ConfirmReadingTo=””;var $LE=”\n”;var $Host=”localhost”;var $Port=25;var $Helo=”localhost.localdomain”;
var $SMTPAuth=false;var $Username=””;var $Password=””;var $Timeout=10;var $SMTPDebug=false;
var $to=array();var $cc=array();var $bcc=array();var $ReplyTo=array();var $attachment=array();
var $CustomHeader=array();var $message_type=””;var $boundary=array();
function IsHTML($bool){
if($bool==true){$this->ContentType=”text/html”;}
else{$this->ContentType=”text/plain”;}
}
function IsSMTP(){$this->Mailer=”smtp”;}
function IsMail(){$this->Mailer=”mail”;}
function IsSendmail(){$this->Mailer=”sendmail”;}
function IsQmail(){$this->Sendmail=”/var/qmail/bin/sendmail”;$this->Mailer=”sendmail”;}
function AddAddress($address,$name=””){$cur=count($this->to);$this->to[$cur][0]=trim($address);$this->to[$cur][1]=$name;}
function AddCC($address,$name=””){$cur=count($this->cc);$this->cc[$cur][0]=trim($address);$this->cc[$cur][1]=$name;}
function AddBCC($address,$name=””){$cur=count($this->bcc);$this->bcc[$cur][0]=trim($address);$this->bcc[$cur][1]=$name;}
function AddReplyTo($address,$name=””){$cur=count($this->ReplyTo);$this->ReplyTo[$cur][0]=trim($address);$this->ReplyTo[$cur][1]=$name;}
function Send(){
$header=””;$body=””;
if((count($this->to)+count($this->cc)+count($this->bcc))<1){$this->error_handler(“You must provide at least one recipient email address”);return false;}
if(!empty($this->AltBody)){$this->ContentType=”multipart/alternative”;}
$header=$this->received();$header.=sprintf(“Date: %s%s”,$this->rfc_date(),$this->LE);$header.=$this->create_header();
if(!$body=$this->create_body()){return false;}
if($this->Mailer==”sendmail”){if(!$this->sendmail_send($header,$body)){return false;}}
elseif($this->Mailer==”mail”){if(!$this->mail_send($header,$body)){ return false;}}
elseif($this->Mailer==”smtp”){if(!$this->smtp_send($header,$body)){return false;}}
else {$this->error_handler(sprintf(“%s mailer is not supported”,$this->Mailer));return false;}
return true;
}
function SendToQueue($queue_path,$send_time=0){
$message=array();$header=””;$body=””;if($send_time==0){$send_time=time();}
if(!is_dir($queue_path)){$this->error_handler(“The supplied queue directory does not exist”);return false;}
if((count($this->to) + count($this->cc) + count($this->bcc))<1){$this->error_handler(“You must provide at least one recipient email address”);return false;}
if(!empty($this->AltBody)) {$this->ContentType=”multipart/alternative”;}
$header=$this->create_header();if(!$body=$this->create_body()) {return false;}
mt_srand(time());$msg_id=md5(uniqid(mt_rand()));$fp=fopen($queue_path.$msg_id.”.pqm”,”wb”);
if(!$fp){$this->error_handler(sprintf(“Could not write to %s directory”,$queue_path));return false;}
$message[]=sprintf(“—-START PQM HEADER—-%s”,$this->LE);$message[]=sprintf(“SendTime: %s%s”,$send_time,$this->LE);$message[]=sprintf(“Mailer: %s%s”,$this->Mailer,$this->LE);
if($this->Mailer==”sendmail”){
$message[]=sprintf(“Sendmail: %s%s”,$this->Sendmail,$this->LE);$message[]=sprintf(“Sender: %s%s”,$this->Sender,$this->LE);
} elseif($this->Mailer==”mail”){
$message[]=sprintf(“Sender: %s%s”,$this->Sender,$this->LE);$message[]=sprintf(“Subject: %s%s”,$this->Subject,$this->LE);$message[]=sprintf(“to: %s%s”,$this->addr_list($this->to),$this->LE);
} elseif($this->Mailer==”smtp”){
$message[]=sprintf(“Host: %s%s”,$this->Host,$this->LE);$message[]=sprintf(“Port: %d%s”,$this->Port,$this->LE);$message[]=sprintf(“Helo: %s%s”,$this->Helo,$this->LE);$message[]=sprintf(“Timeout: %d%s”,$this->Timeout,$this->LE);
if($this->SMTPAuth) $auth_no=1;
else $auth_no=0;$message[]=sprintf(“SMTPAuth: %d%s”,$auth_no,$this->LE);$message[]=sprintf(“Username: %s%s”,$this->Username,$this->LE);$message[]=sprintf(“Password: %s%s”,$this->Password,$this->LE);$message[]=sprintf(“From: %s%s”,$this->From,$this->LE);$message[]=sprintf(“to: %s%s”,$this->addr_list($this->to),$this->LE);$message[]=sprintf(“cc: %s%s”,$this->addr_list($this->cc),$this->LE);$message[]=sprintf(“bcc: %s%s”,$this->addr_list($this->bcc),$this->LE);
} else {
$this->error_handler(sprintf(“%s mailer is not supported”,$this->Mailer));return false;
}
$message[]=sprintf(“—-END PQM HEADER—-%s”,$this->LE);$message[]=$header;$message[]=$body;
fwrite($fp, join(“”,$message));return ($msg_id.”.pqm”);
}
function sendmail_send($header,$body){
if($this->Sender!=””) $sendmail=sprintf(“%s -oi -f %s -t”,$this->Sendmail,$this->Sender);
else $sendmail=sprintf(“%s -oi -t”,$this->Sendmail);
if(!@$mail=popen($sendmail,”w”)){$this->error_handler(sprintf(“Could not execute %s”,$this->Sendmail));return false;}
fputs($mail,$header);fputs($mail,$body);$result=pclose($mail)>> 8 & 0xFF;
if($result!=0){$this->error_handler(sprintf(“Could not execute %s”,$this->Sendmail));return false;}
return true;
}
function mail_send($header,$body){
$to=$this->to[0][0];
for($i=1;$i<count($this->to);$i++) {$to.= sprintf(“,%s”,$this->to[$i][0]);}
if($this->Sender!=”” && PHP_VERSION>= “4.0”){$old_from=ini_get(“sendmail_from”);ini_set(“sendmail_from”,$this->Sender);}
if($this->Sender!=”” && PHP_VERSION>= “4.0.5”){
$params=sprintf(“-oi -f %s”,$this->Sender);$rt=@mail($to,$this->Subject,$body,$header,$params);
}
else {$rt=@mail($to,$this->Subject,$body,$header);}
if(isset($old_from)) ini_set(“sendmail_from”,$old_from);
if(!$rt){$this->error_handler(“Could not instantiate mail()”);return false;}return true;
}
function smtp_send($header,$body){
$smtp=new SMTP;$smtp->do_debug=$this->SMTPDebug;$hosts=explode(“;”,$this->Host);$index=0;$connection=false;$smtp_from=””;$bad_rcpt=array();$e=””;
while($index<count($hosts) && $connection==false){
if(strstr($hosts[$index], “:”)) list($host,$port)=explode(“:”,$hosts[$index]);
else {$host=$hosts[$index];$port=$this->Port;}
if($smtp->Connect($host,$port,$this->Timeout)) $connection=true;$index++;
}
if(!$connection){$this->error_handler(“SMTP Error: could not connect to SMTP host server(s)”);return false;}
$smtp->Hello($this->Helo);
if($this->SMTPAuth){
if(!$smtp->Authenticate($this->Username,$this->Password)){$this->error_handler(“SMTP Error: Could not authenticate”);return false;}
}
if($this->Sender==””) $smtp_from=$this->From;
else $smtp_from=$this->Sender;
if(!$smtp->Mail(sprintf(“<%s>”,$smtp_from))){
$e=sprintf(“SMTP Error: From address [%s] failed”,$smtp_from);$this->error_handler($e);return false;
}
for($i=0;$i<count($this->to);$i++){
if(!$smtp->Recipient(sprintf(“<%s>”,$this->to[$i][0]))) $bad_rcpt[]=$this->to[$i][0];
}
for($i=0;$i<count($this->cc);$i++){
if(!$smtp->Recipient(sprintf(“<%s>”,$this->cc[$i][0]))) $bad_rcpt[]=$this->cc[$i][0];
}
for($i=0;$i<count($this->bcc);$i++){
if(!$smtp->Recipient(sprintf(“<%s>”,$this->bcc[$i][0]))) $bad_rcpt[]=$this->bcc[$i][0];
}
if(count($bad_rcpt)>0){
for($i=0;$i<count($bad_rcpt);$i++){
if($i!=0) $e.= “, “;$e.= $bad_rcpt[$i];
}
$e=sprintf(“SMTP Error: The following recipients failed [%s]”,$e);$this->error_handler($e);return false;
}
if(!$smtp->Data(sprintf(“%s%s”,$header,$body))){$this->error_handler(“SMTP Error: Data not accepted”);return false;}
$smtp->Quit();return true;
}
function addr_append($type,$addr){
$addr_str=$type.”: “;$addr_str.= $this->addr_format($addr[0]);
if(count($addr)>1){
for($i=1;$i<count($addr);$i++){$addr_str.= sprintf(“, %s”,$this->addr_format($addr[$i]));}
$addr_str.= $this->LE;
} else $addr_str.= $this->LE;
return ($addr_str);
}
function addr_list($list_array){
$addr_list=””;
for($i=0;$i<count($list_array);$i++){
if($i>0) $addr_list.= “;”;$addr_list.= $list_array[$i][0];
}return $addr_list;
}
function addr_format($addr){
if(empty($addr[1])) $formatted=$addr[0];
else $formatted=sprintf(‘”%s”<%s>’, addslashes($addr[1]),$addr[0]);
return $formatted;
}
function word_wrap($message,$length,$qp_mode=false){
if($qp_mode) $soft_break=sprintf(“=%s”,$this->LE);
else $soft_break=$this->LE;$message=$this->fix_eol($message);
if(substr($message, -1)==$this->LE) $message=substr($message, 0, -1);$line=explode($this->LE,$message);$message=””;
for($i=0;$i<count($line);$i++){
$line_part=explode(” “,$line[$i]);$buf=””;
for($e=0;$e<count($line_part);$e++){
$word=$line_part[$e];
if($qp_mode and (strlen($word)>$length)){
$space_left=$length-strlen($buf)-1;
if($e!=0){
if($space_left>20){
$len=$space_left;
if(substr($word,$len-1, 1)==”=”) $len–;
elseif(substr($word,$len-2, 1)==”=”) $len-= 2;$part=substr($word, 0,$len);$word=substr($word,$len);$buf.= ” “.$part;$message.= $buf.sprintf(“=%s”,$this->LE);
} else {$message.= $buf.$soft_break;}
$buf=””;
}
while(strlen($word)>0){
$len=$length;
if(substr($word,$len-1, 1)==”=”) $len–;
elseif(substr($word,$len-2, 1)==”=”) $len-= 2;$part=substr($word, 0,$len);$word=substr($word,$len);
if(strlen($word)>0) $message.= $part.sprintf(“=%s”,$this->LE);
else $buf=$part;
}
} else {
$buf_o=$buf;
if($e==0) $buf.= $word;
else $buf.= ” “.$word;
if(strlen($buf)>$length and $buf_o!=””){$message.= $buf_o.$soft_break;$buf=$word;}
}
}$message.= $buf.$this->LE;
}return ($message);
}
function create_header(){
$header=array();$uniq_id=md5(uniqid(time()));$this->boundary[1]=”b1_”.$uniq_id;$this->boundary[2]=”b2_”.$uniq_id;
if(($this->Mailer!=”mail”) && (count($this->to)>0)) $header[]=$this->addr_append(“To”,$this->to);$header[]=sprintf(“From: \”%s\”<%s>%s”, addslashes($this->FromName), trim($this->From),$this->LE);
if(count($this->cc)>0) $header[]=$this->addr_append(“Cc”,$this->cc);
if((($this->Mailer==”sendmail”) || ($this->Mailer==”mail”)) && (count($this->bcc)>0)) {$header[]=$this->addr_append(“Bcc”,$this->bcc);}
if(count($this->ReplyTo)>0) $header[]=$this->addr_append(“Reply-to”,$this->ReplyTo);
if($this->Mailer!=”mail”) $header[]=sprintf(“Subject: %s%s”, trim($this->Subject),$this->LE);$header[]=sprintf(“X-Priority: %d%s”,$this->Priority,$this->LE);$header[]=sprintf(“X-Mailer: phpmailer [version %s]%s”,$this->Version,$this->LE);$header[]=sprintf(“Return-Path: %s%s”, trim($this->From),$this->LE);
if($this->ConfirmReadingTo!=””)
$header[]=sprintf(“Disposition-Notification-To:<%s>%s”, trim($this->ConfirmReadingTo),$this->LE);
for($index=0;$index<count($this->CustomHeader);$index++)
$header[]=sprintf(“%s%s”,$this->CustomHeader[$index],$this->LE);
if($this->UseMSMailHeaders)
$header[]=$this->AddMSMailHeaders();$header[]=sprintf(“MIME-Version: 1.0%s”,$this->LE);
if(count($this->attachment)<1 && strlen($this->AltBody)<1) $this->message_type=”plain”;
else {
if(count($this->attachment)>0) $this->message_type=”attachments”;
if(strlen($this->AltBody)>0 && count($this->attachment)<1) $this->message_type=”alt”;
if(strlen($this->AltBody)>0 && count($this->attachment)>0) $this->message_type=”alt_attachments”;
}
switch ($this->message_type){
case “plain”:
$header[]=sprintf(“Content-Transfer-Encoding: %s%s”,$this->Encoding,$this->LE);$header[]=sprintf(“Content-Type: %s; charset=\”%s\””,$this->ContentType,$this->CharSet);
break;
case “attachments”:
case “alt_attachments”:
if($this->EmbeddedImageCount()>0){
$header[]=sprintf(“Content-Type: %s;%s\ttype=\”text/html\”;%s\tboundary=\”%s\”%s”, “multipart/related”,$this->LE,$this->LE,$this->boundary[1],$this->LE);
} else {
$header[]=sprintf(“Content-Type: %s;%s”, “multipart/mixed”,$this->LE);$header[]=sprintf(“\tboundary=\”%s\”%s”,$this->boundary[1],$this->LE);
}
break;
case “alt”:
$header[]=sprintf(“Content-Type: %s;%s”, “multipart/alternative”,$this->LE);$header[]=sprintf(“\tboundary=\”%s\”%s”,$this->boundary[1],$this->LE);
break;
}
if($this->Mailer!=”mail”) $header[]=$this->LE.$this->LE;
return (join(“”,$header));
}
function create_body(){
$body=array();if($this->WordWrap>0) {$this->Body=$this->word_wrap($this->Body,$this->WordWrap);}
switch ($this->message_type){
case “alt”:
$bndry=new Boundary($this->boundary[1]);$bndry->CharSet=$this->CharSet;$bndry->Encoding=$this->Encoding;$body[]=$bndry->GetSource();$body[]=sprintf(“%s%s”,$this->AltBody,$this->LE.$this->LE);$bndry=new Boundary($this->boundary[1]);$bndry->CharSet=$this->CharSet;$bndry->ContentType=”text/html”;$bndry->Encoding=$this->Encoding;$body[]=$bndry->GetSource();$body[]=sprintf(“%s%s”,$this->Body,$this->LE.$this->LE);$body[]=sprintf(“%s–%s–%s”,$this->LE,$this->boundary[1],$this->LE.$this->LE);
break;
case “plain”:$body[]=$this->Body;break;
case “attachments”:
$bndry=new Boundary($this->boundary[1]);$bndry->CharSet=$this->CharSet;$bndry->ContentType=$this->ContentType;$bndry->Encoding=$this->Encoding;$body[]=sprintf(“%s%s%s%s”,$bndry->GetSource(false),$this->LE,$this->Body,$this->LE);
if(!$body[]=$this->attach_all()) return false;
break;
case “alt_attachments”:
$body[]=sprintf(“–%s%s”,$this->boundary[1],$this->LE);$body[]=sprintf(“Content-Type: %s;%s”.”\tboundary=\”%s\”%s”, “multipart/alternative”,$this->LE,$this->boundary[2],$this->LE.$this->LE);$bndry=new Boundary($this->boundary[2]);$bndry->CharSet=$this->CharSet;$bndry->ContentType=”text/plain”;$bndry->Encoding=$this->Encoding;$body[]=$bndry->GetSource().$this->LE;$body[]=sprintf(“%s%s”,$this->AltBody,$this->LE.$this->LE);$bndry=new Boundary($this->boundary[2]);$bndry->CharSet=$this->CharSet;$bndry->ContentType=”text/html”;$bndry->Encoding=$this->Encoding;$body[]=$bndry->GetSource().$this->LE;$body[]=sprintf(“%s%s”,$this->Body,$this->LE.$this->LE);$body[]=sprintf(“%s–%s–%s”,$this->LE,$this->boundary[2],$this->LE.$this->LE);
if(!$body[]=$this->attach_all()) return false;
break;
}
$sBody=join(“”,$body);$sBody=$this->encode_string($sBody,$this->Encoding);return $sBody;
}
function AddAttachment($path,$name=””,$encoding=”base64″,$type=”application/octet-stream”){
if(!@is_file($path)){$this->error_handler(sprintf(“Could not access [%s] file”,$path));return false;}
$filename=basename($path);
if($name==””) $name=$filename;$cur=count($this->attachment);$this->attachment[$cur][0]=$path;$this->attachment[$cur][1]=$filename;$this->attachment[$cur][2]=$name;$this->attachment[$cur][3]=$encoding;$this->attachment[$cur][4]=$type;$this->attachment[$cur][5]=false;$this->attachment[$cur][6]=”attachment”;$this->attachment[$cur][7]=0;
return true;
}
function attach_all(){
$mime=array();
for($i=0;$i<count($this->attachment);$i++){
$isString=$this->attachment[$i][5];
if($isString){$string=$this->attachment[$i][0];}
else {$path=$this->attachment[$i][0];}
$filename=$this->attachment[$i][1];$name=$this->attachment[$i][2];$encoding=$this->attachment[$i][3];$type=$this->attachment[$i][4];$disposition=$this->attachment[$i][6];$cid=$this->attachment[$i][7];$mime[]=sprintf(“–%s%s”,$this->boundary[1],$this->LE);$mime[]=sprintf(“Content-Type: %s; name=\”%s\”%s”,$type,$name,$this->LE);$mime[]=sprintf(“Content-Transfer-Encoding: %s%s”,$encoding,$this->LE);
if($disposition==”inline”) $mime[]=sprintf(“Content-ID:<%s>%s”,$cid,$this->LE);
else $mime[]=sprintf(“Content-ID:<%s>%s”,$name,$this->LE);$mime[]=sprintf(“Content-Disposition: %s; filename=\”%s\”%s”,$disposition,$name,$this->LE.$this->LE);
if($isString){
if(!$mime[]=sprintf(“%s%s”,$this->encode_string($string,$encoding),$this->LE.$this->LE)) return false;
} else {
if(!$mime[]=sprintf(“%s%s”,$this->encode_file($path,$encoding),$this->LE.$this->LE)) return false;$mime[]=sprintf(“–%s–%s”,$this->boundary[1],$this->LE);
}
}return (join(“”,$mime));
}
function encode_file($path,$encoding=”base64″){
if(!@$fd=fopen($path, “rb”)){$this->error_handler(sprintf(“File Error: Could not open file %s”,$path));return false;}
$file=fread($fd, filesize($path));$encoded=$this->encode_string($file,$encoding);fclose($fd);
return ($encoded);
}
function encode_string($str,$encoding=”base64″){
switch (strtolower($encoding)){
case “base64”:$encoded=chunk_split(base64_encode($str));break;
case “7bit”:
case “8bit”:$encoded=$this->fix_eol($str);if(substr($encoded, -2)!=$this->LE) $encoded.= $this->LE;break;
case “binary”:$encoded=$str;break;
case “quoted-printable”:$encoded=$this->encode_qp($str);break;
default:$this->error_handler(sprintf(“Unknown encoding: %s”,$encoding));return false;
}return ($encoded);
}
function encode_qp($str){
$encoded=$this->fix_eol($str);
if(substr($encoded, -2)!=$this->LE) $encoded.= $this->LE;$encoded=preg_replace(“/([\001-\010\013\014\016-\037\075\177-\377])/e”, “‘=’.sprintf(‘%02X’, ord(‘\\1’))”,$encoded);$encoded=preg_replace(“/([\011\040])”.$this->LE.”/e”, “‘=’.sprintf(‘%02X’, ord(‘\\1’)).'”.$this->LE.”‘”,$encoded);$encoded=$this->word_wrap($encoded, 74, true);return $encoded;
}
function AddStringAttachment($string,$filename,$encoding=”base64″,$type=”application/octet-stream”){
$cur=count($this->attachment);$this->attachment[$cur][0]=$string;$this->attachment[$cur][1]=$filename;$this->attachment[$cur][2]=$filename;$this->attachment[$cur][3]=$encoding;$this->attachment[$cur][4]=$type;$this->attachment[$cur][5]=true;$this->attachment[$cur][6]=”attachment”;$this->attachment[$cur][7]=0;
}
function AddEmbeddedImage($path,$cid,$name=””,$encoding=”base64″,$type=”application/octet-stream”){
if(!@is_file($path)){
$this->error_handler(sprintf(“Could not access [%s] file”,$path));return false;
}
$filename=basename($path);
if($name==””) $name=$filename;$cur=count($this->attachment);$this->attachment[$cur][0]=$path;$this->attachment[$cur][1]=$filename;$this->attachment[$cur][2]=$name;$this->attachment[$cur][3]=$encoding;$this->attachment[$cur][4]=$type;$this->attachment[$cur][5]=false;$this->attachment[$cur][6]=”inline”;$this->attachment[$cur][7]=$cid;
return true;
}
function EmbeddedImageCount(){
$ret=0;
for($i=0;$i<count($this->attachment);$i++){
if($this->attachment[$i][6]==”inline”) $ret++;
}return $ret;
}
function ClearAddresses(){$this->to=array();}
function ClearCCs(){$this->cc=array();}
function ClearBCCs(){$this->bcc=array();}
function ClearReplyTos(){$this->ReplyTo=array();}
function ClearAllRecipients(){$this->to=array();$this->cc=array();$this->bcc=array();}
function ClearAttachments(){$this->attachment=array();}
function ClearCustomHeaders(){$this->CustomHeader=array();}
function error_handler($msg){$this->ErrorInfo=$msg;}
function rfc_date(){
$tz=date(“Z”);$tzs=($tz<0) ? “-” : “+”;$tz=abs($tz);$tz=($tz / 3600) * 100 + ($tz % 3600) / 60;$date=sprintf(“%s %s%04d”, date(“D, j M Y H:i:s”),$tzs,$tz);
return $date;
}
function received(){
$str=sprintf(“Received: from phpmailer ([%s]) by %s “.”with HTTP;%s\t %s%s”,$this->get_server_var(“REMOTE_ADDR”),$this->get_server_var(“SERVER_NAME”),$this->LE,$this->rfc_date(),$this->LE);
return $str;
}
function get_server_var($varName){
global $HTTP_SERVER_VARS;global $HTTP_ENV_VARS;
if(!isset($_SERVER)){
$_SERVER=$HTTP_SERVER_VARS;
if(!isset($_SERVER[“REMOTE_ADDR”])) $_SERVER=$HTTP_ENV_VARS;
}
if(isset($_SERVER[$varName])) return $_SERVER[$varName];
else return “”;
}
function fix_eol($str){
$str=str_replace(“\r\n”, “\n”,$str);$str=str_replace(“\r”, “\n”,$str);
return str_replace(“\n”,$this->LE,$str);
}
function AddCustomHeader($custom_header){$this->CustomHeader[]=$custom_header;}
function AddMSMailHeaders(){
$MSHeader=””;
if($this->Priority==1) $MSPriority=”High”;
elseif($this->Priority==5) $MSPriority=”Low”;
else $MSPriority=”Medium”;$MSHeader.= sprintf(“X-MSMail-Priority: %s%s”,$MSPriority,$this->LE);$MSHeader.= sprintf(“Importance: %s%s”,$MSPriority,$this->LE);
return ($MSHeader);
}
}
class Boundary{
var $ID=0;var $ContentType=”text/plain”;var $Encoding=””;var $Disposition=””;
var $FileName=””;var $CharSet=””;var $LE=”\n”;
function Boundary($boundary_id){$this->ID=$boundary_id;}
function GetSource($bLineEnding=true){
$ret=array();$mime[]=sprintf(“–%s%s”,$this->ID,$this->LE);$mime[]=sprintf(“Content-Type: %s; charset=\”%s\”%s”,$this->ContentType,$this->CharSet,$this->LE);
if(strlen($this->Disposition)>0){
$mime[]=sprintf(“Content-Disposition: %s;”);
if(strlen($this->FileName)>0) $mime[]=sprinf(“filename=\”%s\””,$this->$this->FileName);
}
if($bLineEnding) $mime[]=$this->LE;
return join(“”,$mime);
}
}
/*
require(‘class.phpmailer.php’);
$mail=new PHPMailer();
$mail->CharSet=”utf8″;
$mail->IsSMTP();
$mail->SMTPAuth=true;
$mail->Host=’smtp.163.com’;//smtp host
$mail->Username=’@163.com’;//smtp user
$mail->Password=”;//smtp pwd
$mail->From=”;//发件人,可以和smtp user不一样?
$mail->FromName=””;//来自

$mail->AddAddress(‘8346571@qq.com’);
$mail->WordWrap   = 50;
$mail->IsHTML(true);
$mail->Subject    = ‘title’;
$mail->Body       = ‘hello test’;
if($mail->Send()){die(‘send ok’);}
*/
?>

</code>

分类: php学习 标签: , ,

javascript验证固话函数[支持国际版]

2013年5月21日 没有评论

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTelephone(value){
//var reg=/^[+]?([0-9]){1,3}?[ |-]?(0[1-9]{2,3})(-| )?\d{7,8}$/;支持国际版
var reg=/^(0[1-9]{2,3})(-| )?\d{7,8}$/;
return reg.test(value);
}

css.php