PHP 发送Email的几种方法
2013-11-06 14:16
405 查看
转载链接:http://blog.009it.com/php/75.html
在php中发送Email可以直接调用系统的mail()函数来完成,但是前提是你在php.ini文件中对mail都已经配置好了,以下为相关的配置信息:
Mail 配置选项
名称 默认 描述 可更改
SMTP<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
函数很简单,看似很好用,但是实际上局限性很明显,必须是在本地的开启smtp的情况下,而且经过测试成功率不是很高,所以程序中很少有人用到mail()函数来发邮件。
如果要扩展功能的话,还得重新构造,网上也有好多现成的类可以直接调用来实现,其实就是靠fsockopen来进行连接smtp,下面的代码是个思路:
在php中发送Email可以直接调用系统的mail()函数来完成,但是前提是你在php.ini文件中对mail都已经配置好了,以下为相关的配置信息:
; For Win32 only. SMTP = localhost smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters =
Mail 配置选项
名称 默认 描述 可更改
SMTP<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
函数很简单,看似很好用,但是实际上局限性很明显,必须是在本地的开启smtp的情况下,而且经过测试成功率不是很高,所以程序中很少有人用到mail()函数来发邮件。
如果要扩展功能的话,还得重新构造,网上也有好多现成的类可以直接调用来实现,其实就是靠fsockopen来进行连接smtp,下面的代码是个思路:
<?php
/**
名称:smtpsend . class . php功能:
邮件发送类, 实现邮件发送功能, 可以发送html邮件;可验证用户是否具有发送权限;可实现从任意服务器邮箱发送邮件,并非局限于本机。 $smtpserver;
SMTP服务器 $smtpserverport;
SMTP服务器端口 $smtpuser;
SMTP服务器的用户帐号 $smtppass;
SMTP服务器的用户密码 $fromMail;
SMTP服务器的用户邮箱 $toMail;
发送给谁 $subject;
邮件主题 $content;
邮件内容 $mailtype;
邮件格式(HTML / TXT), TXT为文本邮件
*/
class smtpclass {
function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) {
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 60; //is used in fsockopen()
$this->auth = $auth; //auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $returnpath = "", $cc = "", $bcc = "", $additional_headers = "") {
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header = "";
if (isset($returnpath) && $returnpath != "") {
$header.= "Reply-To:" . $returnpath . "\r\n";
}
$header.= "MIME-Version:1.0\r\n";
if ($mailtype == "HTML") {
$header.= 'Content-Type:text/html; charset=gb2312' . "\r\n";
}
$header.= "To: " . $to . "\r\n";
if ($cc != "") {
$header.= "Cc: " . $cc . "\r\n";
}
$header.= "From: $from<]
以上在网上看到的,其实具体的方法都是大同小异,用的很多的是一个开源的叫phpmailer,很方便的。用搜索引擎搜下然后下载就行。
这里贴出phpmailer的测试代码:<?php
require ("class.phpmailer.php");
require ("class.smtp.php");
//以下是实例:
$mail = new phpmailer(); //建立邮件发送类
$address = xxx@xxx . com;
$mail->IsSMTP(); // 使用SMTP方式发送
$mail->Host = "mail.xxx.com"; // 您的企业邮局域名
$mail->SMTPAuth = false; // 启用SMTP验证功能
$mail->Username = "xxx"; // 邮局用户名(请填写完整的email地址)
$mail->Password = "xxx"; // 邮局密码
$mail->From = "xxxx@xxxx.cn"; //邮件发送者email地址
$mail->FromName = "您的名称";
$mail->AddAddress("$address", ""); //收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
$mail->Subject = "PHPMailer测试邮件"; //邮件标题
$mail->Body = "Hello,这是测试邮件"; //邮件内容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略
if (!$mail->Send()) {
echo "邮件发送失败. <p>";
echo "错误原因: " . $mail->ErrorInfo;
exit;
}
echo "邮件发送成功";
?>[/code]
相关文章推荐
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- php发送get、post请求获取内容的几种方法
- php http发送get、post请求的几种方法
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法 以及获取内容
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- PHP中发送邮件的几种方法总结
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- php发送邮件方法-亲测可用,email.class.php过期解决办法
- php发送get、post请求的几种方法
- php发送get 4000 、post请求的几种方法
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- php发送get、post请求的几种方法
- PHP常用发送get、post请求的几种方法
- php版微信公众平台实现预约提交后发送email的方法