SMTP服务器发送邮件:入门级


呵呵,一穷二白啊,从原理上就不理解...

好不容易找到个手把手的,终于通了。

http://www.gosoa.com.cn/php-email/


==================================

php 邮件发送如何进行的呢?

php邮件发送是通过smtp协议进行的。

下面是一个php邮件发送的类的一个函数。

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.">\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";
		$utfheader=iconv("UTF-8","GB2312",$header);
        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, $utfheader, $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");
        }
        return $sent;
    }
我们如何调用这个类呢?

再看示例

include("sendmail.php");//发送邮件类
		####################--发邮件--####################
		$smtpserver 	= 	"smtp.126.com";//SMTP服务器
		$smtpserverport =	25;//SMTP服务器端口
		$smtpusermail 	= 	"test@126.com";//SMTP服务器的用户邮箱
		$smtpuser 		= 	"test";//SMTP服务器的用户帐号
		$smtppass 		= 	"123456";//SMTP服务器的用户密码

		$smtpemailto 	= 	"dianzhong@126.com";//发送给谁
		$mailsubject 	= 	$username.'报名!';//邮件主题
		$mailtime		=	date("Y-m-d H:i:s");
		$mailbody 		= 	$content;//邮件内容

		$utfmailbody	=	iconv("UTF-8","GB2312",$mailbody);//转换邮件编码
		$mailtype 		= 	"HTML";//邮件格式(HTML/TXT),TXT为文本邮件

在这里需要一个smtp服务器。我们可以注册一个126的邮箱。 在上面的代码中,修改成你自己注册的邮箱地址和用户名、密码即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值