php電子郵件發送bcc(PHP 發郵件)

  • 1、電子郵件中的CC和BCC分別代表什麼?
  • 2、求php 發送郵件的代碼?
  • 3、php怎麼實現發送郵件
  • 4、怎麼利用php發送郵件求詳細教程
  • 5、php中郵件密抄怎麼使用

CC 英文全稱是 Carbon Copy(抄送)

BCC英文全稱是 Blind Carbon Copy(暗抄送)。

兩者的區別在於在BCC欄中的收件人可以看到所有的收件人名(TO,CC,BCC),而在TO 和CC欄中的收件人看不到BBC的收件人名

抄送的英文縮寫為CC,來源於Carbon Copy,在使用手動機械打字機時代,採用複寫紙,同樣一份文件可以一次列印出兩份乃至多份,分送給不同的人。到了互聯網時代,如果一份郵件需要發送給多個人閱讀,只要在「抄送」或「CC」一欄填上相關人員的信箱地址即可。如果是抄送多人,只要將他們的地址用逗號隔開,即可發送到相應的信箱。

密送的英文縮寫為BCC,來源於Blind Carbon Copy,由於某種原因或出於某種考慮,你不希望收信人知道你把這封郵件還發送給了另外的人,則可將這位幕後的人的信箱地址放在密送一欄。

擴展資料

安全知識

電子郵件的安全問題包含兩個方面,一個是郵件可能給系統帶來的不安全因素,二是郵件內容本身的隱私性。

1. 瀏覽器的安全設置。

以IE為例,首先建議將IE升級到最新版本,然後點擊「工具→Internet屬性」,進入「安全」選項卡,在這裡可以對四種不同區域(包括「Internet」、「本地Intranet」、「受信任的站點」以及「受限制的站點」)分別進行安全設置。對於「Internet」區域通過「自定義級別」可以按照自己的安全考慮加以設置。

雖然大多數Cookie對於系統來說是安全的,但在「隱私」選項卡中對它加以設置可以適當保護自己的隱私。如果擔心信件內容的泄露,可以在「內容」選項卡中進行證書的設置等。

2. 關閉電子郵件地址自動處理功能。

由於軟體中自動處理功能的日益增加,我們會越來越多地看到由於意外地選擇了錯誤收件人而造成的安全事件了。微軟Outlook中的「可怕的自動填寫功能」 就是一個很明顯的例子,在使用下拉式清單的時候很容易不小心選擇臨近實際收件人的收件人。在討論類似商業機密之類敏感信息的時候,這樣的操作很容易導致各種安全事件的出現。

3. 群發郵件的時候採用密送(BCC)的設置。

從安全形度來說,將電子郵件地址與沒有必要知道的人分享,是一個壞做法。在未經允許的情況下,將電子郵件地址與陌生人分享也是不禮貌的。在發送電子郵件給多個人的時候,可以選擇收件人(TO)或者抄送(CC)的方式,這樣的情況下,所有收件人可以分享所有的電子郵件地址。如果沒有明確確認電子郵件地址應該被所有收件人分享的時候,應該使用密送(BCC)的設置。這樣的話,收件人不會知道還有其它接收者的存在。

4. 做好閱後的工作。

如果在公共場所收發信件,信件內容的隱私性就變得至關重要。可以通過「Internet 選項」的「常規」選項卡刪除文件(包括所有離線內容)、清除歷史記錄以及刪除Cookie。另外,還可以到「內容」選項卡的「個人信息」欄進行自動完成設置,清除表單以及密碼等。

安全因素

安全考量包括傳輸安全、儲存安全、發送者身份確認、接收者已收到確認、拒絕服務攻擊等。有兩種標準:PGP和S/MIME。

參考資料:百度百科-電子郵件

