当前位置: 编程技术>php
本页文章导读:
▪phpexcel导入excel到数据库的代码 phpexcel实现导入内容到数据库中,代码如下:
<?php
/**
* phpexcel实例 导入数据库
* by www.
*/
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); .........
▪php实例:经典email验证类 分享一个email验证类,代码如下:
<?php
/**
* EMail地址验证类
* 根据RFC5321,RFC5322,验证电子邮件地址
*/
final class EmailAddressValidator
{
/**
* 电子邮件地址验证
*
.........
▪php 邮件发送类(smtp方式或mail函数方式) php实现的邮件发送类,二种方式:
smtp方式与mail函数方式。
代码:
<?php
/**
* 邮件发送类
* by www.
*/
Class sendmail{
public $smtp_host;
public $smtp_port = 25;
public $smtp_user;
public.........
[1]phpexcel导入excel到数据库的代码
来源: 互联网 发布时间: 2013-12-24
phpexcel实现导入内容到数据库中,代码如下:
<?php
/**
* phpexcel实例 导入数据库
* by www.
*/
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www./../Classes/');//设置环境变量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //这个是读 xls的
$inputFileType = 'Excel2007';//这个是计xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
/*
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn = $sheet->getHighestColumn(); //取得总列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
$highestRow = $objWorksheet->getHighestRow();//取得总列数
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br />";
$headtitle=array();
for ($row = 1;$row <= $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++)
{
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
$info = array(
'word1'=>"$strs[0]",
'word2'=>"$strs[1]",
'word3'=>"$strs[2]",
'word4'=>"$strs[3]",
);
//引处可放置代码,写入数据库
print_r($info);
echo '<br />';
}
?>
您可能感兴趣的文章:
PHPExcel常用方法举例
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
phpExcel类的使用方法分享
phpexcel导出excel的经典实例
PHPExcel读取excel文件的例子
phpexcel类库实例 支持(excel2003 excel2007)
phpexcel导出数据的实例代码
phpexcel快速开发指南(不错)
phpExcel中文帮助手册(知识点)
使用PHPExcel判别和格式化Excel中的日期格式的例子
phpexcel导出excel的颜色与网页中颜色不一致的解决方法
CI中使用PHPExcel导出数据到Excel
[2]php实例:经典email验证类
来源: 互联网 发布时间: 2013-12-24
分享一个email验证类,代码如下:
<?php
/**
* EMail地址验证类
* 根据RFC5321,RFC5322,验证电子邮件地址
*/
final class EmailAddressValidator
{
/**
* 电子邮件地址验证
*
* @access private
* @var string $_emailAddress
*/
private $_emailAddress;
/**
* 是否允许带引号的字符串本地部分
*
* @access private
* @var bool $_quotedString
*/
private $_quotedString = false;
/**
* An obsolete local part
*
* @access private
* @var bool $_obsolete
*/
private $_obsolete = false;
/**
* A domain literal domain
*
* @access private
* @var bool $_domainLiteral
*/
private $_domainLiteral = false;
/**
* Comments and folding white spaces
*
* @access private
* @var bool $_cfws
*/
private $_cfws = false;
/**
* Set the email address and turn on the relevant standard if required
*
* @access public
* @param string $emailAddress
* @param integer $standard
*/
public function __construct($emailAddress, $standard = null)
{
// Set the email address
$this->_emailAddress = $emailAddress;
// Turn on the RFC 5321 standard if requested
if ($standard == 5321)
{
$this->setStandard5321();
}
// Otherwise turn on the RFC 5322 standard if requested
elseif ($standard == 5322)
{
$this->setStandard5322();
}
}
/**
* Call the constructor fluently
*
* @access public
* @static
* @param string $emailAddress
* @param null|integer $standard
* @return EmailAddressValidator
*/
public static function setEmailAddress($emailAddress, $standard = null)
{
return new self($emailAddress, $standard);
}
/**
* Validate the email address according to RFC 5321 and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setStandard5321($allow = true)
{
// A quoted string local part is either allowed (true) or not (false)
$this->_quotedString = $allow;
// A domain literal domain is either allowed (true) or not (false)
$this->_domainLiteral = $allow;
// Return itself
return $this;
}
/**
* Validate the email address according to RFC 5322 and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setStandard5322($allow = true)
{
// An obsolete local part is either allowed (true) or not (false)
$this->_obsolete = $allow;
// A domain literal domain is either allowed (true) or not (false)
$this->_domainLiteral = $allow;
// Comments and folding white spaces are either allowed (true) or not (false)
$this->_cfws = $allow;
// Return itself
return $this;
}
/**
* Either allow (true) or disallow (false) a quoted string local part and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setQuotedString($allow = true)
{
// Either allow (true) or disallow (false) a quoted string local part
$this->_quotedString = $allow;
// Return itself
return $this;
}
/**
* Either allow (true) or disallow (false) an obsolete local part and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setObsolete($allow = true)
{
// Either allow (true) or disallow (false) an obsolete local part
$this->_obsolete = $allow;
// Return itself
return $this;
}
/**
* Either allow (true) or disallow (false) a domain literal domain and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setDomainLiteral($allow = true)
{
// Either allow (true) or disallow (false) a domain literal domain
$this->_domainLiteral = $allow;
// Return itself
return $this;
}
/**
* Either allow (true) or disallow (false) comments and folding white spaces and return itself
*
* @access public
* @param bool $allow
* @return EmailAddressValidator
*/
public function setCFWS($allow = true)
{
// Either allow (true) or disallow (false) comments and folding white spaces
$this->_cfws = $allow;
// Return itself
return $this;
}
/**
* Return the regular expression for a dot atom local part
*
* @access private
* @return string
*/
private function _getDotAtom()
{
return "([!#-'*+\/-9=?^-~-]+)(?>\.(?1))*";
}
/**
* Return the regular expression for a quoted string local part
*
* @access private
* @return string
*/
private function _getQuotedString()
{
return '"(?>[ !#-\[\]-~]|\\\[ -~])*"';
}
/**
* Return the regular expression for an obsolete local part
*
* @access private
* @return string
*/
private function _getObsolete()
{
return '([!#-\'*+\/-9=?^-~-]+|"(?>'
. $this->_getFWS()
. '(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*'
. $this->_getFWS()
. '")(?>'
. $this->_getCFWS()
. '\.'
. $this->_getCFWS()
. '(?1))*';
}
/**
* Return the regular expression for a domain name domain
*
* @access private
* @return string
*/
private function _getDomainName()
{
return '([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>'
. $this->_getCFWS()
. '\.'
. $this->_getCFWS()
. '(?2)){0,126}';
}
/**
* Return the regular expression for an IPv6 address
*
* @access private
* @return string
*/
private function _getIPv6()
{
return '([a-f0-9]{1,4})(?>:(?3)){7}|(?!(?:.*[a-f0-9][:\]]){8,})((?3)(?>:(?3)){0,6})?::(?4)?';
}
/**
* Return the regular expression for an IPv4-mapped IPv6 address
*
* @access private
* @return string
*/
private function _getIPv6v4()
{
return '(?3)(?>:(?3)){5}:|(?!(?:.*[a-f0-9]:){6,})(?5)?::(?>((?3)(?>:(?3)){0,4}):)?';
}
/**
* Return the regular expression for an IPv4 address
*
* @access private
* @return string
*/
private function _getIPv4()
{
return '(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?6)){3}';
}
/**
* Return the regular expression for a domain literal domain
*
* @access private
* @return string
*/
private function _getDomainLiteral()
{
return '\[(?:(?>IPv6:(?>'
. $this->_getIPv6()
. '))|(?>(?>IPv6:(?>'
. $this->_getIPv6v4()
. '))?'
. $this->_getIPv4()
. '))\]';
}
/**
* Return either the regular expression for folding white spaces or its backreference if allowed
*
* @access private
* @var bool $define
* @return string
*/
private function _getFWS($define = false)
{
// Return the backreference if $define is set to false otherwise return the regular expression
if ($this->_cfws)
{
return !$define ? '(?P>fws)' : '(?<fws>(?>[ ]+(?>\x0D\x0A[ ]+)*)?)';
}
}
/**
* Return the regular expression for comments
*
* @access private
* @return string
*/
private function _getComments()
{
return '(?<comment>\((?>'
. $this->_getFWS()
. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?P>comment)))*'
. $this->_getFWS()
. '\))';
}
/**
* Return either the regular expression for comments and folding white spaces or its backreference if allowed
*
* @access private
* @var bool $define
* @return string
*/
private function _getCFWS($define = false)
{
// Return the backreference if $define is set to false
if ($this->_cfws && !$define)
{
return '(?P>cfws)';
}
// Otherwise return the regular expression
if ($this->_cfws)
{
return '(?<cfws>(?>(?>(?>'
. $this->_getFWS(true)
. $this->_getComments()
. ')+'
. $this->_getFWS()
. ')|'
. $this->_getFWS()
. ')?)';
}
}
/**
* Establish, and return, the valid format for the local part
*
* @access private
* @return string
*/
private function _getLocalPart()
{
// The local part may be obsolete if allowed
if ($this->_obsolete)
{
return $this->_getObsolete();
}
// Or the local part may be either a dot atom or a quoted string if the latter is allowed
if ($this->_quotedString)
{
return '(?>' . $this->_getDotAtom() . '|' . $this->_getQuotedString() . ')';
}
// Otherwise the local part may only be a dot atom
return $this->_getDotAtom();
}
/**
* Establish, and return, the valid format for the domain
*
* @access private
* @return string
*/
private function _getDomain()
{
// The domain may be either a domain name or a domain literal if the latter is allowed
if ($this->_domainLiteral)
{
return '(?>' . $this->_getDomainName() . '|' . $this->_getDomainLiteral() . ')';
}
// Otherwise the domain must be a domain name
return $this->_getDomainName();
}
/**
* Check to see if the domain can be resolved to MX RRs
*
* @access private
* @param array $domain
* @return bool
*/
private function _verifyDomain($domain)
{
// Return false if the domain cannot be resolved to MX RRs
if (!checkdnsrr(end($domain), 'MX'))
{
return false;
}
// Otherwise return true
return true;
}
/**
* Perform the validation check on the email address's syntax and, if required, call _verifyDomain()
*
* @access public
* @param bool $verify
* @return bool|integer
*/
public function isValid($verify = false)
{
// Return false if the email address has an incorrect syntax
if (!preg_match(
'/^'
. $this->_getCFWS()
. $this->_getLocalPart()
. $this->_getCFWS()
. '@'
. $this->_getCFWS()
. $this->_getDomain()
. $this->_getCFWS(true)
. '$/isD'
, $this->_emailAddress
))
{
return false;
}
// Otherwise check to see if the domain can be resolved to MX RRs if required
if ($verify)
{
// Return 0 if the domain cannot be resolved to MX RRs
if (!$this->_verifyDomain(explode('@', $this->_emailAddress)))
{
return 0;
}
// Otherwise return true
return true;
}
// Otherwise return 1
return 1;
}
}
email验证类的调用示例:
<?php
$valid = array(
1 => 'test@example.com',
2 => 'test@example.co.uk',
3 => 'test@example.museum',
4 => 'test@example',
5 => 'test@xn--example.com',
6 => 'test@example.xn--com',
7 => 'test.test@example.com',
8 => "!#$%&'*+-/=?^_`{|}~@example.com",
9 => 'test@123.com',
10 => 'test@example.123',
10 => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl@example.com',
11 => 'test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.com',
11 => 'test@example.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk',
12 => 'a@a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l
.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v',
13 => '"test"@example.com',
14 => '"test@test"@example.com',
15 => '"test".test@example.com',
16 => 'test@[255.255.255.255]',
17 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]',
18 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::FFFF]',
19 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255]',
20 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF::255.255.255.255]',
21 => ' test @ example . com ',
22 => '(comment)test@example.com',
23 => '((nested)comment)test@example.com',
24 => 'test@(comment)[255.255.255.255]',
25 => '"My name".is."Michael" (and I am (really) awesome) @ ("That\'s what she said") example . com',
);
$invalid = array(
1 => 'test',
2 => 'test@',
3 => '@example.com',
4 => 'test.@example.com',
5 => '.test@example.com',
6 => 'test..test@example.com',
7 => 'test@example..com',
8 => "tést@example.com",
9 => '"test@example.com',
10 => 'test"@example.com',
10 => '"\"@example.com',
11 => '"test"account"@example.com',
12 => 'a@a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j
.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x',
13 => '"test"test@example.com',
14 => 'test\@test@example.com',
15 => 'test@exam!ple.com',
16 => 'test@[255.255.255.256]',
17 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]',
18 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::FFFF:FFFF]',
19 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255]',
20 => 'test@[IPv6:FFFF:FFFF:FFFF:FFFF:FFFF::FFFF:255.255.255.255]',
21 => 'test test@example.com',
22 => '(commenttest@example.com',
23 => '((nestedcomment)test@example.com',
24 => 'test@[255(comment).255.255.255]',
25 => ' @ ',
);
include 'EmailAddressValidator.php';
foreach ($valid as $address)
{
if (!EmailAddressValidator::setEmailAddress($address, 5322)->isValid())
{
echo 'Incorrect result: ' . $address . ' is a <strong>valid</strong> address.<br />';
}
}
foreach ($invalid as $address)
{
if (EmailAddressValidator::setEmailAddress($address, 5322)->isValid())
{
echo 'Incorrect result: ' . $address . ' is an <strong>invalid</strong> address.<br />';
}
}
[3]php 邮件发送类(smtp方式或mail函数方式)
来源: 互联网 发布时间: 2013-12-24
php实现的邮件发送类,二种方式:
smtp方式与mail函数方式。
代码:
<?php
/**
* 邮件发送类
* by www.
*/
Class sendmail{
public $smtp_host;
public $smtp_port = 25;
public $smtp_user;
public $smtp_password;
public $from_name;
public $SendFromMail;
public $mail_to;
public $subject;
public $message;
public $headers = '';
public $ContentType = 'html';
public $charset = 'windows-1251';
public $smtp_debug = true;
public $socket;
public $error;
public $SendMailVia = 'smtp';
public function __construct()
{
if($this->SendFromMail == ''){
$this->SendFromMail = $this->smtp_user;
}
}
public function Send($mail_to = '', $subject = '', $message = '')
{
if($mail_to!=''){$this->mail_to = stripslashes($mail_to);}
if($subject!=''){$this->subject = stripslashes($subject);}
if($message!=''){$this->message = $message;}
$meilsArr = array_filter($this->GetMailAndNameArr());
if(trim($this->mail_to)==''){$this->error = 'Enter the recipient address'; }
if($meilsArr == array()){$this->error = 'Please enter a valid recipient address'; }
foreach ($meilsArr as $val)
{
$validEmail = $this->validEmail($val[2]);
if($validEmail)
{
if($this->SendMailVia=='smtp'){
return $this->SMTPsend($mail_to = $val[2], $name_to = $val[1]);
}
else{
return $this->MAILsend($mail_to = $val[2], $name_to = $val[1]);
}
}
}
}
public function MAILsend($mail_to, $name_to)
{
if($this->ContentType=="text"){
$header="Content-Type: text/plain; charset=".$this->charset."";
}
else{
$header="Return-Path: ".$this->smtp_user."\n".
"Reply-To: ".$this->SendFromMail."\n".
"From: ".$this->from_name." <".$this->SendFromMail.">\n".
"Subject: ".$this->subject."\n".
"Content-Type: text/html; charset=".$this->charset."\n";
}
if(mail("$name_to <$mail_to>",$this->subject,$this->message,$header)){
return true;
}else{
return false;
}
}
public function SMTPsend($mail_to, $name_to)
{
$SEND = "Date: ".date("D, d M Y H:i:s") . "\r\n";
$SEND .= 'Subject: =?'.$this->charset.'?B?'.base64_encode($this->subject)."=?=\r\n";
if ($this->headers!=''){ $SEND .= $this->headers."\r\n\r\n"; }
else
{
$SEND .= "Reply-To: ".$this->SendFromMail."\r\n";
$SEND .= "MIME-Version: 1.0\r\n";
$SEND .= "Content-Type: text/".$this->ContentType."; charset=\"".$this->charset."\"\r\n";
$SEND .= "Content-Transfer-Encoding: 8bit\r\n";
$SEND .= "From: \"".$this->from_name."\" <".$this->SendFromMail.">\r\n";
$SEND .= "To: $name_to <$mail_to>\r\n";
$SEND .= "X-Priority: 3\r\n\r\n";
}
$SEND .= $this->message."\r\n";
$socket = fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, 30);
if(!socket)
{
if($this->smtp_debug) $this->error = $errno." - ".$errstr;
return false;
}
if (!$this->server_parse($socket, "220", __LINE__)){ return false; }
fputs($socket, "HELO ".$this->smtp_host. "\r\n");
if (!$this->server_parse($socket, "250", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Can not send HELO!</p>';
fclose($socket);
return false;
}
fputs($socket, "AUTH LOGIN\r\n");
if (!$this->server_parse($socket, "334", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Can not find an answer to a request authorization.</p>';
fclose($socket);
return false;
}
fputs($socket, base64_encode($this->smtp_user) . "\r\n");
if (!$this->server_parse($socket, "334", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Login authorization was not accepted by server!</p>';
fclose($socket);
return false;
}
fputs($socket, base64_encode($this->smtp_password) . "\r\n");
if (!$this->server_parse($socket, "235", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>No password was not accepted as a true server! Authorization Error!</p>';
fclose($socket);
return false;
}
fputs($socket, "MAIL FROM: <".$this->smtp_user.">\r\n");
if (!$this->server_parse($socket, "250", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Unable to send command MAIL FROM: </p>';
fclose($socket);
return false;
}
fputs($socket, "RCPT TO: <" . $mail_to . ">\r\n");
if (!$this->server_parse($socket, "250", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Unable to send command RCPT TO: </p>';
fclose($socket);
return false;
}
fputs($socket, "DATA\r\n");
if (!$this->server_parse($socket, "354", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Unable to send command DATA</p>';
fclose($socket);
return false;
}
fputs($socket, $SEND."\r\n.\r\n");
if (!$this->server_parse($socket, "250", __LINE__)) {
if ($this->smtp_debug) $this->error = '<p>Unable to send the message body. The letter was sent!</p>';
fclose($socket);
return false;
}
fputs($socket, "QUIT\r\n");
fclose($socket);
return TRUE;
}
private function GetMailAndNameArr(){
$mailingArr = array();
$tos = preg_split("/;|,/",$this->mail_to);
$pregcode = '/(.*?)<(.*?)>/i';
foreach($tos as $to)
{
if(preg_match('/(.*?)<(.*?)>/i',$to,$matches))
{
unset($matches[0]);
$matches[1] = trim(str_replace('"','',$matches[1]));
$matches[2] = trim($matches[2]);
$mailingArr[] =$matches;
}
elseif(preg_match('/\b([A-Z0-9._%-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})\b/i',$to,$matches2))
{
unset($matches[0]);
$matches[1] = trim(str_replace('"','',$matches2[1]));
$matches[2] = trim($matches2[0]);
$mailingArr[] =$matches;
}
}
return $mailingArr;
}
private function server_parse($socket, $response, $line = __LINE__) {
while (substr($server_response, 3, 1) != ' ') {
if (!($server_response = fgets($socket, 256))) {
if ($this->smtp_debug) $this->error = "<p>$line Problems sending mail! $response</p>";
return false;
}
}
if (!(substr($server_response, 0, 3) == $response)) {
if ($this->smtp_debug) $this->error = "<p>$line Problems sending mail! $response</p>";
return false;
}
return true;
}
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
$msg = '';
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64){
$msg = 'local part length exceeded';
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255){
$msg = ' domain part length exceeded ';
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.'){
$msg = ' local part starts or ends with .';
$isValid = false;
}
else if (preg_match('/\\.\\./', $local)){
$msg = 'local part has two consecutive dots';
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
$msg = 'character not valid in domain part';
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain)){
$msg = ' domain part has two consecutive dots';
$isValid = false;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))){
$msg = ' character not valid in local part unless local part is quoted';
if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))){
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))){
$msg = ' domain <b>'.$domain.'</b> not found in DNS';
$isValid = false;
}
}
$this->error = $msg;
return $isValid;
}
}
?>
调用示例:
<?php
include "sendmail.class.php";
$Mail = new sendmail();
// Set congif
$Mail->SendMailVia = 'smtp'; // Send via smtp server or mail function
$Mail->smtp_host = 'mail.myhost.com';
$Mail->smtp_port = 25;
$Mail->smtp_user = 'user@myhost.com';
$Mail->smtp_password = 'mypassw';
// 例1 (mail from me)
if($Mail->Send('mymail1@mydomain.com; recipient2 name <recipientmail2@mydomain.com>,"recipient name" <recipient@mail.com>',
'My subject','My message here.'))
{
echo '邮件已发送!';
}
else
{
echo $Mail->error;
}
// 例2 (mail from me)
$Mail->mail_to = 'mymail1@mydomain.com; recipient2 name <recipientmail2@mydomain.com>,"recipient name" <recipient@mail.com>';
$Mail->subject = 'My subject';
$Mail->message = 'My message here';
if($Mail->Send())
{
echo '邮件已发送!';
}
else
{
echo $Mail->error;
}
// 例3 (mail from another user: example user2@site2.com)
$Mail->mail_to = 'Recipient Name <recipientmail@domain.com>';
$Mail->subject = 'My subject';
$Mail->message = 'My message here';
$Mail->from_name = 'User2 Name';
$Mail->SendFromMail = 'user2@site2.com';
if($Mail->Send())
{
echo 'message Mail send!';
}
else
{
echo $Mail->error;
}
?>
您可能感兴趣的文章:
php使用smtp发送邮件的实现代码
php smtp发送邮件的函数
php中通过curl smtp发送邮件的例子
php使用Pear的NetMail发送smtp邮件
使用pear:Net_SMTP类发送邮件的例子
linux下php配置smtp发送邮件的方法
php写的smtp邮件发送类
最新技术文章: