phpmailer 发送邮件实例代码
header("Content-type:text/html;charset=utf-8");
include('phpmailer/class.phpmailer.php');
include('phpmailer/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.sohu.com"; // SMTP服务器
$mail->Port = 25;
$mail->SMTPAuth = true; // SMTP认证?
$mail->Username = "yourmail@sohu.com"; // 用户名
$mail->Password = "yourmail168"; // 密码
$mail->From = "spr_zsql@163.com"; //发件人地址
$mail->FromName = "test"; //发件人
$mail->AddAddress("yourmail@qq.com", "test"); //收件人地址,收件人名称
$mail->WordWrap = 50; //
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 附件
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // 附件,新文件名
$mail->IsHTML(true); // HTML格式
$mail->Subject = "测试";
$mail->Body = "测试";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
echo "发送邮件错误!";
}else{
echo "邮件发送成功!";
}
html代码
<form action="lead_mail_send.php" method="Post" enctype="multipart/form-data" name="myfm">
<table width="80%" border="0" align="center" cellspacing="6">
<tr>
<td width="10%">邮箱:</td>
<td width="90%"><label>
<input name="mail" type="text" id="mail" size="18" />
</label></td>
</tr>
<tr>
<td>电话:</td>
<td><input name="tel" type="text" id="tel" size="18" /></td>
</tr>
<tr>
<td>内容:</td>
<td><label>
<textarea name="bodys" id="bodys" cols="45" rows="5"></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="button2" type="submit" class="but01" id="button2" value="发布提交" onclick="return check();" />
<input name="button3" type="reset" class="but02" id="button3" value="清楚建议" />
</label></td>
</tr>
</table>
</form>
去down.111cn.net教程下载一个phpmailer源码教程
require("mail/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.163.com"; // smtp1.example.com;smtp2.example.comspecify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mailangel123"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->From = "mailangel123@163.com";
$mail->FromName = "人才自荐";
$Subject =PostGet('tel',1);
$jmail =PostGet('mail',1);
$body =PostGet('bodys',1);
$array ='zjj@oeoa.cn';
$mail->AddReplyTo("mailangel123@163.com", "此邮箱无需回复!");
$mail->AddAddress($array,'您好!');
$mail->WordWrap = 50;
$mail->CharSet="GB2312"; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $Subject;
$mail->Body = $body.'<br>'.$jmail;
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
$mail->ClearAddresses();
$mail->ClearAttachments();
}
else
{
echo "您好,你的信息己经发送到我们负责人信箱,请等候佳声。<a href="javascript教程:window.close();">点击
关闭</a>";
}
/* echo "<script>alert('发关".$count."邮件成功');</script>"; */
?>
// 本站原创转载注明来源www.111cn.net
<?php
class AttachmentMail {
private $from = 'yours@email.com';
private $from_name = 'Your Name';
private $reply_to = 'yours@email.com';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
$this -> to = $to;
$this -> subject = $subject;
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
}
public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = dirname($this -> attachment);
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path.'/'.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail."> ";
$header .= "Reply-To: ".$replyto." ";
$header .= "MIME-Version: 1.0 ";
$header .= "Content-Type: multipart/mixed; boundary="".$uid."" ";
$header .= "This is a multi-part message in MIME format. ";
$header .= "--".$uid." ";
$header .= "Content-type:text/plain; charset=iso-8859-1 ";
$header .= "Content-Transfer-Encoding: 7bit ";
$header .= $message." ";
$header .= "--".$uid." ";
$header .= "Content-Type: application/octet-stream; name="".$filename."" "; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64 ";
$header .= "Content-Disposition: attachment; filename="".$filename."" ";
$header .= $content." ";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from)."> ";
$header .= "Reply-To: ".($this -> reply_to)." ";
if (mail($this -> to, $this -> subject, $this -> message, $header)) {
return true;
} else {
return false;
}
}
}
}
?>
调用方法
<?php
require ('Email-Attachment.php');
$sendit = new AttachmentEmail('marry@example.com', 'Merry Christmas!', 'Hi', '/home/racker/gift.jpg');
$sendit -> mail();
?>
这个例子说明如何发送电子邮件使用SMTP身份验证。此特定示例使用Gmail发送。为通过SMTP发送如需Gmail要求的SMTP TLS身份验证。幸运的是,当我们使用梨邮件认证,连接自动TLS的。
这个例子之间的区别,只是在前面的示例使用SMTP是增加以下SMTP参数。
$smtp_params["auth"] = true; $smtp_params["username"] = "user@gmail.com"; $smtp_params["password"] = "pass";
<?
include('Mail.php');
include('Mail/mime.php');
// Constructing the email
$sender = "user@gmail.com"; // Your email address
$recipient = "Leigh <leigh@no_spam.net>"; // The Recipients name and email address
$subject = "Test Email"; // Subject for the email
$text = 'This is a text message.'; // Text version of the email
$html = '<html><body><p>This is a html message</p></body></html>'; // HTML version of the email
$crlf = "n";
$headers = array(
'From' => $sender,
'Return-Path' => $sender,
'Subject' => $subject
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
// Add an attachment
$file = "Hello World!";
$file_name = "Hello text.txt";
$content_type = "text/plain";
$mime->addAttachment ($file, $content_type, $file_name, 0);
// Set body and headers ready for base mail class
$body = $mime->get();
$headers = $mime->headers($headers);
// SMTP authentication params
$smtp_params["host"] = "smtp.gmail.com";
$smtp_params["port"] = "25";
$smtp_params["auth"] = true;
$smtp_params["username"] = "user@gmail.com";
$smtp_params["password"] = "pass";
// Sending the email using smtp
$mail =& Mail::factory("smtp", $smtp_params);
$result = $mail->send($recipient, $headers, $body);
if($result === 1)
{
echo("Your message has been sent!");
}
else
{
echo("Your message was not sent: " . $result);
}
?>
php 发送邮件代码程序
<?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("(^|(rn))(\.)", "[url=file://\1.\3]\1.\3[/url]", $body);
$header .= "MIME-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/htmlrn ";
}
$header .= "To: ".$to."rn";
if ($cc != "") {
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $from<".$from.">rn";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$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."n");
$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.">n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote hostn");
}
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."n");
$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."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]", "[url=file://\1]\1[/url]", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX "".$domain.""n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$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."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
continue;
}
$this->log_write("Connected to mx host ".$host."n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned "".$response.""n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".n");
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.""n");
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "[url=file://\([^()]*\]\([^()]*\[/url])";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "[url=file://\1]\1[/url]", $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;
}
}
?>
send.php:
$title = $_POST['title'];
$Message = $_POST['Message'];
$obj_title = iconv('UTF-8','Shift-JIS',$title);
$obj_message = iconv('UTF-8', 'Shift-JIS',$Message);
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "";//SMTP服务器的用户邮箱
$smtpemailto = "";//发送给谁
$smtpuser = ""; //SMTP服务器的用户帐号
$smtppass = "";//SMTP服务器的用户密码
$mailsubject = $title;//邮件主题
$mailbody = $Message ;//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = true;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
相关文章
- 本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- 这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- 这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
- 这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
- 本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
- 这篇文章主要介绍了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- 学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
- //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
- 这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
- 这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
node.js 基于 STMP 协议和 EWS 协议发送邮件
这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15- <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25
解决PHPMailer错误SMTP Error: Could not connect to SMTP host的办法
PHPMailer发邮件时提示SMTP Error: Could not connect to SMTP host错误是smtp服务器的问题我们一起来看看关于SMTP Error: Could not connect to SMTP host问题的解...2016-11-25