?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;$icount($addressArray);$i++){ if($this-checkEmail($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;$icount($addressArray);$i++){ if($this-checkEmail($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;$icount($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; $icount($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;$icount($attachmentArray);$i++){

//–分割 $mailHeader .= “n–“.$attachmentBoundary. “n”;

//–附件信息

$mailHeader .= $this-formatAttachmentHeader($attachmentArray[$i]);

}

$mailHeader .= “–“.$attachmentBoundary. “–“;

return mail($this-mailTo,$this-mailSubject, “”,$mailHeader);

}

return false;

}

}

?

PHP發送郵件是「非常的簡單」 因為他提供了mail()函數直接發送,但配置相當麻煩 (1)通過mail()函數發送郵件 mail() 配置PHP.ini 郵件信息 需要類似sendmail這樣的組件支持 (2)通過socket通訊,使用SMTP傳輸 socket連接-SMTP通訊-獲取通訊消息-發送 mail函數的使用 mail() 函數允許您從腳本中直接發送電子郵件。 如果郵件的投遞被成功地接收,則返回 true,否則返回 mail(to,subject,message,headers,parameters) socket方式發送原理 給你一個別人寫好的類 用法在下面 本人經測試很多網站都不提供免費的smtp服務(126、sina、netease 這幾個試過了),騰訊郵箱支持此功能。 用法: ? require_once (’email.class.php’); //########################################## $smtpserver = “smtp.163.com”;//SMTP伺服器 $smtpserverport =25;//SMTP伺服器埠 $smtpusermail = “”;//SMTP伺服器的用戶郵箱 $smtpemailto = “”;//發送給誰 $smtpuser = “”;//SMTP伺服器的用戶帳號 $smtppass = “”;//SMTP伺服器的用戶密碼 $mailsubject = “PHP100測試郵件系統”;//郵件主題 $mailbody = “h1 這是一個測試程序 PHP100.com /h1”;//郵件內容 $mailtype = “HTML”;//郵件格式(HTML/TXT),TXT為文本郵件 ########################################## $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證. $smtp-debug = FALSE;//是否顯示發送的調試信息 $smtp-sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); ? 郵件發送類 ? 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(“(^|(\r\n))(\\.)”, “\\1.\\3”, $body); $header .= “MIME-Version:1.0\r\n”; if($mailtype==”HTML”){ $header .= “Content-Type:text/html\r\n”; } $header .= “To: “.$to.”\r\n”; if ($cc != “”) { $header .= “Cc: “.$cc.”\r\n”; } $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 != “”) { $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 host\n”); } 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 CRLF.CRLF [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(“^.+@([^@]+)$”, “\\1”, $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.”\r\n”.$body); $this-smtp_debug(” “.str_replace(“\r\n”, “\n”.” “, $header.”\n “.$body.”\n “)); return TRUE; } function smtp_eom() { fputs($this-sock, “\r\n.\r\n”); $this-smtp_debug(“. [EOM]\n”); return $this-smtp_ok(); } function smtp_ok() { $response = str_replace(“\r\n”, “”, fgets($this-sock, 512)); $this-smtp_debug($response.”\n”); if (!ereg(“^[23]”, $response)) { fputs($this-sock, “QUIT\r\n”); 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.”\r\n”); $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 = “\\([^()]*\\)”; while (ereg($comment, $address)) { $address = ereg_replace($comment, “”, $address); } return $address; } function get_address($address) { $address = ereg_replace(“([ \t\r\n])+”, “”, $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; } } ?

PHP雖然提供了mail()函數,但並不好用,而PHPMailer是一個不錯的郵件發送工具,接下來將詳細介紹,需要了解的朋友可以參考下:

本人使用wamp集成開發環境,Apache2.4.4, Mysql5.6.12 , php5.4.12.開始的時候使用mail()發送郵件,更改配置始終無法成功,了解到mail()函數使用需要sendmail程序。又下載了sendmail程序擴展包。按照網上的說法也改好了php.ini和sendmail.ini。使用foxmail 7.1創建了自己的qq郵箱賬戶,開啟了POP3/SMTP服務,更改發件伺服器為POP3,使用和收件伺服器相同的身份驗證,結果還是報錯:Warning: mail(): SMTP server response: 503 Error: need EHLO and AUTH first ! in F:\PHP\wamp\www\mail.php on line 8。以下是使用mail()函數發送郵件的php代碼:

[php] view plain copy

span style=”font-size:14px”?php

$to = “757006080@qq.com”;

$subject = “Test mail”;

$message = “Hello! This is a simple email message.”;

$from = “757006080@qq.com”;

$headers = “From: $from”;

$send=mail($to,$subject,$message,$headers);

if($send)

echo “Mail Sent”;

else

echo “Sorry,mail sent failed!”

?/span

在CSDN論壇上發現phpmailer可以方便快捷的發送郵件,以下寫出詳細使用教程:

1.需要下載PHPMailer文件包,(點擊打開鏈接)

2.確認你的伺服器已經系統支持socket,通過phpinfo()查看是否支持socket;

3.把文件解壓到你的WEB伺服器目錄下,就可以使用PHPMailer發送郵件了。

以下為前台表單php代碼:

[php] view plain copy

span style=”font-size:14px”html

body

h3phpmailer Unit Test/h3

請你輸入font color=”#FF6666″收信/font的郵箱地址:

form name=”phpmailer” action=”testemail.php” method=”post”

input type=”hidden” name=”submitted” value=”1″/

郵箱地址: input type=”text” size=”50″ name=”to” /

br/

input type=”submit” value=”發送”/

/form

/body

/html /span

以下為後台程序:

[php] view plain copy

?php

/**

* Simple example script using PHPMailer with exceptions enabled

* @package phpmailer

* @version $Id$

*/

header(“content-type:text/html;charset=utf-8”);

ini_set(“magic_quotes_runtime”,0);

require(‘class.phpmailer.php’);

try {

$mail = new PHPMailer(true); //New instance, with exceptions enabled

//$body = file_get_contents(‘contents.html’);

//$body = preg_replace(‘/\\\\/’,”, $body); //Strip backslashes

$to = $_POST[‘to’];

$mail-CharSet=”GB2312″;//設置郵件字元編碼否則郵件會亂碼

$mail-Encoding=”base64″;

$mail-IsSMTP(); // tell the class to use SMTP

$mail-SMTPAuth = true; // enable SMTP authentication

$mail-Port = 25; // set the SMTP server port

$mail-Host = “smtp.qq.com”; // SMTP server

$mail-Username = “757006080@qq.com”; // SMTP server username

$mail-Password = “000000000000”; // SMTP server password

//$mail-IsSendmail(); // tell the class to use Sendmail

$mail-AddReplyTo(“757006080@qq.com”,”han qing”);

$mail-From = “757006080@qq.com”;

$mail-FromName = “han qing”;

//$to = “hanqing757@gmail.com”;

$mail-AddAddress($to);

$mail-Subject =$mail-Subject = “=?utf-8?B?” . base64_encode(“First PHPMailer Message”) . “?=”;

$mail-Body = “h1phpmailer演示/h1 這是用PHPMAILER發的第一份郵件,從QQ郵箱發到Google郵箱.”;

$mail-AddAttachment(“F:/myloe.jpg”);

$mail-AltBody = “To view the message, please use an HTML compatible email viewer!”; // optional, comment out and test

$mail-WordWrap = 80; // set word wrap

//$mail-MsgHTML($body);

$mail-IsHTML(true); // send as HTML

$mail-Send();

echo ‘Message has been sent.’;

} catch (phpmailerException $e) {

echo $e-errorMessage();

}

?

增加其他收信人的時候使用bcc,密抄送。不知道你是用的什麼樣的發送方式,mail函數的話如下,其中寫到header中,bcc後面的地址就是密抄送了。

$headers = ‘MIME-Version: 1.0’ . “\r\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

// Additional headers

$headers .= ‘To: Mary mary@example.com, Kelly kelly@example.com’ . “\r\n”;

$headers .= ‘From: Birthday Reminder birthday@example.com’ . “\r\n”;

$headers .= ‘Cc: birthdayarchive@example.com’ . “\r\n”;

$headers .= ‘Bcc: birthdaycheck@example.com’ . “\r\n”;

// Mail it

mail($to, $subject, $message, $headers);

原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126142.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:06
下一篇 2024-10-03 23:06

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若伺服器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台伺服器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種資料庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論