php傳送郵箱簡單教程

2021-07-10 21:24:38 字數 4554 閱讀 2790

在這裡使用163郵箱做演示,也使用其他的比如126 ,qq的

<?php

error_reporting(0);

/*** 1. 使用者名稱和密碼是否正確;

* 2. 檢查郵箱設定是否啟用了smtp服務;

* 3. 是否是php環境的問題導致;

* 4. 將26行的$smtp->debug = false改為true,可以顯示錯誤資訊,然後可以複製報錯資訊到網上搜一下錯誤的原因;

* 5. 如果還是不能解決,可以訪問:

*/require_once

"email.class.php";

//******************** 配置資訊 ********************************

$smtpserver = "smtp.163.com";//smtp伺服器

$smtpserverport =25;//smtp伺服器端口

// $smtpusermail = "[email protected]";//smtp伺服器的使用者郵箱

// $smtpemailto = $_post['toemail'];//傳送給誰

$smtpemailto = $email;//傳送給誰

$smtpuser = "new2008oh";//smtp伺服器的使用者帳號

$smtppass = "你的密碼";//smtp伺服器的使用者密碼

$mailtitle = $_post['title'];//郵件主題

$email2=md5($email);

$mailcontent = $_post['content'];

;//郵件內容

// $mailcontent = "";//郵件內容

$mailtype = "html";//郵件格式(html/txt),txt為文字郵件

//************************ 配置資訊 ****************************

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的乙個true是表示使用身份驗證,否則不使用身份驗證.

$smtp->debug = true;//是否顯示傳送的除錯資訊

$state = $smtp->sendmail($email, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

echo

""; if($state=="")

echo

"恭喜!郵件傳送成功!!";

echo

"點此返回";

echo"";

?>

引用

email.class.php

<?php

// error_reporting(e_all^e_notice);

error_reporting(0);

class

smtp

/* main function */

function

sendmail

($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

$header .= "to: ".$to."\r\n";

if ($cc != "")

$header .= "from: $from<".$from.">\r\n";

$header .= "subject: ".$subject."\r\n";

$header .= $additional_headers;

$header .= "date: ".date("r")."\r\n";

$header .= "x-mailer:by redhat (php/".phpversion().")\r\n";

list($msec, $sec) = explode(" ", microtime());

$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";

$to = explode(",", $this->strip_comment($to));

if ($cc != "")

if ($bcc != "")

$sent = true;

foreach ($to

as$rcpt_to)

if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) else

fclose($this->sock);

$this->log_write("disconnected from remote host\n");

}return

$sent;

}/* private functions */

function

smtp_send

($helo, $from, $to, $header, $body = "")

#auth

if($this->auth)

if (!$this->smtp_putcmd("", base64_encode($this->pass))) }#

if (!$this->smtp_putcmd("mail", "from:<".$from.">"))

if (!$this->smtp_putcmd("rcpt", "to:<".$to.">"))

if (!$this->smtp_putcmd("data"))

if (!$this->smtp_message($header, $body))

if (!$this->smtp_eom())

if (!$this->smtp_putcmd("quit"))

return

true;

}function

smtp_sockopen

($address)

else

}function

smtp_sockopen_relay

()$this->log_write("connected to relay host ".$this->relay_host."\n");

return

true;;

}function

smtp_sockopen_mx

($address)

//專注與php學習 歡迎您的訪問

foreach ($mxhosts

as$host)

$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)

function

smtp_eom

()function

smtp_ok

()return

true;

}function

smtp_putcmd

($cmd, $arg = "")

fputs($this->sock, $cmd."\r\n");

$this->smtp_debug("> ".$cmd."\n");

return

$this->smtp_ok();

}function

smtp_error

($string)

function

log_write

($message)

$message = date("m d h:i:s ").get_current_user()."[".getmypid()."]: ".$message;

if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))

flock($fp, lock_ex);

fputs($fp, $message);

fclose($fp);

return

true;

}function

strip_comment

($address)

return

$address;

}function

get_address

($address)

function

smtp_debug

($message)}}

?>

php 傳送郵件到郵箱

stmp協議 我們傳送的郵件通過先將郵件傳送到stmp伺服器上,然後stmp伺服器在去傳送到對應的郵件的地方,這裡面我採用了163的郵箱,埠號25,在使用的時候需要開啟郵箱的stmp服務,但在qq上的服務開啟沒有通過這個程式,163通過了。我們採用了別人的框架和程式,可以參考一下 created b...

phpmailer郵箱傳送

引入phpmailer的核心檔案 require once common phpmailer master src phpmailer.php require once common phpmailer master src smtp.php use phpmailer phpmailer 例項化p...

python 傳送郵箱

coding utf 8 import smtplib from email.mime.multipart import mimemultipart from email.mime.text import mimetext from email.mime.image import mimeimage...