I guess another Mail class....

the mail() function

Moderators: macek, egami, gesf

Alex
New php-forum User
New php-forum User
Posts: 180
Joined: Fri Mar 07, 2003 4:59 pm
Contact:

I guess another Mail class....

Postby Alex » Mon May 24, 2004 4:46 pm

Hey guys,
Couldn't sleep last knight, stupid storm woke me up at like 3, so i wrote a random class, a mail class with a capability of attatching a file to it. Hope it works, and someone has a use for it... Actually the idea came from an e-mail subscription, but there were parts that i had to modify and rewrite, if anyone has comments or anything that they can think of to add on to this class (or correct it, because i din't have time to go through it after i wrote it and there might be mistakes because i was sleepy :sleep: , it was early, i had to leave at 6 and i couldn't sleep) please feel free to share.

Code: Select all

<?php

class mail
{

$from;
$to;
$header;
$body;

function mail()
{
$this -> from = "";
$this -> to = "";
$this -> body = "";
$this -> header = Array();
$this -> subject = "";
}

function attach_file($file_name = "" , $file_content,$encoding_type = "application/octet-stream")
{

$this -> header[] = array(
"name" => $file_name,
"content" => $file_content,
"encode" => $encoding_type);
}

function build_letter($header)
{
$letter = $header["content"];
if ($header["encode"] != "text/plain")
{
$letter = chunk_split(base64_encode($letter));
$encoding = "base64";
}
else
$encoding = $header["encode"];

return "Content-Type: ".$header["encode"]. ($header["name"]? ";
$name = \"".$header["name"]."\"" : "") ."\r\nContent-Transfer-Encoding: $encoding\r\n\r\n$letter\n";
}

function set_multipart_mail()
{
$boundry = 'b'.md5(uniqid(time()));
$multipart = "Content-Type: multipart/mixed;
boundry =$boundary\n\nThis is a MIME encoded letter\r\n\r\n--$boundry";

for($step = sizeof($this->header)-1; $step >=0; $step--)
{
$multipart .= "\r\n".$this->build_letter($this->header[$step])."--$boundary";
}

return $multipart .= "--\r\n";
}

function get_full_message()
{
$mime = "";

if (!empty($this->from)):
$mime .= "From: ".$this->from." \r\n";

if (!empty($this->body)):
{
$this -> attach_file("",$this->body,"text/plain");
$mime .= "MIME-Version: 1.0\r\n".$this->set_multipart_mail();
}

return $mime;
}

function send_mail()
{

$mime = $this -> get_full_message(false);
mail($this->to,$this->subject,"",$mime);
}

}

?>



P.S. Apparently [php] tags are not working anymore... I miss them, they were so useful in the past.

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: I guess another Mail class....

Postby WiZARD » Mon May 24, 2004 11:31 pm

very simly and usefull...
propose add some function:
read mail header
manipulate by SMTP.

Alex
New php-forum User
New php-forum User
Posts: 180
Joined: Fri Mar 07, 2003 4:59 pm
Contact:

Postby Alex » Tue May 25, 2004 5:40 am

I will add some SMTP functions in a little while (today or tomorrow maybe), but dunno about read mail header function, that i'll have to work on later maybe.

Alex
New php-forum User
New php-forum User
Posts: 180
Joined: Fri Mar 07, 2003 4:59 pm
Contact:

Postby Alex » Tue May 25, 2004 4:33 pm

Hey i was putting around on the net, and i think i found another way of checking an email address for validity... here is a new addon function to my class

Code: Select all

