php mail发送邮件代码

 更新时间:2016年11月25日 17:01  点击:1395
文章提供一款简单的php自带的邮件发送函数来发送电子邮件,功能非常简单,只要简单的带参数就好,但是必须配置好服务器的mail功能才可以。

?>

 代码如下 复制代码

<html>
<head>
<title>发信给网管</title>
</head>
<body>
<h2 align="center">网管收信</h2><br>
<hr><br>
<center>
<form action="mail.php">
发件人:<input type="text" name="from" size=25><br>
主题:<input type="text" name="subject" size=20><br>
内容:
<textarea name="content" cols=80 rows=15>你好,站长:</textarea><br>
<input type="submit" value="寄出"> <input type="reset" value="重写"><br>
</form>
</body></html>


mail.php文件代码

<?php
if (empty($from) or empty($subject) or empty($content)) {
 echo "没有完成填写,请<a href='mail.html'>返回</a>";
}
$body="[主题] $subjectn";
$body.="[发件人] $fromn";
$body.=$content;
$deal=mail("guoanyuan@eyou.com",$subject,$body,"from:$from");
if ($deal) {echo "寄件成功!";}else{echo "寄件失败!!!";}
?>

关于php mail函数

mail 简介
http 函数允许您从脚本中直接发送电子邮件。
需求
要使邮件函数可用,php 需要已安装且正在运行的邮件系统。要使用的程序是由 php.ini 文件中的配置设置定义的。
安装
邮件函数是 php 核心的组成部分。无需安装即可使用这些函数。
运行时配置
邮件函数的行为受 php.ini 的影响。

mail 配置选项
名称 默认 描述 可更改
smtp "localhost" windows 专用:smtp 服务器的 dns 名称或 ip 地址。 php_ini_all
smtp_port "25" windows 专用:smtp 段口号。自 php 4.3 起可用。 php_ini_all
sendmail_from null windows 专用:规定从 php 发送的邮件中使用的 "from" 地址。 php_ini_all
sendmail_path null unix 系统专用:规定sendmail 程序的路径(通常 /usr/sbin/sendmail 或 /usr/lib/sendmail)

 

 代码如下 复制代码

