POP3, SMTP mail transceiver

Source: Internet
Author: User
Tags define copy count return socket string sprintf version
smtp| program <?php
if ($EMAIL _inc) return;
$EMAIL _inc= "defined";
Define ("Smtpport", 25);

Class Pop3 {
var $subject; Message subject
var $from _email; Sender Address
var $from _name; Sender Name
var $to _email; Recipient address
var $to _name; Recipient Name
var $body; Message content
var $filename; Filename
var $socket; The current socket
var $Line;
var $Status;

function Pop3_open ($server, $port)
{

$this->socket = Fsockopen ($server, $port);
if ($this->socket <= 0) {
return false;
}
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return false;
return true;
}

function Pop3_user ($user)
{

if ($this->socket < 0) {
return false;
}
Fputs ($this->socket, "USER $this->userrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return false;

return true;
}

function Pop3_pass ($pass)
{

Fputs ($this->socket, "pass $PASSRN");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

return 1;
}

function Pop3_stat ()
{

Fputs ($this->socket, "statrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

if (!eregi ("+ok (. *) (. *)", $this->line, $regs))
return 0;

return $regs [1];
}

function Pop3_list ()
{
Fputs ($this->socket, "listrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

$i = 0;
while (substr ($this->line = fgets ($this->socket, 1024), 0, 1) <> ".")
{
$articles [$i] = $this->line;
$i + +;
}
$articles ["count"] = $i;

return $articles;
}

function Pop3_retr ($NR)
{

Fputs ($this->socket, "RETR $nrrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

while (substr ($this->line = fgets ($this->socket, 1024), 0, 1) <> ".")
{
$data [$i] = $this->line;
$i + +;
}
$data ["count"] = $i;

return $data;
}

function Pop3_dele ($NR)
{

Fputs ($this->socket, "DELE $nrrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;
return 1;
}

function Pop3_quit ()
{

Fputs ($this->socket, "quitrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

return 1;
}
}

Class Smtp {

var $Subject; String The email ' s subject
var $FromName; String Sender ' s name (opt)
var $ToName; String Recipient ' s name (opt)
var $Body; String Body Copy
var $Attachment; Attachment (optional)
var $AttachmentType;
var $Socket;
var $Line;
var $Status;

function Smtp ($Server = "localhost", $Port = Smtpport)
{
return $this->open ($Server, $Port);
}

function SmtpMail ($FromEmail, $FromName, $ToEmail, $ToName, $Subject, $Body, $Attachment =null, $AttachmentType = "TEXT")
{
$this->subject = $Subject;
$this->toname = $ToName;

$this->fromname = $FromName;
$this->body = $Body;

$this->attachment = $Attachment;
$this->attachmenttype = $AttachmentType;

if ($this->helo () = = False) {
return false;
}
if ($this->mailfrom ($FromEmail) = = False) {
return false;
}
if ($this->rcptto ($ToEmail) = = False) {
return false;
}
if ($this->body () = = False) {
return false;
}
if ($this->quit () = = False) {
return false;
}
}

function Open ($Server, $Port)
{

$this->socket = Fsockopen ($Server, $Port);
if ($this->socket < 0) return false;

$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}


function Helo ()
{
if (fputs ($this->socket, "Helorn") < 0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}

function Ehlo ()
{

/* Yes, let's use the "Helo" for now. Until we need the
Extra Func ' s [Unk]
*/
if (fputs ($this->socket, "Helo localhostrn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}


function Mailfrom ($FromEmail)
{

if (fputs ($this->socket, "MAIL from: < $FromEmail >rn") <0) {
return false;
}

$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}

function Rcptto ($ToEmail)
{

if (fputs ($this->socket, "RCPT to: < $ToEmail >rn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;
return true;
}

function Body ()
{
$FileSize = 0;
$Attachment = null;
$fp = null;

$buffer = sprintf ("From:%SRNTO:%SRNSUBJECT:%SRN", $this->fromname, $this->toname, $this->subject);

if (fputs ($this->socket, "Datarn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "3") return false;

if (fputs ($this->socket, $buffer) <0) {
return false;
}


if ($this->attachment = = null) {

if (fputs ($this->socket, "Mime-version:1.0rncontent-type:text/plain; Charset=iso-8859-1rncontent-transfer-encoding:7bitrnrn ") <0) {
return false;
}
if (fputs ($this->socket, "$this->bodyrnrn") <0) {
return false;
}

if (fputs ($this->socket, ". Rn") <0) {
return false;
}

$this->
POP3, SMTP mail transceiver

Publish time: September 28, 2001 read times: 18 recommended to friends print this page

<?php
if ($EMAIL _inc) return;
$EMAIL _inc= "defined";
Define ("Smtpport", 25);

Class Pop3 {
var $subject; Message subject
var $from _email; Sender Address
var $from _name; Sender Name
var $to _email; Recipient address
var $to _name; Recipient Name
var $body; Message content
var $filename; Filename
var $socket; The current socket
var $Line;
var $Status;

function Pop3_open ($server, $port)
{

$this->socket = Fsockopen ($server, $port);
if ($this->socket <= 0) {
return false;
}
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return false;
return true;
}

function Pop3_user ($user)
{

if ($this->socket < 0) {
return false;
}
Fputs ($this->socket, "USER $this->userrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return false;

return true;
}

function Pop3_pass ($pass)
{

Fputs ($this->socket, "pass $PASSRN");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

return 1;
}

function Pop3_stat ()
{

Fputs ($this->socket, "statrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

if (!eregi ("+ok (. *) (. *)", $this->line, $regs))
return 0;

return $regs [1];
}

function Pop3_list ()
{
Fputs ($this->socket, "listrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

$i = 0;
while (substr ($this->line = fgets ($this->socket, 1024), 0, 1) <> ".")
{
$articles [$i] = $this->line;
$i + +;
}
$articles ["count"] = $i;

return $articles;
}

function Pop3_retr ($NR)
{

Fputs ($this->socket, "RETR $nrrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

while (substr ($this->line = fgets ($this->socket, 1024), 0, 1) <> ".")
{
$data [$i] = $this->line;
$i + +;
}
$data ["count"] = $i;

return $data;
}

function Pop3_dele ($NR)
{

Fputs ($this->socket, "DELE $nrrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;
return 1;
}

function Pop3_quit ()
{

Fputs ($this->socket, "quitrn");
$this->line = fgets ($this->socket, 1024);
$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "+") return 0;

return 1;
}
}

Class Smtp {

var $Subject; String The email ' s subject
var $FromName; String Sender ' s name (opt)
var $ToName; String Recipient ' s name (opt)
var $Body; String Body Copy
var $Attachment; Attachment (optional)
var $AttachmentType;
var $Socket;
var $Line;
var $Status;

function Smtp ($Server = "localhost", $Port = Smtpport)
{
return $this->open ($Server, $Port);
}

function SmtpMail ($FromEmail, $FromName, $ToEmail, $ToName, $Subject, $Body, $Attachment =null, $AttachmentType = "TEXT")
{
$this->subject = $Subject;
$this->toname = $ToName;

$this->fromname = $FromName;
$this->body = $Body;

$this->attachment = $Attachment;
$this->attachmenttype = $AttachmentType;

if ($this->helo () = = False) {
return false;
}
if ($this->mailfrom ($FromEmail) = = False) {
return false;
}
if ($this->rcptto ($ToEmail) = = False) {
return false;
}
if ($this->body () = = False) {
return false;
}
if ($this->quit () = = False) {
return false;
}
}

function Open ($Server, $Port)
{

$this->socket = Fsockopen ($Server, $Port);
if ($this->socket < 0) return false;

$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}


function Helo ()
{
if (fputs ($this->socket, "Helorn") < 0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}

function Ehlo ()
{

/* Yes, let's use the "Helo" for now. Until we need the
Extra Func ' s [Unk]
*/
if (fputs ($this->socket, "Helo localhostrn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}


function Mailfrom ($FromEmail)
{

if (fputs ($this->socket, "MAIL from: < $FromEmail >rn") <0) {
return false;
}

$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;

return true;
}

function Rcptto ($ToEmail)
{

if (fputs ($this->socket, "RCPT to: < $ToEmail >rn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "2") return false;
return true;
}

function Body ()
{
$FileSize = 0;
$Attachment = null;
$fp = null;

$buffer = sprintf ("From:%SRNTO:%SRNSUBJECT:%SRN", $this->fromname, $this->toname, $this->subject);

if (fputs ($this->socket, "Datarn") <0) {
return false;
}
$this->line = fgets ($this->socket, 1024);

$this->status["Lastresult"] = substr ($this->line, 0, 1);
$this->status["lastresulttxt"] = substr ($this->line, 0, 1024);

if ($this->status["Lastresult"] <> "3") return false;

if (fputs ($this->socket, $buffer) <0) {
return false;
}


if ($this->attachment = = null) {

if (fputs ($this->socket, "Mime-version:1.0rncontent-type:text/plain; Charset=iso-8859-1rncontent-transfer-encoding:7bitrnrn ") <0) {
return false;
}
if (fputs ($this->socket, "$this->bodyrnrn") <0) {
return false;
}

if (fputs ($this->socket, ". Rn") <0) {
return false;
}

$this->


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.