PHP发送邮件

释放双眼,带上耳机,听听看~!

这里使用的是阿里云的邮件推送服务;

开通阿里云邮件推送服务,根据步骤新建发信域名和发信地址

新建发信域名:一定专门创建一个单独的二级域名,不要使用网站正在使用中的二级域名否则会导致网站域名再次解析网站不能访问;

发信域名新建完之后按照新建域名的配置PHP发送邮件

在域名解析中解析,解析完验证通过,在发信地址中新建发信域名,发信域名中有一个SMTP密码,这个密码在之后发送邮件时会使用到;

PHP发送邮件代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1     function send_email($sampleinfo){
2    
3        require(LIB_PATH.'aliyunemail/email.class.php');//引入类文件
4        //require(LIB_PATH.'email.class.php');
5        $mailto=$sampleinfo['email'];//收件邮箱
6        $mailsubject='请查收样本文件';//邮件标题
7        $samplename = $sampleinfo['sample_title'];
8        $sampleurl = $sampleinfo['sample_upload_url'];
9        //邮件内容
10        $mailbody=<<<aaa
11               <div style="width:100%;height:auto;background-color:#F0F0F0;padding: 50px 0;">
12                 <div style="width:600px;margin:0 auto;background-color:#fff;">
13                    <p style='height: 80px;line-height: 80px;padding-left: 20px;'>你好,</p>
14                    <p style='height: 50px;padding-left: 20px;'>请查收文件"{$samplename}"</p>
15                    <a href="{$sampleurl}" style='margin-left: 20px;margin-bottom: 30px;text-decoration:none;color:#FFF;background-color:#0099cc;border:solid #0099cc;border-width:10px 20px;line-height:2em;font-weight:bold;text-align:center;cursor:pointer;display:inline-block;border-radius:2px;text-transform:capitalize;'>点击下载</a>
16                 </div>
17                 <div style="width:600px;margin:0 auto;height:80px;text-align:center;line-height:80px;">
18                   本邮件由 <a href='http://www.a.com' style='color:#0099cc;'>aaa</a> 系统自动发出,请勿直接回复。
19                 </div>
20               </div>
21aaa;
22        $smtpserver     = "smtpdm.aliyun.com";//中继主机,使用的阿里云的所以这里是阿里云
23        $smtpserverport = 25;//SMTP端口
24        $smtpusermail   = "发信地址";
25        // 发件人的账号,填写控制台配置的发信地址,比如xxx@xxx.com
26        $smtpuser       = "发信账号";
27        // 访问SMTP服务时需要提供的密码(在控制台选择发信地址进行设置)
28        $smtppass       = "密码";
29        $mailsubject    = "=?UTF-8?B?" . base64_encode($mailsubject) . "?=";
30        $mailtype       = "HTML";
31        //可选,设置回信地址
32        $smtpreplyto    = "";
33        $smtp           = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
34        $smtp->debug    = false;//这里在linux中必须要改成false,否则会报500错误
35        $cc   ="";
36        $bcc  = "";
37        $additional_headers = "";
38        //设置发件人名称,名称用户可以自定义填写。
39        $sender  = "xx网";
40        $res = $smtp->sendmail($mailto,$smtpusermail, $mailsubject, $mailbody, $mailtype, $cc, $bcc, $additional_headers, $sender, $smtpreplyto);
41
42        return $res;
43    }
44