function smtp_mail ($sendto_email,$subject,$body) {
global $mailconfig,$_cfg;
$mail = new phpmailer(); 
$mail->issmtp();
$mail->host = $mailconfig['smtps教程ervers'];//smtp servers 
$mail->smtpauth = true;// 启用smtp验证功能
$mail->username = $mailconfig['smtpusername'];
$mail->password = $mailconfig['smtppassword'];
$mail->from =$mailconfig['smtpfrom'];
$mail->fromname =$_cfg['site_name'];
$mail->charset = "gb2312";
$mail->encoding = "base64";
$mail->addaddress($sendto_email,"");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是addaddress("收件人email","收件人姓名")
$mail->addreplyto($mailconfig['addreplyto'],"");//增加回复标签addreplyto
$mail->ishtml(true);
$mail->subject = $subject;
$mail->body =$body;
$mail->altbody ="text/html";//该属性的设置是在邮件正文不支持html的备用显示
if(!$mail->send()) 
{
return false;
//echo "邮件发送有误 <p>"; 
//echo "邮件错误信息: " . $mail->errorinfo;

else {
return true;
}
}
方法二

<?php
class email {
//---设置全局变量
var $mailto = ""; // 收件人
var $mailcc = ""; // 抄送
var $mailbcc = ""; // 秘密抄送
var $mailfrom = ""; // 发件人
var $mailsubject = ""; // 主题
var $mailtext = ""; // 文本格式的信件主体
var $mailhtml = ""; // html格式的信件主体
var $mailattachments = ""; // 附件
/* 函数setto($inaddress) :用于处理邮件的地址 参数 $inaddress
为包涵一个或多个字串,email地址变量,使用逗号来分割多个邮件地址
默认返回值为true
**********************************************************/
function setto($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckemail($addressarray[$i])==false) return false; }
//--所有合法的email地址存入数组中
$this->mailto = implode($addressarray, ",");
return true; }
/**************************************************
函数 setcc($inaddress) 设置抄送人邮件地址
参数 $inaddress 为包涵一个或多个邮件地址的字串,email地址变量,
使用逗号来分割多个邮件地址 默认返回值为true
**************************************************************/
function setcc($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckemail($addressarray[$i])==false) return false; }
//--所有合法的email地址存入数组中
$this->mailcc = implode($addressarray, ",");
return true; }
/***************************************************
函数setbcc($inaddress) 设置秘密抄送地址 参数 $inaddress 为包涵一个或多
个邮件地址的字串,email地址变量,使用逗号来分割多个邮件地址 默认返回值为
true
******************************************/
function setbcc($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$i<count($addressarray);$i++)
{ if($this->checkemail($addressarray[$i])==false)
return false;
}
//--所有合法的email地址存入数组中
$this->mailbcc = implode($addressarray, ",");
return true;
}
/*****************************************************************
函数setfrom($inaddress):设置发件人地址 参数 $inaddress 为包涵邮件
地址的字串默认返回值为true
***************************************/
function setfrom($inaddress){
if($this->checkemail($inaddress)){
$this->mailfrom = $inaddress;
return true;
} return false; }
/**********************
函数 setsubject($insubject) 用于设置邮件主题参数$insubject为字串,
默认返回的是true
*******************************************/
function setsubject($insubject){
if(strlen(trim($insubject)) > 0){
$this->mailsubject = ereg_replace( "n", "",$insubject);
return true; }
return false; }
/****************************************************
函数settext($intext) 设置文本格式的邮件主体参数 $intext 为文本内容默
认返回值为true
****************************************/
function settext($intext){
if(strlen(trim($intext)) > 0){
$this->mailtext = $intext;
return true; }
return false;
}
/**********************************
函数sethtml($inhtml) 设置html格式的邮件主体参数$inhtml为html格式,
默认返回值为true
************************************/
function sethtml($inhtml){
if(strlen(trim($inhtml)) > 0){
$this->mailhtml = $inhtml;
return true; }
return false; }
/**********************
函数 setattachments($inattachments) 设置邮件的附件 参数$inattachments
为一个包涵目录的字串,也可以包涵多个文件用逗号进行分割 默认返回值为true
*******************************************/
function setattachments($inattachments){
if(strlen(trim($inattachments)) > 0){
$this->mailattachments = $inattachments;
return true; }
return false; }
/*********************************
函数 checkemail($inaddress) :这个函数我们前面已经调用过了,主要就是
用于检查email地址的合法性
*****************************************/
function checkemail($inaddress){
return (ereg( "^[^@ ]+@([a-za-z0-9-]+.)+([a-za-z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inaddress));
}
/*************************************************
函数loadtemplate($infilelocation,$inhash,$informat) 读取临时文件并且
替换无用的信息参数$infilelocation用于定位文件的目录
$inhash 由于存储临时的值 $informat 由于放置邮件主体
***********************************************************/
function loadtemplate($infilelocation,$inhash,$informat){
/* 比如邮件内有如下内容: dear ~!username~,
your address is ~!useraddress~ */
//--其中"~!"为起始标志"~"为结束标志
$templatedelim = "~";
$templatenamestart = "!";
//--找出这些地方并把他们替换掉
$templatelineout = ""; //--打开临时文件
if($templatefile = fopen($infilelocation, "r")){
while(!feof($templatefile)){
$templateline = fgets($templatefile,1000);
$templatelinearray = explode($templatedelim,$templateline);
for( $i=0; $i<count($templatelinearray);$i++){
//--寻找起始位置
if(strcspn($templatelinearray[$i],$templatenamestart)==0){
//--替换相应的值
$hashname = substr($templatelinearray[$i],1);
//--替换相应的值
$templatelinearray[$i] = ereg_replace($hashname,(string)$inhash[$hashname],$hashname);
}
}
//--输出字符数组并叠加
$templatelineout .= implode($templatelinearray, "");
} //--关闭文件fclose($templatefile);
//--设置主体格式(文本或html)
if( strtoupper($informat)== "text" )
return($this->settext($templatelineout));
else if( strtoupper($informat)== "html" )
return($this->sethtml($templatelineout));
} return false;
}
/*****************************************
函数 getrandomboundary($offset) 返回一个随机的边界值
参数 $offset 为整数 – 用于多管道的调用 返回一个md5()编码的字串
****************************************/
function getrandomboundary($offset = 0){
//--随机数生成
srand(time()+$offset);
//--返回 md5 编码的32位 字符长度的字串
return ( "----".(md5(rand()))); }
/********************************************
函数: getcontenttype($infilename)用于判断附件的类型
**********************************************/
function getcontenttype($infilename){
//--去除路径
$infilename = basename($infilename);
//--去除没有扩展名的文件
if(strrchr($infilename, ".") == false){
return "application/octet-stream";
}
//--提区扩展名并进行判断
$extension = strrchr($infilename, ".");
switch($extension){
case ".gif": return "image/gif";
case ".gz": return "application/x-gzip";
case ".htm": return "text/html";
case ".html": return "text/html";
case ".jpg": return "image/jpeg";
case ".tar": return "application/x-tar";
case ".txt": return "text/plain";
case ".zip": return "application/zip";
default: return "application/octet-stream";
}
return "application/octet-stream";
}
/**********************************************
函数formattextheader把文本内容加上text的文件头
*****************************************************/
function formattextheader(){ $outtextheader = "";
$outtextheader .= "content-type: text/plain;
charset=us-asciin";
$outtextheader .= "content-transfer-encoding: 7bitnn";
$outtextheader .= $this->mailtext. "n";
return $outtextheader;
} /************************************************
函数formathtmlheader()把邮件主体内容加上html的文件头
******************************************/
function formathtmlheader(){
$outhtmlheader = "";
$outhtmlheader .= "content-type: text/html;
charset=us-asciin";
$outhtmlheader .= "content-transfer-encoding: 7bitnn";
$outhtmlheader .= $this->mailhtml. "n";
return $outhtmlheader;
}
/**********************************
函数 formatattachmentheader($infilelocation) 把邮件中的附件标识出来
********************************/
function formatattachmentheader($infilelocation){
$outattachmentheader = "";
//--用上面的函数getcontenttype($infilelocation)得出附件类型
$contenttype = $this->getcontenttype($infilelocation);
//--如果附件是文本型则用标准的7位编码
if(ereg( "text",$contenttype)){
$outattachmentheader .= "content-type: ".$contenttype. ";n";
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. "n";
$outattachmentheader .= "content-transfer-encoding: 7bitn";
$outattachmentheader .= "content-disposition: attachment;n";
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. "nn";
$textfile = fopen($infilelocation, "r");
while(!feof($textfile)){
$outattachmentheader .= fgets($textfile,1000);
}
//--关闭文件 fclose($textfile);
$outattachmentheader .= "n";
}
//--非文本格式则用64位进行编码
else{ $outattachmentheader .= "content-type: ".$contenttype. ";n";
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. "n";
$outattachmentheader .= "content-transfer-encoding: base64n";
$outattachmentheader .= "content-disposition: attachment;n";
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. "nn";
//--调用外部命令uuencode进行编码
exec( "uuencode -m $infilelocation nothing_out",$returnarray);
for ($i = 1; $i<(count($returnarray)); $i++){
$outattachmentheader .= $returnarray[$i]. "n";
}
} return $outattachmentheader;
}
/******************************
函数 send()用于发送邮件,发送成功返回值为true
************************************/
function send(){
//--设置邮件头为空
$mailheader = "";
//--添加抄送人
if($this->mailcc != "")
$mailheader .= "cc: ".$this->mailcc. "n";
//--添加秘密抄送人
if($this->mailbcc != "")
$mailheader .= "bcc: ".$this->mailbcc. "n";
//--添加发件人
if($this->mailfrom != "")
$mailheader .= "from: ".$this->mailfrom. "n";
//---------------------------邮件格式------------------------------
//--文本格式
if($this->mailtext != "" && $this->mailhtml == "" && $this->mailattachments == ""){
return mail($this->mailto,$this->mailsubject,$this->mailtext,$mailheader);
}
//--html或text格式
else if($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments == ""){
$bodyboundary = $this->getrandomboundary();
$textheader = $this->formattextheader();
$htmlheader = $this->formathtmlheader();
//--设置 mime-版本
$mailheader .= "mime-version: 1.0n";
$mailheader .= "content-type: multipart/alternative;n";
$mailheader .= ' boundary="'.$bodyboundary. '"';
$mailheader .= "nnn";
//--添加邮件主体和边界
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $textheader;
$mailheader .= "--".$bodyboundary. "n";
//--添加html标签
$mailheader .= $htmlheader;
$mailheader .= "n--".$bodyboundary. "--";
//--发送邮件
return mail($this->mailto,$this->mailsubject, "",$mailheader);
}
//--文本加html加附件
else if($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments != ""){
$attachmentboundary = $this->getrandomboundary();
$mailheader .= "content-type: multipart/mixed;n";
$mailheader .= ' boundary="'.$attachmentboundary. '"'. "nn";
$mailheader .= "this is a multi-part message in mime format.n";
$mailheader .= "--".$attachmentboundary. "n";
$bodyboundary = $this->getrandomboundary(1);
$textheader = $this->formattextheader();
$htmlheader = $this->formathtmlheader();
$mailheader .= "mime-version: 1.0n";
$mailheader .= "content-type: multipart/alternative;n";
$mailheader .= ' boundary="'.$bodyboundary. '"';
$mailheader .= "nnn";
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $textheader;
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $htmlheader;
$mailheader .= "n--".$bodyboundary. "--";
//--获取附件值
$attachmentarray = explode( ",",$this->mailattachments);
//--根据附件的个数进行循环
for($i=0;$i<count($attachmentarray);$i++){
//--分割 $mailheader .= "n--".$attachmentboundary. "n";
//--附件信息
$mailheader .= $this->formatattachmentheader($attachmentarray[$i]);
}
$mailheader .= "--".$attachmentboundary. "--";
return mail($this->mailto,$this->mailsubject, "",$mailheader);
}
return false;
}
}
?>


使用方法:
<?

 代码如下 复制代码

include "email.class"

$mail->setto("a@a.com"); //收件人
$mail-> setcc("b@b.com,[url=mailto:c@c.com]c@c.com[/url]"); //抄送
$mail-> setcc("d@b.com,[url=mailto:e@c.com]e@c.com[/url]"); //秘密抄送
$mail->setfrom("[url=mailto:f@f.com]f@f.com[/url]");//发件人
$mail->setsubject("主题") ; //主题
$mail->settext("文本格式") ;//发送文本格式也可以是变量
$mail->sethtml("html格式") ;//发送html格式也可以是变量
$mail->setattachments("c:a.jpg") ;//添加附件,需表明路径
$mail->send(); //发送邮件

?>

这款php 邮件发送类代码是一款用户注册这后忘了密码然后自动找回密码时,会自动发送一封邮件到用户邮箱,并有用户名与密码。
 代码如下 复制代码

class smtp
{
/* public variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* private variables */
var $sock;

/* constractor */
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 = 30; //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, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|( ))(\.)", "\1.\3", $body);
$header .= "mime-version:1.0 ";
if($mailtype=="html"){
$header .= "content-type:text/html ";
}
$header .= "to: ".$to." ";
if ($cc != "") {
$header .= "cc: ".$cc." ";
}
$header .= "from: $from<".$from."> ";
$header .= "subject: ".$subject." ";
$header .= $additional_headers;
$header .= "date: ".date("r")." ";
$header .= "x-mailer:by redhat (php/".phpversion().") ";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from."> ";
$to = explode(",", $this->strip_comment($to));

if ($cc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($bcc)));
}

