PHP Mail Class

Source: Internet
Author: User
Tags getmessage

Write a PHP socket to send mail class, easy to use, when using the PHP program to send mail,

In the 163 server, you can also verify that 163 mailboxes exist in the RCPT command, and there are many useful

I haven't thought about it for the moment.

Note that a friend in need can refer to the following

<?php
Class Mail {

/**
* These can be sent by email
*/
Public $host;
Public $port;
Public $user;
Public $password;
Public $mail _form;
Public $rcpt;

Public $body;

/**
* Information attached to the header
*/
Public $to _name;
Public $from _name;
Public $subject;
Public $html;

Public $connection;

Public $msg = Array ();


Initialization of parameters
Public function __construct ($conf, $RCPT, $header, $body, $html = True) {

try {

$this->host = $conf [' Host '];
$this->port = $conf [' Port '];
$this->user = $conf [' user '];
$this->password = $conf [' Password '];

$this->rcpt = $RCPT;

$this->to_name = $header [' To_name '];
$this->from_name = $header [' From_name '];
$this->subject = $header [' Subject '];
$this->html = $html;

$this->body = Base64_encode ($body);

} catch (Exception $e) {
throw new Exception ($e->getmessage ());
}

}



Public Function connect () {
$this->connection = @fsockopen ($this->host, ' + ', $errno, $ERRSTR, 10);
if (! $this->connection) {
throw new Exception (' Connect failed! ');
}
$greet = $this->callback_code ();

}

Public Function helo () {
if ($this->is_connect () &&
$this->send_data (' HELO '. $this->host) &&
$this->callback_code () = = 250
) {
return true;
} else {
throw new Exception ($this->get_callback_msg_lastest ());

}
}

Public Function Send_data ($cmd) {
if (Is_resource ($this->connection)) {
Return @fwrite ($this->connection, $cmd. " \ r \ n ", strlen ($cmd) + 2);
}
}

Public Function auth () {
if ($this->is_connect () &&
$this->send_data (' AUTH LOGIN ') && $this->callback_code () = = 334 &&
$this->send_data (Base64_encode ($this->user)) && $this->callback_code () = = 334 &&
$this->send_data (Base64_encode ($this->password)) && $this->callback_code () = = 235) {
return true;
} else {
throw new Exception ($this->get_callback_msg_lastest ());
}
}

Public Function Mail () {

if ($this->is_connect () &&
$this->send_data ("MAIL from:< $this->user>") &&
$this->callback_code () = = 250
) {
return true;
} else {
throw new Exception ($this->get_callback_msg_lastest ());
}
}

Public Function Is_connect () {
Return Is_resource ($this->connection);
}

Public Function Callback_code () {
if ($this->is_connect ()) {
$msg = fgets ($this->connection);
if (!empty ($msg)) {
$code = substr ($msg, 0, Strpos ($msg, "));
} else {
Return ';
}
$this->msg[] = $msg;
return $code;
}
}

Public Function get_callback_msg_lastest () {
Return End ($this->msg);
}

Public Function Rcpt ($RCPT) {
if ($this->is_connect () &&
$this->send_data ("RCPT to:< $rcpt >") &&
$this->callback_code () = = 250
) {
return true;
} else {
throw new Exception ($this->get_callback_msg_lastest ());
}
}

Public Function data () {
if ($this->is_connect () &&
$this->send_data (' data ') &&
$this->callback_code () = = 354) {
return true;
} else {
throw new Exception ($this->get_callback_msg_lastest ());
}
}


Public Function Send_mail () {

try {

$this->connect ();
$this->helo ();
$this->auth ();
$this->mail ();

if (Is_array ($this->rcpt)) {
foreach ($this->rcpt as $RCPT) {
$this->rcpt ($RCPT);
}

} else {
$this->rcpt ($this->rcpt);
}

$this->data ();

$header = Str_replace ("\ r \ n". '. ', ' \ r \ n '. '. ', Trim (implode ("\ r \ n", $this->get_header ())));
$body = Str_replace ("\ r \ n". '. ', ' \ r \ n '. '.. ', $this->body);
$body = substr ($body, 0, 1) = = '. '? ‘.‘ . $body: $body;

$this->send_data ($header);
$this->send_data (");
$this->send_data ($body);
$this->send_data ('. ');

if ($this->callback_code ()! = 250) {
throw new Exception (' Send mail falied! ');
}
return true;
} catch (Exception $e) {
throw new Exception ($e->getmessage ());
}


}



can only detect email address on the same mail server
Public Function is_email_exist () {




}


Public Function Get_header () {

$header = Array ();
$content _type = $this->html? ' text/html; ': ' Text/plain; ';

$headers [] = ' Date: '. Gmdate (' D, J M Y H:i:s '). ' +0000 ';
$to _mail = Is_array ($this->rcpt)? Implode (', ', $this->rcpt): $this->rcpt;
$headers [] = ' to: '. ' =? UTF8? B? '. Base64_encode ($this->to_name). '? = '. ' < '. $to _mail. ' > ';
$headers [] = ' from: '. ' =?utf8? B? '. Base64_encode ($this->from_name). '? = '. ' < '. $this->user. ' > ';
$headers [] = ' Subject: '. ' =?utf8? B? '. Base64_encode ($this->subject). '? = ';
$headers [] = ' content-type: '. $content _type. ' Charset=utf8; Format=flowed ';
$headers [] = ' content-transfer-encoding:base64 ';
$headers [] = ' content-disposition:inline ';

return $headers;
}



}


/**
* Encapsulating classes with functions
*/
function Send_mail ($conf, $RCPT, $header, $body) {

$mail = new Mail ($conf, $RCPT, $header, $body);

if ($mail->send_mail ()) {
Echo ' send email successfully! ';
} else {
Echo ' falied! ';
}


}


$conf = Array (
' Host ' = ' smtp.163.com ',
' Port ' = ' 25 ',
' User ' = ' [email protected] ',
' Password ' = ' zeiwei123 ',
);

$RCPT = Array (
' [email protected] ',
' [Email protected] '
);

$header = Array (
' To_name ' = ' Willstang ',
' From_name ' = ' Sinawill ',
' Subject ' = ' TST of REO ',
);

$body = '


$mail = new Mail ($conf, $RCPT, $header, $body);

$mail->send_mail ();


Echo ' <pre> ';
Print_r ($mail->msg);

Familiar with the mail sending process, its STMP protocol,

There are some details sent, such as: the content is divided into two parts, part of the head, the other part is the body of the email,

PHP Mail Class

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.