---
title: "用PHPMailer发送邮件"
author: "muyu"
date: "2009-04-29T14:27:38Z"
tags: ["Gmail", "Linux", "PHP", "WordPress"]
categories: ["编程语言"]
slug: "phpmailer"
draft: false
lastmod: "2009-04-29T14:28:29Z"
id: 376650
url: "https://muyu.iteye.com/blog/376650"
---

php中的mail函数使用起来虽然简单，配置却较为复杂。安装一个SMTP着实不易。在linux下，sendmail好像也不靠谱，今天163能收到信件，gmail收不到；明天163收不到了，gmail又可以收邮件了。

还是用PHPMailer吧，wordpress也用PHPMailer！看看它的代码：

 

```
	global $phpmailer;

	// (Re)create it, if it's gone missing
	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		require_once ABSPATH . WPINC . '/class-smtp.php';
		$phpmailer = new PHPMailer();
	}
```

 

在PHPMailer README的 Installation 一节：

Copy class.phpmailer.php into your php.ini include\_path. If you are  
using the SMTP mailer then place class.smtp.php in your path as well.

 

而从wordpress的代码看，它直接用require\_once来达到此目的，这样省去了配置php.ini的麻烦，有利于代码部署。

 

其他代码我就不贴了，README里的例子非常清晰了~