$sent = true;
foreach ($to as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("error: cannot send email to ".$rcpt_to." ");
$sent = false;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("e-mail has been sent to <".$rcpt_to."> ");
} else {
$this->log_write("error: cannot send email to <".$rcpt_to."> ");
$sent = false;
}
fclose($this->sock);
$this->log_write("disconnected from remote host ");
}
echo "<br>";
//echo $header;
return $sent;
}

/* private functions */

function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("helo", $helo)) {
return $this->smtp_error("sending helo command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("auth login", base64_encode($this->user))) {
return $this->smtp_error("sending helo command");
}

if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending helo command");
}
}
#
if (!$this->smtp_putcmd("mail", "from:<".$from.">")) {
return $this->smtp_error("sending mail from command");
}

if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) {
return $this->smtp_error("sending rcpt to command");
}

if (!$this->smtp_putcmd("data")) {
return $this->smtp_error("sending data command");
}

if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}

if (!$this->smtp_eom()) {
return $this->smtp_error("sending <cr><lf>.<cr><lf> [eom]");
}

if (!$this->smtp_putcmd("quit")) {
return $this->smtp_error("sending quit command");
}

return true;
}

function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}

function smtp_sockopen_relay()
{
$this->log_write("trying to ".$this->relay_host.":".$this->smtp_port." ");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("error: cannot connenct to relay host ".$this->relay_host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
return false;
}
$this->log_write("connected to relay host ".$this->relay_host." ");
return true;;
}

