php mail 发送邮件

 更新时间:2016年11月25日 17:01  点击:1883
mail ( )函数可让您发送电子邮件直接从脚本,讲法mail(to,subject,message,headers,parameters)

定义和用法
在mail ( )函数可让您发送电子邮件直接从脚本。

此函数返回TRUE电子邮件是否已成功接受交付,否则返回FALSE 。

语法

mail(to,subject,message,headers,parameters)
to收信人
subject标题
message

内容
<?php
$txt = str_replace("n.", "n..", $txt);
?>

headers
任择。指定额外的标题一样,从,副本,并密件副本。额外的标题应该是分开的CRLF ( r n )段。注意:当发送电子邮件,它必须包含一个从头。这可以设置此参数或在php.ini文件。
parameters
任择。指定一个额外的参数邮件程序(一个界定的sendmail_path配置设置) 。 (即本可用来设置信封发件人地址邮件时使用的口邮件选项)

提示和说明
注意:请记住,仅仅因为电子邮件被接受交付,但这并不意味着电子邮件实际上是发送和接收。




范例1
发送一个简单的电子邮件:

 

$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("somebody@example.com","My subject",$txt);
 
好了下面我们来看看php发送邮件函数mail 教程,如果要使用mail函数能够正常使用我们还得配置php.ini里面的参数哦。

<?php
//定义边界线
$boundary = uniqid( "" );
//生成邮件头
$header = "From: $from Content-type: multipart/mixed;
boundary="$boundary" X-Mailer:PHP X-Priority:3";
//获取附件文件的MIME类型
$mimetype = mime_content_type('test.zip')
//获取附件文件的名字
$attach = 'test.zip'
//对附件文件进行编码和切分
$fp = fopen($attach, "r");
$content = fread($fp, filesize($attach));
$content = chunk_split( base64_encode($content) );
//生成邮件主体
$body ="
--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit
$message
--$boundary
Content-Type: $mimeType; name=$filename
Content-Disposition: attachment; filename=$filename
Content-Transfer-Encoding: base64
$content
--$boundary--";
//发送邮件
mail( $to, $subject, $body, $header );
?>

转载请注明:www.111cn.net

phpmailer多邮件发送

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "list@mydomain.com";
$mail->FromName = "List manager";
$mail->Host     = "smtp1.site.com;smtp2.site.com";
$mail->Mailer   = "smtp";

@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result)) {
    // HTML body
    $body  = "Hello <font size="4">" . $row["full_name"] . "</font>, <p>";
    $body .= "<i>Your</i> personal photograph to this message.<p>";
    $body .= "Sincerely, <br>";
    $body .= "PHPMailer List manager";

    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $row["full_name"] . ", ";
    $text_body .= "Your personal photograph to this message. ";
    $text_body .= "Sincerely, ";
    $text_body .= "PHPMailer List manager";

    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->AddAddress($row["email"], $row["full_name"]);
    $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

    if(!$mail->Send())
        echo "There has been a mail error sending to " . $row["email"] . "<br>";

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();
}
   

Mailer Error: Language string failed to load: from_failedmailangel123@163.com,

今天在写给会员定时发邮件[phpmailer]时突然出现这种问题,经过仔细发查看发现是用户名与密码不正确造成的.下面为正确实可以发送邮箱的函数.

nclude_once("../pmail/class.phpmailer.php");
smtp_mail();

function smtp_mail () {
$mail    = new PHPMailer(); 
$mail->IsSMTP();                  // send via SMTP 
$mail->Host  = "smtp.163.com";   // SMTP servers 
$mail->SMTPAuth = true;           // turn on SMTP authentication 
$mail->Username = "mailangel123";     // SMTP username     注意:普通邮件认证不需要加 @域名
$mail->Password = "*******";          // SMTP password 
$mail->From  = "mailangel123@163.com";        // 发件人邮箱
$mail->FromName =  "您好,你很久没来这里了";    // 发件人
$mail->CharSet  = "GB2312";              // 这里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress("mailangel123@163.com","test");    // 收件人邮箱和姓名
$mail->AddReplyTo("mailangel123@163.com","163.com");
$mail->IsHTML(true);    // send as HTML            
$mail->Subject  = '您好,你很久没来这里了!'; // 邮件主题
$mail->Body = "test content";// 邮件内容
$mail->AltBody ="text/html"; 
$mail->Send();
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
}

