>  기사  >  백엔드 개발  >  PHP 이메일 전송

PHP 이메일 전송

巴扎黑
巴扎黑원래의
2016-11-23 11:37:571038검색

class smtp 

/* 공개 변수 */ 
var $smtp_port; 
var $time_out; 
var $host_name; 
var $log_file; 
var $relay_host; 
var $debug; 
var $auth; 
var $user; 
var $pass; 
/* 개인 변수 */ 
var $sock; 
/* 생성자 */ 
함수 smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 

$this->debug = 거짓; 
$this->smtp_port = $smtp_port; 
$this->relay_host = $relay_host; 
$this->time_out = 30; //fsockopen()에서 사용됨 

$this->auth = $auth;//auth 
$this->user = $user; 
$this->pass = $pass; 

$this->host_name = "localhost"; //HELO 명령에 사용됩니다.
$this->log_file = ""; 
$this->sock = 거짓; 

/* 주요 기능 */ 
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = " ", $additional_headers = "") 

$mail_from = $this->get_address($this->strip_comment($from)); 
$body = ereg_replace("(^|(rn))(\.)", "\1.\3", $body); 
$header .= "MIME 버전:1.0rn"; 
if($mailtype=="HTML"){ 
$header .= "Content-Type:text/htmlrn"; 

$header .= "받는 사람: ".$to."rn"; 
if ($cc != "") { 
$header .= "Cc: ".$cc."rn"; 

$header .= "보낸 사람: $from<".$from.">;rn"; 
$header .= "제목: ".$subject."rn"; 
$header .= $additional_headers; 
$header .= "날짜: ".date("r")."rn"; 
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn"; 
list($msec, $sec) = 폭발(" ", 마이크로타임()); 
$header .= "메시지 ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">;rn" ; 
$TO = 폭발(",", $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를 $rcpt_to로) { 
$rcpt_to = $this->get_address($rcpt_to); 
if (!$this->smtp_sockopen($rcpt_to)) { 
$this->log_write("오류: ".$rcpt_to."n으로 이메일을 보낼 수 없습니다"); 
$sent = 거짓; 
계속; 

if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
$this->log_write("E- 메일이 <".$rcpt_to.">n")으로 전송되었습니다. 
} else { 
$this->log_write("오류: <".$rcpt_to.">;n으로 이메일을 보낼 수 없습니다"); 
$sent = 거짓; 

fclose($this->sock); 
$this->log_write("원격 호스트에서 연결이 끊어졌습니다."); 

$보낸 것을 반환합니다. 

/* 비공개 함수 */ 
function smtp_send($helo, $from, $to, $header, $body = "") 

if (!$this- >smtp_putcmd("HELO", $helo)) { 
return $this->smtp_error("HELO 명령 전송 중"); 

#auth 
if($this->auth){ 
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)) ) { 
return $this->smtp_error("HELO 명령을 보내는 중"); 

if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
return $this->smtp_error("HELO 명령 전송 중"); 



if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">;")) { 
return $this->smtp_error("MAIL FROM 명령을 보내는 중"); 

if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">;")) { 
return $this->smtp_error( "RCPT TO 명령을 보내는 중"); 

if (!$this->smtp_putcmd("DATA")) { 
return $this->smtp_error("sending DATA 명령"); 

if (!$this->smtp_message($header, $body)) { 
return $this->smtp_error("메시지를 보내는 중"); 

if (!$this->smtp_eom()) { 
return $this->smtp_error("sending ;;.;< ;LF>;; [EOM]"); 

if (!$this->smtp_putcmd("QUIT")) { 
return $this->smtp_error("QUIT 명령 보내기"); 

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("".$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("오류: 릴레이 호스트에 연결할 수 없습니다. ".$this-> Relay_host."n"); 
$this->log_write("오류: ".$errstr." (".$errno.")n"); 
FALSE를 반환합니다. 

$this->log_write("릴레이 호스트에 연결되었습니다. ".$this->relay_host."n"); 
TRUE를 반환합니다. 

함수 smtp_sockopen_mx($address) 

$domain = ereg_replace("^.+@([^@]+)$", "\1", $address); 
if (!@getmxrr($domain, $MXHOSTS)) { 
$this->log_write("오류: MX "".$domain.""n"); 
FALSE를 반환합니다. 

foreach ($MXHOSTS를 $host로) { 
$this->log_write("".$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("경고: mx 호스트 ".$host."n에 연결할 수 없습니다."n "); 
$this->log_write("오류: ".$errstr." (".$errno.")n"); 
계속; 

$this->log_write("mx 호스트 ".$host."n에 연결되었습니다."); 
TRUE를 반환합니다. 

$this->log_write("오류: 어떤 mx 호스트에도 연결할 수 없습니다(".implode(", ", $MXHOSTS).")n"); 
FALSE를 반환합니다. 

function smtp_message($header, $body) 

fputs($this->sock, $header."rn".$body); 
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; ") ); 
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("오류: 원격 호스트가 "".$response를 반환했습니다.""n"); 
FALSE를 반환합니다. 

TRUE를 반환합니다. 

함수 smtp_putcmd($cmd, $arg = "") 

if ($arg != "") { 
if($cmd=="") $cmd = $arg; 
그렇지 않으면 $cmd = $cmd." ".$arg; 

fputs($this->sock, $cmd."rn"); 
$this->smtp_debug(">; ".$cmd."n"); 
return $this->smtp_ok(); 

function smtp_error($string) 

$this->log_write("오류: ".$string.".n 동안 오류가 발생했습니다."); 
FALSE를 반환합니다. 

함수 log_write($message) 

$this->smtp_debug($message); 
if ($this->log_file == "") { 
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("경고: 로그 파일을 열 수 없습니다. "".$this->log_file.""n"); 
FALSE를 반환합니다. 

군집($fp, LOCK_EX); 
fputs($fp, $message); 
fclose($fp); 
TRUE를 반환합니다. 

function Strip_comment($address) 

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

$address를 반환합니다. 

함수 get_address($address) 

$address = ereg_replace("([ trn])+", "", $address); 
$address = ereg_replace("^.*<(.+)>;.*$", "\1", $address); 
$address를 반환합니다. 

함수 smtp_debug($message) 

if ($this->debug) { 
echo $message; 



기능 sendmail($smtpserver,$smtpuser,$smtppass,$smtpemailto,$smtpusermail, $mailsubject, $mailbody){ 
$smtp = 새 smtp($ smtpserver,25,true,$smtpuser,$smtppass); 

$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, "HTML"); 


//sendmail("smtp.126.com","test@126.com","password","1034555083@qq.com","test@126.com", "제목","본문"); 

//$smtpserver = "SMTP.163.com"; //smtp服务器的地址
$smtpserver="smtp.qq.com"; 
$port =25; //smtp服务器的端口,一般是 25 
$smtpuser = "116206443@emil.com"; //smtp服务器의 사용자 이름 
$smtppwd = "123456789oiuy"; //您登录smtp服务器的密码 
$mailtype = "TXT"; //가장 좋은 유형, 可选值是 TXT 或 HTML ,TXT 表示是纯文本的邮件,HTML 表示是 html格式的邮件 
$sender = "116206443@qq.com"; 
//发件人, 一般要与您登录smtp服务器의 사용자 이름($smtpuser)상동,否则可能会因为smtp服务器的设置导致发送失败 
$smtp = 새 smtp($smtpserver,$port ,true,$smtpuser,$smtppwd,$sender); 
$smtp->디버그 = true; //是否开启调试,只在测试程序时使用, 正式使用时请将此行注释 
$to = "116206443@qq.com"; //收件人 
$subject = "你好"; 
$body = "测试发送的内容"; 
$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype); 

if($send==1){ 
echo "邮件发送成功"; 
}else{ 
echo "邮件发送失败
"; 
//echo "원래:".$this->smtp->logs; 

?> 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 생성자의 이해다음 기사:PHP 생성자의 이해