function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $mxhosts)) {
$this->log_write("error: cannot resolve mx "".$domain."" ");
return false;
}
foreach ($mxhosts as $host) {
$this->log_write("trying to ".$host.":".$this->smtp_port." ");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("warning: cannot connect to mx host ".$host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
continue;
}
$this->log_write("connected to mx host ".$host." ");
return true;
}
$this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).") ");
return false;
}

function smtp_message($header, $body)
{
fputs($this->sock, $header." ".$body);
$this->smtp_debug("> ".str_replace(" ", " "."> ", $header." > ".$body." > "));

return true;
}

function smtp_eom()
{
fputs($this->sock, " . ");
$this->smtp_debug(". [eom] ");

return $this->smtp_ok();
}

function smtp_ok()
{
$response = str_replace(" ", "", fgets($this->sock, 512));
$this->smtp_debug($response." ");

if (!ereg("^[23]", $response)) {
fputs($this->sock, "quit ");
fgets($this->sock, 512);
$this->log_write("error: remote host returned "".$response."" ");
return false;
}
return true;
}

function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}

fputs($this->sock, $cmd." ");
$this->smtp_debug("> ".$cmd." ");

return $this->smtp_ok();
}

function smtp_error($string)
{
$this->log_write("error: error occurred while ".$string.". ");
return false;
}

