Jump to navigation

You are currently browsing all posts tagged with 'google'

使用php PHPMailer SMTP Gmail来发送邮件

  • Posted on September 3, 2010 at 11:42 am

原文地址:http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

为什么要使用Gmail来发送邮件呢?
首先它是免费的,每天大概可以发送500封邮件。
关于这个数量限制,可以来这里看看:http://mail.google.com/support/bin/answer.py?hl=en&answer=22839
其次您网站所在的服务器IP很有可能已经在垃圾邮件的黑名单里面了。

操作步骤:
1 注册Gmail帐号
2 下载最新版本的PHPMailer http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/
3 检查服务器防火墙是否允许你连接其它服务器的465端口
4 包含phpmailer的class:

require_once('phpmailer/class.phpmailer.php');

5 设置用户名密码:

define('GUSER', 'you@gmail.com'); // Gmail username
define('GPWD', 'password'); // Gmail password

6 发送邮件的函数:

function smtpmailer($to, $from, $from_name, $subject, $body) { 
	global $error;
	$mail = new PHPMailer();  // create a new object
	$mail->IsSMTP(); // enable SMTP
	$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
	$mail->SMTPAuth = true;  // authentication enabled
	$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
	$mail->Host = 'smtp.gmail.com';
	$mail->Port = 465; 
	$mail->Username = GUSER;  
	$mail->Password = GPWD;           
	$mail->SetFrom($from, $from_name);
	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->AddAddress($to);
	if(!$mail->Send()) {
		$error = 'Mail error: '.$mail->ErrorInfo; 
		return false;
	} else {
		$error = 'Message sent!';
		return true;
	}
}

另外为了保证邮件发送的成功率,最好是设置一下您域名的SPF记录。
各大网站都设置了呢。

163.com.                14836   IN      TXT     "v=spf1 include:spf.163.com -all"
qq.com.                 2614    IN      TXT     "v=spf1 ip4:119.147.10.0/23 ip4:119.147.18.0/24 ip4:222.202.96.0/24 ip4:64.71.138.0/25 ip4:58.251.149.0/24 ip4:119.147.6.0/24 ip4:119.147.8.0/24 ip4:112.90.137.0/22 ip4:119.147.14.0/24 ip4:183.60.2.0/24 ip4:113.108.77.0/24 ip4:183.62.126.0/24 ~all"
sina.com.               60      IN      TXT     "v=spf1 include:spf.sinamail.sina.com.cn -all"

关于SPF可以在这里看一下说明:

http://www.google.com/support/a/bin/answer.py?hl=en&answer=33786

使用php来压缩JavaScript代码

  • Posted on September 2, 2010 at 4:47 pm

原文地址:

http://www.sitepoint.com/blogs/2010/08/30/compress-javascript-closure-compiler-rest-api/

以及:

http://www.sitepoint.com/blogs/2010/08/31/compress-javascript-with-php/

随着ajax的盛行,我们网站要调用的JavaScript代码越来越多,
虽然客户端和服务端的带宽都在不断地提升,
PS:最近杭州电信有个说法是“光城市”,光纤覆盖的城市
但我们还是应该优化一下JavaScript代码。
一般来说,有三种优化的方式:
1 把

<script>

标签放到

</body>

之后,这样网站内容可以尽快显示出来。
2 把多个JS文件合并成一个。有效减少HTTP请求次数。
3 压缩JS文件,去掉其中的空格,空行,缩短变量名,以及其它的优化方式。

虽然网络上已经有N多的压缩软件,或者在线压缩程序,可以帮助我们压缩JS,
我经常使用的是GOOGLE提供的Closure Compiler在线压缩,
网址是: http://closure-compiler.appspot.com/home
但这通常是一个手工的过程。
对于大多数开发者来说,
在开发环境中,要修改未压缩的JS文件,
开发完毕之后,手工对其进行压缩,
再发布到生产环境中。

有没有自动的办法呢?
GOOGLE的Closure Compiler终于提供REST API了,可以用PHP来调用它了。
我们想做到:
1 压缩之后,生成错误报告
2 发布未压缩版本的JS文件到测试和生产环境中
3 测试人员和普通用户要访问到压缩之后的JS代码。
4 版本控制机制
5 自动进行压缩

假设我们的网站最后调用三个JS文件

<script src="script/file1.js"></script>  
<script src="script/file2.js"></script>  
<script src="script/file3.js"></script>

首先我们需要把这段代码替换成:

<script src="script/?file1&file2&file3"></script>

然后在script目录中创建一个index.php

<?php  
// fetch JavaScript files to compress  
// 得到JS文件名
$jsfiles = array_keys($_GET);  
$js = '';       // code to compress  
$jscomp = '';           // compressed JS  
$err = '';          // error string
// fetch JavaScript files  
// 读取所有的JS文件内容,并且合并到一起。
// 如果出错,就记录下文件名。
for ($i = 0, $j = count($jsfiles); $i < $j; $i++)   
{  
  $fn = $jsfiles[$i] . '.js';  
  $jscode = @file_get_contents($fn);  
  if ($jscode !== false) {  
    $js .= $jscode . "\n";  
  }  
  else {  
    $err .= $fn . '; ';  
  }  
}
if ($err != '') {  
  // error: missing files  
  // 如果出错的话,就返回如下的JS。提醒开发者。
  $jscomp = "alert('The following JavaScript files   
could not be read:\\n$err');";  
}

如果没有出错:
$apiArgs 数组存放的是Closure Compiler API Options。
更多options请见: http://code.google.com/closure/compiler/docs/api-ref.html

