[php]php curl smtp发送邮件
公司的云平台把fsockopen关掉了,如果要使用smtp外网的邮箱来发送邮件的话 只能试试使用curl来进行了
先google了一下,发现很多问相关问题的但没有相关的解答,在phpclasses里也没有找到相关的类于是自己边看stmp的相关协议边开始尝试curl
SMTP协议
这个在网上可以找到多相关的例子,可以自己实验一下使用telnet去连接mail服务器
$ telnet 邮箱SMTP服务地址 25
Trying 邮箱服务IP地址...
Connected to 邮箱SMTP服务地址.
Escape character is '^]'.
220 exchange邮箱服务器地址 Microsoft ESMTP MAIL Service ready at Sat, 2 Jun 2012 15:02:12 +0800
EHLO 127.0.0.1
250-exchange邮箱服务器地址 Hello [邮箱服务IP地址]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
AUTH LOGIN
334 VXNlcm5hbWU6
用户名(base64_encode)
334 UGFzc3dvcmQ6
密码(base64_encode)
235 2.7.0 Authentication successful
MAIL FROM:发件箱地址
250 2.1.0 Sender OK
RCPT TO:收件箱地址
250 2.1.5 Recipient OK
DATA
354 Start mail input; end with
要发送的内容(这里的相关的规范有很多)
.
250 2.6.0 Queued mail for delivery
QUIT
221 2.0.0 Service closing transmission channel
Connection closed by foreign host.
php测试代码:
1
2 header("content-type:text/html;charset=utf-8");
3 $smtp = array(
4 "url" => "邮箱SMTP服务器地址",
5 "port" => "邮箱SMTP服务器端口", // 一般为25
6 "username" => "用户名",
7 "password" => "密码",
8 "from" => "发件地址",
9 "to" => "收件地址",
10 "subject" => "测试一下标题",
11 "body" => "测试一下内容"
12 );
13
14 $CRLF = "\r\n";
15 $test = "";
16 $curl = curl_init();
17
18 curl_setopt($curl, CURLOPT_URL, $smtp['url']);
19 curl_setopt($curl, CURLOPT_PORT, $smtp['port']);
20 curl_setopt($curl, CURLOPT_TIMEOUT,10);
21
22 function inlineCode($str){
23 $str = trim($str);
24 return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
25 }
26
27 function buildHeader($headers){
28 $ret = '';
29 foreach($headers as $k=>$v){
30 $ret.=$k.': '.$v."\n";
31 }
32 return $ret;
33 }
34
35 //
36 $header = array(
37 'Return-path'=>'',
38 'Date'=>date('r'),
39 'From'=> '',
40 'MIME-Version'=>'1.0',
41 'Subject'=>inlineCode($smtp['subject']),
42 'To'=>$smtp['to'],
43 'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
44 'Content-Transfer-Encoding'=>'base64'
45 );
46 $data = buildHeader($header).$CRLF.chunk_split(base64_encode($smtp['body']));
47
48
49 $content = "EHLO ".$smtp["url"].$CRLF; // 先得hello一下
50 $content .= "AUTH LOGIN".$CRLF.base64_encode($smtp["username"]).$CRLF.base64_encode($smtp["password"]).$CRLF; // 验证登陆
51 $content .= "MAIL FROM:".$smtp["from"].$CRLF; // 发件地址
52 $content .= "RCPT TO:".$smtp["to"].$CRLF; // 收件地址
53 $content .= "DATA".$CRLF.$data.$CRLF.".".$CRLF; // 发送内容
54 $content .= "QUIT".$CRLF; // 退出
55
56 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl接收返回数据
57 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $content);
58 $test = curl_exec($curl);
59 var_dump($test);
60 echo "
\r\n";
61 var_dump($content);
62
63 // 结束
64 curl_close($curl);
以上只是测试的php
包测试+修改花了近6个小时让产品的代码兼容了fsockopen和curl
以后有空写个兼容fsockopen和curl简单发送邮件的smtp类
think in coding
摘自 bluefrog

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