function log_write($message)
{
$this->smtp_debug($message);

if ($this->log_file == "") {
return true;
}

$message = date("m d h:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("warning: cannot open log file "".$this->log_file."" ");
return false;
}
flock($fp, lock_ex);
fputs($fp, $message);
fclose($fp);

return true;
}

function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}

return $address;
}

function get_address($address)
{
$address = ereg_replace("([ ])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);

return $address;
}

function smtp_debug($message)
{
if ($this->debug) {
echo $message."<br>";
}
}

function get_attach_type($image_tag) { //

$filedata = array();

$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=addslashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);

$filedata['context'] = $image_data;
$filedata['filename']= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
switch($extension){
case ".gif":
$filedata['type'] = "image/gif";
break;
case ".gz":
$filedata['type'] = "application/x-gzip";
break;
case ".htm":
$filedata['type'] = "text/html";
break;
case ".html":
$filedata['type'] = "text/html";
break;
case ".jpg":
$filedata['type'] = "image/jpeg";
break;
case ".tar":
$filedata['type'] = "application/x-tar";
break;
case ".txt":
$filedata['type'] = "text/plain";
break;
case ".zip":
$filedata['type'] = "application/zip";
break;
default:
$filedata['type'] = "application/octet-stream";
break;
}


return $filedata;
}

}

//使用方法

 代码如下 复制代码
$smtps教程erver = "smtp.163.com";//smtp服务器
$smtpserverport =25;//smtp服务器端口
$smtpusermail = "请输入自己的网易邮箱账户";//smtp服务器的用户邮箱
$smtpemailto = $rs['email'];//发送给谁
$smtpuser = "请输入自己的网易邮箱账户名";//smtp服务器的用户帐号
$smtppass = "请输入自己的网易邮箱密码";//smtp服务器的用户密码
$mailsubject = "设置新密码";//邮件主题
$mailbody = "{$rs['username']}您好!这里是powered by 您的临时新密码是: $emailpwd" .
   " 请点击下面的网址,进入设置新密码页面,完成新密码的设置。" .
   "<a href='http://www.111cn.net'>设置新密码</a>";//邮件内容