else if ($js != '') {  
  // REST API arguments  
  $apiArgs = array(  
    'compilation_level'=>'ADVANCED_OPTIMIZATIONS',   //优化度
    'output_format' => 'text',  		     
    'output_info' => 'compiled_code'  
   );  
	//$js 就是所有的JS代码的合体
   $args = 'js_code=' . urlencode($js);  
	//把它们拼到一起,一定很长。
   foreach ($apiArgs as $key => $value) {  
     $args .= '&' . $key .'='. urlencode($value);  
   }

接下来就可以去调用REST API了,还是继续用CURL吧:

// API call using cURL  
  $call = curl_init();  
  curl_setopt_array($call, array(  
    CURLOPT_URL =>   
'http://closure-compiler.appspot.com/compile',  
    CURLOPT_POST => 1,  
    CURLOPT_POSTFIELDS => $args,  
    CURLOPT_RETURNTRANSFER => 1,  
    CURLOPT_HEADER => 0,  
    CURLOPT_FOLLOWLOCATION => 0  
  ));  
	//$jscomp 就是返回的压缩之后的JS代码
  $jscomp = curl_exec($call);  
  curl_close($call);

最后输出:

} //end of if-else 
 
// output content   
header('Content-type: text/javascript');   
echo $jscomp;   
?>

完整代码下载地址:http://blogs.sitepointstatic.com/examples/tech/jscompress/php-js-compress.zip

但是这还没有完。
如果仅仅是这样, 用户每次访问的时候,都会去调用一下这个REST API,速度可想而知了。
后续的完善工作:
1 错误处理: 需要检查API调用的返回值,以及压缩过程中,Closure Compiler生成的错误报告。
2 服务端缓存: 压缩一次之后,写入缓存文件。
3 客户端缓存: 设置HTTP Expire headers。 这样客户端直接缓存住了。
4 js的URL后面加上版本号:

<script src="script/?file1&file2&file3=1.1.11"></script>

以便在JS更新之后,可以立即更新客户端缓存。

[图片]Hearing a Random Song in 1990 vs. Hearing a Random Song in 2010

  • Posted on August 30, 2010 at 11:13 am

不翻译。

或许用google搜索已经过时了,
百度不是已经有了新的音乐搜索方式了么:
您随便哼几句,它就能搜索出来!

google webfonts 网页字体在线服务启动

  • Posted on May 20, 2010 at 2:19 pm

google webfonts 网页字体在线服务启动

目前网页上常用字体有如下几种:’Lucida Grande’, Verdana, Arial, Sans-Serif。
中文字体常见的有宋体,雅黑等等。
要想使用比较小众的字体就麻烦了,万一用户的电脑没有安装这种字体,或者是浏览器不支持这种字体,就会严重破坏显示效果。即使是上面说的常用字体,在各个浏览器上面的表现也不是完全一样的。
一直以来,最常用的解决办法就是把文字制作成图片。

为了从根本上解决这个问题,google雇佣了一批很有天赋的字体设计师,
设计了很多款高质量的免费字体。并决定从2010年5月19日启动网页在线字体服务,暂时发布十八款字体,而且完全免费。 http://code.google.com/webfonts

The Google Font API provides a simple, cross-browser method for using any font in the Google Font Directory on your web page.
The fonts have all the advantages of normal text: in addition to being richer visually, text styled in web fonts is still searchable, scales crisply when zoomed, and is accessible to users using screen readers.

使用举例:

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">
    <style>
      body {
        font-family: 'Tangerine', serif;
        font-size: 48px;
      }
    </style>
  </head>
  <body>
    <h1>Making the Web Beautiful!</h1>
  </body>
</html>

效果图:

再修改一下CSS:

body {
  font-family: 'Tangerine', serif;
  font-size: 48px;
  text-shadow: 4px 4px 4px #aaa;
}

效果图:

不过,IE6对这个新玩意不太感冒。失败!

google站内搜索代码

  • Posted on March 5, 2010 at 11:58 am

试用了一下google自定义搜索,功能确实很多,可以投放自己的adsense,还可以嵌入google Analytics 统计代码,还可以自定义搜索结果页面的外观,还可以设置某些搜索结果置顶显示,但是有二个缺点不得不提:

1  搜索框的外观实在是丑得可以了。我就不贴图了,愿意感受一下的同学们自己去试用一下吧。

2 忘记了。。。

最近记忆力出问题了, 很多东西转瞬即忘。

于是,不得不换一种形式的站内搜索代码了:

37 <!–google–>
38 <div style=”float:left;margin-left:20px;margin-top:10px !important; margin-top:5px;”>
39 <form action=”http://www.google.cn/search” method=”get” target=_blank>
40 <input name=”sitesearch” value=”masalife.com” type=”hidden”>
41 <input name=”hl” value=”zh-CN” type=”hidden”>
42 <input name=”ie” value=”GB2312″ type=”hidden”>
43 <input size=”25″ name=”q” id=”query” type=”text” >
44 <input name=”Search” value=”Google搜索” attr=”value” type=”submit”><br>
45 <input name=s onClick=”this.form.sitesearch.value=”;” type=radio> 互联网
46 <input name=s onclick=”this.form.sitesearch.value=’www.masalife.com’” type=radio checked> masalife.com
47 </form>
48 </div>
49 <!–google–>

备注一下:

<input name=”ie” value=”GB2312″ type=”hidden”>  根据此搜索框被嵌入页面的编码来修改。

再附一下百度站内搜索代码  http://www.baidu.com/search/freecode.html

邪恶的google 还是 邪恶的baidu

  • Posted on January 13, 2010 at 2:30 pm

什么也不说了,上图吧,有图有真相:

baidu_google

Top