以mailangel改成你们自己的邮箱吧.


<?php 
// 请求 PHPmailer类 文件
require_once("class.phpmailer.php"
);  
//发送Email函数
function smtp_mail $sendto_email$subject$body$extra_hdrs$user_name
) { 
$mail = new PHPMailer
();  
$mail->IsSMTP();                                     
// send via SMTP  
$mail->Host "smtp.163.com";                       
// SMTP servers  
$mail->SMTPAuth true;                             
// turn on SMTP authentication  
$mail->Username "cfc4n";                          
// SMTP username     注意:普通邮件认证不需要加 @域名 
$mail->Password "123456";                         
// SMTP password  
$mail->From "cfc4n@163.com";                      
// 发件人邮箱 
$mail->FromName "中国资金管理网";                 
//   发件人 ,比如 中国资金管理网
$mail->CharSet "GB2312";                          
// 这里指定字符集! 
$mail->Encoding "base64"
;  
$mail->AddAddress($sendto_email,$user_name);        
// 收件人邮箱和姓名 
$mail->AddReplyTo("treasurer@treasurer.org.cn","中国资金管理网"
);  
//$mail->WordWrap = 50; // set word wrap  
//$mail->AddAttachment("/var/tmp/file.tar.gz");                                                    // attachment  附件1
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");                                         //附件2
$mail->IsHTML(true);                               
// send as HTML  
$mail->Subject $subject
;                        
// 邮件内容      可以直接发送html文件
$mail->Body 
= <<<EOT
<html><head> 
<meta http-equiv="Content-Language" c> 
<meta http-equiv="Content-Type" c>
<body>
{$user_name}
你好,欢迎您注册本站用户!
</body>
</html>
EOT;
$mail->AltBody ="text/html"
;  
if(
$mail->Send
())  
        {  
   
info_write("ok.txt","$user_name发送成功"
);
        }  
else { 
   
info_write("falied.txt","$user_name失败,错误信息$mail->ErrorInfo"
);
     } 

// 发送Email函数结束
// 写入发送结果函数
function info_write($filename,$info_log
)
    {
  
$info.= $info_log
;
  
$info.="\r\n"
;
  
$fp fopen ($filename,a
);
  
fwrite($fp,$info
);
  
fclose($fp
);
}
//定时跳转页面 函数  其中 1000是时间,1秒, 您可以自定义
function redirect($url
)
    {
         echo 
"<script>
    function redirect() {
     window.location.replace('$url');
    }
    window.setTimeout('redirect();', 15000);
     </script>"
;
    }
//读取文本 邮件地址  您也可以读 数据库
        
$filename "email.txt"
;
        
$fp fopen($filename,"r"
);
        
$contents fread($fp,filesize($filename
));
        
$num_email=explode("\r\n",$contents
);         
  
$len=count($num_email
);
        
fclose($fp
);
// 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)
$i $_GET['action'
];
        
$i
++;  
if (
$i<$len
)
    {
  
$rs=explode("@",$num_email[$i
]);
  
$user_name $rs['0'
];
  echo 
"正在发送第{$i}封({$num_email[$i]})邮件......"
;
        
smtp_mail($num_email[$i], 'Treasury Online周刊第十期'$body'http://www.treasurer.org.cn/'$user_name
);
        
redirect("?action=$i"
);
    }
else {
     echo 
"邮件全部发送完毕"
;
  exit;
      }
?>

[!--infotagslink--]

相关文章

  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php邮件发送的两种方式

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

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
  • php邮件发送的两种方式

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

    这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
  • c#使用netmail方式发送邮件示例

    这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • PHPMailer在SAE上无法发送邮件的解决方法

    PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
  • 整理几个android后台发送邮件的方法

    本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • 网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2

    Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25
  • node.js 基于 STMP 协议和 EWS 协议发送邮件

    这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15
  • Delphi7中群发Email邮件的方法

    这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下...2020-06-30
  • PHP利用Jmail组件实现发送邮件

    学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • C#编程实现发送邮件的方法(可添加附件)

    这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php中利用curl smtp发送邮件实例

    本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
  • Python基于httpx模块实现发送请求

    这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
  • php定时发送邮件

    <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25