$mailtype = "html";//邮件格式(html/txt),txt为文本邮件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
$prompt_msg->p('系统已经邮件发出,请注意查收!','index.php','返回首页');

?>

这是一款利用php 自带的函数mail进行邮件发送,同时还支持附件的发送哦。

(可带附件<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"

 代码如下 复制代码

"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php mail函数发送电子邮件(可带附件)</title>
</head>

<body>
<?php
 // you might need to change this line, if you do not use
 // the default ports, 80 for normal traffic and 443 for ssl
 if($_server['server_port']!=443)
   echo '<p><font color = red>
            warning: you have not connected to this page using ssl. 
            your message could be read by others.</font></p>';
?>

<form method = post action = "send_private_mail.php"><br>
your email address:<br>
<input type = text name = from size = 38><br>
subject:<br>
<input type = text name = title size = 38><br>
your message:<br>
<textarea name = body cols = 30 rows = 10>
</textarea><br>
<input type = submit value = "send!">
</form>
</body>
</html>

send_private_mail.php文件

 代码如下 复制代码

<?php
  //create short variable names
  $from = $_post['from'];
  $title = $_post['title'];
  $body = $_post['body'];

  $to_email = 'luke@localhost';

  // tell gpg where to find the key ring
  // on this system, user nobody's home directory is /tmp/
  putenv('gnupghome=/tmp/.gnupg');
 
  //create a unique file name
  $infile = tempnam('', 'pgp'); 
  $outfile = $infile.'.asc';

  //write the user's text to the file
  $fp = fopen($infile, 'w');
  fwrite($fp, $body);
  fclose($fp);

  //set up our command
  $command =  "/usr/local/bin/gpg -a \
               --recipient 'luke welling <luke@tangledweb.com.au>' \
               --encrypt -o $outfile $infile";

  // execute our gpg command
  system($command, $result);

  //delete the unencrypted temp file
  unlink($infile);

  if($result==0)
  {
    $fp = fopen($outfile, 'r');   
    if(!$fp||filesize ($outfile)==0)
    {
      $result = -1;
    }
    else
    {
      //read the encrypted file
      $contents = fread ($fp, filesize ($outfile));
      //delete the encrypted temp file
      unlink($outfile);     

      mail($to_email, $title, $contents, "from: $from ");
      echo '<h1>message sent</h1>
            <p>your message was encrypted and sent.
            <p>thank you.';
    }
  }

  if($result!=0)
  {
    echo '<h1>error:</h1>
          <p>your message could not be encrypted, so has not been sent.
          <p>sorry.';
  }
 

/*
 
  */
?>)

本文章不光是是一款利用php发送邮件类代码与是一款实用的php邮件发送代码,如果你是初学者,可以拿当作学习同时也可以使用,如果是你开发者,这款邮件类完全可以使用哦。
 代码如下 复制代码

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>使用php发送电子邮件</title>
<style type="text/css教程">
<!--
.style1 {font-size: 12px}
.style2 {
 font-size: 24px;
 font-weight: bold;
}
-->
</style>
</head>

<body>
<p align="center" class="style2">使用php发电子邮件</p>
<form name="form1" method="post" action="send_mail.php">
<table width="444" height="347" border="0" align="center">
    <tr>
        <td width="71" height="23" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;收件人</div>
        </div>
        </td>
        <td width="363">
            <label>&nbsp;<input type="text" name="sendto"></label>
        </td>
    </tr>
    <tr>
        <td height="27" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;邮件标题</div>
        </div></td>
        <td>
            <label>&nbsp;<input type="text" name="subject"></label>
        </td>
    </tr>
    <tr>
        <td height="23" colspan="2" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;邮件正文</div>
        </div>
        </td>
    </tr>
    <tr>
        <td colspan="2" bgcolor="#d6b1e9">
        <div align="right">www.111cn.net
            <label>
            <div align="left">
                <textarea name="emailcontent" cols="60" rows="18"></textarea>
            </div>
            </label>
        </div>
          <div align="right"></div>
        <div align="right"></div>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <label><input type="submit" name="submit" value="提交"></label>
        </td>
    </tr>
</table>
</form>

</body>
</html>

 

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30