search
HomeBackend DevelopmentPHP TutorialSending email using Socket (using SMTP server that requires authentication)_PHP tutorial

*  名称:用Socket发送电子邮件
*  描述:本类实现了直接使用需要验证的SMTP服务器直接发送邮件,参考文章《用Socket发送电子邮件》作者:limodou
*        此文章比较早,他是用不用验证SMTP服务器发送邮件,现在基本上SMTP服务器都需要验证了,所以这个文章里的类
          意义也不是很大!同时参考了[RFC 1869]和PHP手册!!和上文还有不同的是我用的不是fsockopen()函数
          具体你自己看吧!!我刚刚测试通过了,很爽!!
          其实把这个类再改写一下就可以直接发送带附件的邮件了,期待。。。我过几天给大家写出来!
          我刚刚调试通过,如果你不恶意去捉弄这个程序,他还是很听话的,过些日子我会对他完善加上发送附件的功能!!

即使你的虚拟主机不支持MAIL函数,现在也不怕了!快快试试这个吧!

 
请尊重别人的劳动成功,请保留此版权信息,谢谢!
作者:小露珠3.3  ,欢迎和我联系交流PHP QQ6550382 
MAIL:cleverzhang@tom.com
*/
set_time_limit(120);
class smtp_mail
{
var $host;          //主机
var $port;          //端口 一般为25
var $user;          //SMTP认证的帐号
var $pass;          //认证密码
var $debug = false;   //是否显示和服务器会话信息?
var $conn;
var $result_str;      //结果
var $in;          //客户机发送的命令
var $from;          //源信箱
var $to;          //目标信箱
var $subject;         //主题
var $body;          //内容
function smtp_mail($host,$port,$user,$pass,$debug=false)
{
$this->host   = $host;
$this->port   = $port;
$this->user   = base64_encode($user);
$this->pass   = base64_encode($pass);
$this->debug  = $debug;
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);  //具体用法请参考手册
if($this->socket)
{
$this->result_str  =  "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失败,请检查您的网络连接和参数");
}
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str  =  "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失败,请检查您的网络连接和参数");
}
$this->result_str = "服务器应答:".socket_read ($this->socket, 1024)."";
$this->debug_show($this->result_str);


}
function debug_show($str)
{
if($this->debug)
{
echo $str."

\r\n";
}
}
function send($from,$to,$subject,$body)
{
if($from == "" || $to == "")
{
exit("请输入信箱地址");
}
if($subject == "") $sebject = "无标题";
if($body    == "") $body    = "无内容";
$this->from     =  $from;
$this->to       =  $to;
$this->subject  =  $subject;
$this->body     =  $body;

$All          = "From:".$this->from."\n";
$All          .= "To:".$this->to."\n";
$All          .= "Subject:".$this->subject."\n";
$All          .= $this->body;
/*
  如过把$All的内容再加处理,就可以实现发送MIME邮件了
  不过还需要加很多程序
*/


//以下是和服务器会话
$this->in       =  "EHLO HELOrn";
$this->docommand();

$this->in       =  "AUTH LOGINrn";
$this->docommand();

$this->in       =  $this->user."rn";
$this->docommand();

$this->in       =  $this->pass."rn";
$this->docommand();

$this->in       =  "MAIL FROM:".$this->from."rn";
$this->docommand();

$this->in       =  "RCPT TO:".$this->to."rn";
$this->docommand();

$this->in       =  "DATArn";
$this->docommand();

$this->in       =  $All."rn.rn";
$this->docommand();

$this->in       =  "QUITrn";
$this->docommand();

//结束,关闭连接

 

}
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客户机命令:".$this->in);
$this->result_str = "服务器应答:".socket_read ($this->socket, 1024)."";
$this->debug_show($this->result_str);
}
}
//这个是我做的测试,我用的是smtp.163.com,那你的信箱也必须是163.com的,要不人家不让你发!!
//你用这个类的时候你修改成你自己的信箱就可以了
$smtp  =   new smtp_mail("smtp.163.com","25","你的163.com的帐号","你的密码");
//如果你需要显示会话信息,请将上面的修改成
//$smtp  =   new smtp_mail("smtp.163.com","25","你的163.com的帐号","你的密码",true);
$smtp->send("你的163.com的帐号@163.com","目标地址","你好","你好");
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/314294.htmlTechArticle? * 名称:用Socket发送电子邮件 * 描述:本类实现了直接使用需要验证的SMTP服务器直接发送邮件,参考文章《用Socket发送电子邮件》作者:...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.