function mail_check($email){

if(ereg("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$", $email)
die('Email address is not valid');

list($user, $host) =explode("@", $email);

$fp = fsockopen ($host, 25);

set_socket_blocking ($fp, true);

fputs ($fp, "Hello Local\n");
fgets ($fp, 2000);
fgets ($fp, 2000);
fputs ($fp, "Mail From:<$user@$host> \n");
fgets ($fp, 2000);
fputs ($fp, "RCPT to:aetos<$user@$host> \n");

$result= fgets ($fp, 2000);
$st= substr($result,0,3);

if ($st==250)
{
echo"Email address is valid";
}

else
die('The address is not valid');

}


I am still working on those SMTP functions...

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Tue May 25, 2004 7:50 pm

you may and will find this link intersting and useful


http://www.faqs.org/rfcs/rfc821.html

Alex
New php-forum User
New php-forum User
Posts: 180
Joined: Fri Mar 07, 2003 4:59 pm
Contact:

Postby Alex » Wed May 26, 2004 7:58 am

Ok, I came up with a list of POP3 and SMTP functions that will be really helpful in this class:
POP3 functions

pop3_open
pop3_close
pop3_user_and_pass
pop3_statistics
pop3_list
pop3_retrieve
pop3_delete

SMTP functions
smtp_open
smtp_helo
smtp_from
smtp_to
smtp_data
smtp_close

ok here comes some code:

Code: Select all

//POP3 functions
function pop3_test($pop3)
{
global $POP3_STATUS;
$test= fgets($pop3, 1024);
$POP3_STATUS[$pop3]["result"] = substr($test, 0, 1);
$POP3_STATUS[$pop3]["resulttext"] = substr($test, 0, 1024);

if($POP3_STATUS[$pop3]["result"]<>"+")
return false;

return 1;
}

function pop3_open($server, $port)
{
global $POP3_STATUS;
$pop3=fsockopen($server, $port);

if($pop3<0)
return false;

if (pop3_test($pop3)==1)
return 1;
else
return 0;
}

function pop3_user_and_pass($pop3, $user, $pass)
{
global $POP3_STATUS;

fputs($pop3, "USER $user\r\n");
if (pop3_test($pop3)<>1)
return 0;

fputs($pop3, "PASS $pass\r\n");
if (pop3_test($pop3)<>1)
return 0;

return 1;
}

function pop3_statistics($pop3)
{
global $POP3_STATUS;

fputs($pop3, "STAT\r\n");

if (pop3_test($pop3)<>1)
return 0;

if(!eregi("+OK(.*)(.*)", $test, $regs))
return false;

return $regs[1];
}

function pop3_list($pop3)
{
global $POP3_STATUS;

fputs($pop3, "LIST\r\n");
if (pop3_test($pop3)<>1)
return 0;

$i=0;
while (substr($line = fgets($pop3, 1024), 0, 1) <> ".")
{
$text[$i]=$test;
$i++;
}
$text["count"] = $i;
return $text;
}

function pop3_retrieve($pop3, $nr)
{
global $POP3_STATUS;

fputs($pop3, "RETR $nr\r\n");
if (pop3_test($pop3)<>1)
return 0;
while(substr($line = fgets($pop3, 1024), 0, 1) <> ".")
{
$data[$i] = $line;
$i++;
}
$data["count"]=$i;
return $data;
}

function pop3_delete($pop3, $nr)
{
global $POP3_STATUS;
fputs($pop3, "DELE $nr\r\n");
if (pop3_test($pop3)==1)
return 1;
else
return 0;
}

function pop3_close($pop3)
{
global $POP3_STATUS;
fputs($pop3, "QUIT\r\n");
if (pop3_test($pop3)==1)
return 1;
else
return 0;
}

//SMTP functions

function smtp_test($smtp)
{
global $SMTP_STATUS;
$test=fgets($smtp, 1024);
$SMTP_STATUS[$smtp]["result"] = substr($line, 0, 1);
$SMTP_STATUS[$smtp]["resulttext"] = substr($line, 0, 1024);
if($SMTP_STATUS[$smtp]["result"]<>"2")
return 0;
return 1;
}

function smtp_open($server, $host)
{
global $SMTP_STATUS;
$smtp=fsockopen($server, $port);
if ($smtp < 0)
return 0;
if(smtp_test($smtp)==0)
return 0;
return $smtp;
}

function smtp_helo($smtp)
{
global $SMTP_STATUS;
//we will use helo for now
//'localhost' should work too
fputs($smtp, "helo localhost\r\n");
if(smtp_test($smtp)==0)
return 0;
return 1;
}

function smtp_from($smtp, $from)
{
global $SMTP_STATUS;
fputs($smtp, "MAIL FROM:<$from>\r\n");
if(smtp_test($smtp)==0)
return 0;
return 1;
}

function smtp_to($smtp, $to)
{
global $SMTP_STATUS;
fputs($smtp, "RCPT TO:<$to>\r\n");
if(smtp_test($smtp)==0)
return 0;
return 1;
}

function smtp_data($smtp, $subject, $data)
{
global $SMTP_STATUS;
fputs($smtp, "DATA\r\n");
if(smtp_test($smtp)==0)
return 0;

fputs($smtp, "Mime-Version: 1.0\r\n");
fputs($smtp, "Subject: $subject\r\n");
fputs($smtp, "$data\r\n\r\n");
fputs($smtp, ".\r\n");
$test=fgets($smtp, 1024);
if(substr($line, 0, 1) <> "2")
return 0;
return 1;
}

function smtp_quit($smtp)
{
global $SMTP_STATUS;

fputs($smtp, "QUIT\r\n");
if(smtp_test($smtp)==0)
return 0;
return 1;
}


I just ran out of time for now. If anyone wants, I can give an example of these functions interacting. I hope they work...


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest