PHP로 이메일을 보내는 간단한 클래스

WBOY
풀어 주다: 2016-08-08 09:33:35
원래의
2678명이 탐색했습니다.

sm.php

클래스 smtp
{
/* 공용 변수 */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $패스;

/* PRivate 변수 */
var $양말;

/* 생성자 */
함수 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;
$this->pass = $pass;
#
$this->host_name = "localhost"; //HELO 명령에 사용됩니다
$this->log_file = "";

$this->sock = 거짓;
}

/* 주요 기능 */
함수 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 .= "콘텐츠 유형:텍스트/htmlrn";
}
$header .= "받는 사람: ".$to."rn";
if ($cc != "") {
$header .= "참조: ".$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, 폭발(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$TO = array_merge($TO, 폭발(",", $this->strip_comment($bcc)));
}

$sent = 참;
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("이메일이 <".$rcpt_to.">;n으로 전송되었습니다.");
} 그 밖의 {
$this->log_write("오류: <".$rcpt_to.">;n으로 이메일을 보낼 수 없습니다.");
$sent = 거짓;
}
fclose($this->sock);
$this->log_write("원격 호스트에서 연결이 끊어졌습니다.");
}
$sent 반환
}

/* 비공개 함수 */

함수 smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("HELO 명령을 보내는 중");
}
#인증
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("DATA 명령 전송 중");
}

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

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

if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("QUIT 명령을 보내는 중");
}

TRUE를 반환합니다.
}

함수 smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} 그 밖의 {
return $this->smtp_sockopen_relay();
}
}

함수 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");
거짓을 반환합니다.
}
$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");
거짓을 반환합니다.
}
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에 연결할 수 없습니다.");
$this->log_write("오류: ".$errstr." (".$errno.")n");
계속;
}
$this->log_write("mx 호스트 ".$host."n에 연결되었습니다.");
TRUE를 반환합니다.
}
$this->log_write("오류: 어떤 mx 호스트에도 연결할 수 없습니다(".implode(", ", $MXHOSTS).")n");
거짓을 반환합니다.
}

함수 smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; "));

TRUE를 반환합니다.
}

함수 smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(".[EOM]n");

return $this->smtp_ok();
}

함수 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"을 반환했습니다.
거짓을 반환합니다.
}
TRUE를 반환합니다.
}

함수 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();
}

함수 smtp_error($string)
{
$this->log_write("오류: ".$string.".n 동안 오류가 발생했습니다.");
거짓을 반환합니다.
}

함수 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 (log_file">!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("경고: 로그 파일 "".$this->log_file.""n");
거짓을 반환합니다.
}
무리($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);

TRUE를 반환합니다.
}

함수 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) {
에코 $메시지;
}
}
}
?>

메일.php

require("sm.php");
#####################################
$smtpserver = "mail.asdf.com";//SMTP 서버
$smtpserverport =25;//SMTP 서버 포트
$smtpusermail = "asdf@asdf.com";//SMTP 서버 사용자 이메일
$smtpemailto = "asdf@QQ.com";//보낼 사람
$smtpuser = "asdf@asdf.com";//SMTP 서버 사용자 계정
$smtppass = "asdf";//SMTP 서버의 사용자 비밀번호
$mailsubject = "테스트 제목";//메일 제목
$mailbody = "

;테스트 메일입니다

;";//메일 내용
$mailtype = "HTML";//이메일 형식(HTML/TXT), TXT는 텍스트 이메일
#####################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//여기서 true는 인증이 사용됨을 의미하고, 그렇지 않으면 인증이 사용되지 않음을 의미합니다.
$smtp->debug = TRUE;//전송된 디버깅 정보를 표시할지 여부
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

위 내용은 PHP로 이메일을 보내는 간단한 클래스를 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!