利用php傳送郵件

2021-07-25 11:03:44 字數 4775 閱讀 1732

啥都不說了,直接上**

<?php

//郵件傳送程式

//匯入smtp郵件傳送類

require("mail.php");

//smtp郵件伺服器

$smtpserver = "smtp.163.com";

//smtp伺服器端口

$smtpserverport = 25;

//smtp使用者郵箱位址

$smtpusermail = "******";

//收件人郵箱位址

$smtpemailto = "******x";

//smtp使用者名稱和密碼

$smtpuser = "******";

$smtppass = "******x";

//是否是用身份驗證

$isauth = true;

//郵件格式(html/txt),txt為文字郵件

$mailtype = "txt";

//郵件主題

$mailsubject = "大帥是個好同志";

//郵件內容

$mailbody = "嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎";

//新建smtp例項

$smtp = new smtp($smtpserver, $smtpserverport, $isauth, $smtpuser, $smtppass);

//是否顯示傳送的除錯資訊

$smtp->debug = false;

//傳送郵件

$res = $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

echo

"郵件已經傳送";

//var_dump($res);

?>

下面要用到乙個類

<?php 

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;

var_dump($to);

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)

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

="")

else

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

} } ?>

這裡的smtp使用者名稱和密碼

需要去相應的郵箱去設定

$smtpuser = "***xx";  

$smtppass = "******";

我用的是163郵箱所以

$smtpserver = "smtp.163.com";

php郵件傳送

今天要用到php的郵件傳送,便用phpmailer弄了個 類檔案 emaillclass.php class phpmailer function send addr,fromname,title,content smtp array array ehlo hello lb,220,250 ehlo...

php 郵件傳送

2 修改class.phpmailer.php 增加變數 fromemail 修改下面內容 成如下內容 if this fromname else result this headerline from this fromemail.fromname result this headerline r...

php 傳送郵件

results wpdb query sql 傳送郵件提醒有人申請 smtpserver smtp.qq.com smtp伺服器,如 smtp.qq.com smtpserverport 25 smtp伺服器端口,一般為25 smtpusermail 117 x837 qq.com smtp伺服器的...