PHP有一个email.class.php类用来发送邮件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
1<?php
2/**
3 * email smtp (support php7)
4 *
5 */
6class Smtp
7{
8    /* Public Variables */
9    public $smtp_port;
10    public $time_out;
11    public $host_name;
12    public $log_file;
13    public $relay_host;
14    public $debug;
15    public $auth;
16    public $user;
17    public $pass;
18    /* Private Variables */
19    private $sock;
20    /* Constractor */
21    function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
22    {
23        $this->debug = FALSE;
24        $this->smtp_port = $smtp_port;
25        $this->relay_host = $relay_host;
26        $this->time_out = 30; //is used in fsockopen()
27        $this->auth = $auth;//auth
28        $this->user = $user;
29        $this->pass = $pass;
30        $this->host_name = "127.0.0.1"; //is used in HELO command
31        $this->log_file = "";
32        $this->sock = FALSE;
33    }
34    /* Main Function */
35    function sendmail($to, $from, $subject, $body, $mailtype, $cc, $bcc, $additional_headers, $fromUser, $replyToAddress)
36    {
37        $mail_from = $this->get_address($this->strip_comment($from));
38        $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
39        $header = "MIME-Version:1.0\r\n";
40        if($mailtype=="HTML"){
41            $header .= "Content-Type:text/html\r\n";
42        }
43        $header .= "To: ".$to."\r\n";
44        if ($cc != "") {
45            $header .= "Cc: ".$cc."\r\n";
46        }
47        $header .= "From: $fromUser<".$from.">\r\n";
48        $header .= "Subject: ".$subject."\r\n";
49        $header .= "Reply-To: ".$replyToAddress."\r\n";
50        $header .= $additional_headers;
51        $header .= "Date: ".date("r")."\r\n";
52        $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
53        list($msec, $sec) = explode(" ", microtime());
54        $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
55        $TO = explode(",", $this->strip_comment($to));
56        if ($cc != "") {
57            $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
58        }
59        if ($bcc != "") {
60            $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
61        }
62        $sent = TRUE;
63        foreach ($TO as $rcpt_to) {
64            $rcpt_to = $this->get_address($rcpt_to);
65            if (!$this->smtp_sockopen($rcpt_to)) {
66                $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
67                $sent = FALSE;
68                continue;
69            }
70            if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
71                $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
72            } else {
73                $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
74                $sent = FALSE;
75            }
76            fclose($this->sock);
77            $this->log_write("Disconnected from remote host\n");
78        }
79        return $sent;
80    }
81    /* Private Functions */
82    function smtp_send($helo, $from, $to, $header, $body = "")
83    {
84        if (!$this->smtp_putcmd("HELO", $helo)) {
85            return $this->smtp_error("sending HELO command");
86        }
87        //auth
88        if($this->auth){
89            if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
90                return $this->smtp_error("sending HELO command");
91            }
92            if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
93                return $this->smtp_error("sending HELO command");
94            }
95        }
96        if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
97            return $this->smtp_error("sending MAIL FROM command");
98        }
99        if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
100            return $this->smtp_error("sending RCPT TO command");
101        }
102        if (!$this->smtp_putcmd("DATA")) {
103            return $this->smtp_error("sending DATA command");
104        }
105        if (!$this->smtp_message($header, $body)) {
106            return $this->smtp_error("sending message");
107        }
108        if (!$this->smtp_eom()) {
109            return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
110        }
111        if (!$this->smtp_putcmd("QUIT")) {
112            return $this->smtp_error("sending QUIT command");
113        }
114        return TRUE;
115    }
116    function smtp_sockopen($address)
117    {
118        if ($this->relay_host == "") {
119            return $this->smtp_sockopen_mx($address);
120        } else {
121            return $this->smtp_sockopen_relay();
122        }
123    }
124    function smtp_sockopen_relay()
125    {
126        $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
127        $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
128        if (!($this->sock && $this->smtp_ok())) {
129            $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
130            $this->log_write("Error: ".$errstr." (".$errno.")\n");
131            return FALSE;
132        }
133        $this->log_write("Connected to relay host ".$this->relay_host."\n");
134        return TRUE;
135    }
136    function smtp_sockopen_mx($address)
137    {
138        $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
139        if (!@getmxrr($domain, $MXHOSTS)) {
140            $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
141            return FALSE;
142        }
143        foreach ($MXHOSTS as $host) {
144            $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
145            $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
146            if (!($this->sock && $this->smtp_ok())) {
147                $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
148                $this->log_write("Error: ".$errstr." (".$errno.")\n");
149                continue;
150            }
151            $this->log_write("Connected to mx host ".$host."\n");
152            return TRUE;
153        }
154        $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
155        return FALSE;
156    }
157    function smtp_message($header, $body)
158    {
159        fputs($this->sock, $header."\r\n".$body);
160        $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
161        return TRUE;
162    }
163    function smtp_eom()
164    {
165        fputs($this->sock, "\r\n.\r\n");
166        $this->smtp_debug(". [EOM]\n");
167        return $this->smtp_ok();
168    }
169    function smtp_ok()
170    {
171        $response = str_replace("\r\n", "", fgets($this->sock, 512));
172        $this->smtp_debug($response."\n");
173        if (!preg_match("/^[23]/", $response)) {
174            fputs($this->sock, "QUIT\r\n");
175            fgets($this->sock, 512);
176            $this->log_write("Error: Remote host returned \"".$response."\"\n");
177            return FALSE;
178        }
179        return TRUE;
180    }
181    function smtp_putcmd($cmd, $arg = "")
182    {
183        if ($arg != "") {
184            if($cmd=="") $cmd = $arg;
185            else $cmd = $cmd." ".$arg;
186        }
187        fputs($this->sock, $cmd."\r\n");
188        $this->smtp_debug("> ".$cmd."\n");
189        return $this->smtp_ok();
190    }
191    function smtp_error($string)
192    {
193        $this->log_write("Error: Error occurred while ".$string.".\n");
194        return FALSE;
195    }
196    function log_write($message)
197    {
198        $this->smtp_debug($message);
199        if ($this->log_file == "") {
200            return TRUE;
201        }
202        $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
203        if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
204            $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
205            return FALSE;
206        }
207        flock($fp, LOCK_EX);
208        fputs($fp, $message);
209        fclose($fp);
210        return TRUE;
211    }
212    function strip_comment($address)
213    {
214        $comment = "/\([^()]*\)/";
215        while (preg_match($comment, $address)) {
216            $address = preg_replace($comment, "", $address);
217        }
218        return $address;
219    }
220    function get_address($address)
221    {
222        $address = preg_replace("/([ \t\r\n])+/", "", $address);
223        $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
224        return $address;
225    }
226    function smtp_debug($message)
227    {
228        if ($this->debug) {
229            echo $message;
230        }
231    }
232}
233

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++回调函数

2022-1-11 12:36:11

安全运维

分类与监督学习,朴素贝叶斯分类算法

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索