Joke Collection Website - Blessing messages - The novice wants to make a PHP form submission and send it to the designated mailbox. Please give me your advice.

The novice wants to make a PHP form submission and send it to the designated mailbox. Please give me your advice.

First, it contains a mail sending class require ("sendmail.php");

Then you have to have a server to send mail (you can register at 163 at will, for example, the registered server is aa@ 163.com, and the password is aa 123), and then the code to send mail. Let me give you an example I used.

$ SMTP server = " SMTP . 163 . com "; //SMTP server you selected

$ smtpserverport = 25//SMTP server port

$ smtpusermail = " aa @ 163 . com "; //User mailbox of //SMTP server

$ SMTP email to = "bb @ qq.com// inbox

$ smtpuser = " aa @ 163 . com "; //User account of //SMTP server

$ smtppass = " aa 123 "; //User password of //SMTP server

$MailBody= "Name:"。 $ _ POST[' Name ']; //E-mail content (if the form you submitted is Name)

$ TERM subject = @ iconv ("UTF-8 ","GB 23 12 ","XX website-problem submission "); //If your page is UTF 8, you need to transcode it here.

$ mailbody = $ MailBody// email content

$ mailtype = "HTML// mail format (HTML/TXT), TXT is text mail.

$smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $ smtppass); //true indicates that authentication is used, otherwise authentication is not used.

$ SMTP-& gt; Debug = FALSE// Whether to display debugging information, send true, send false, and don't send.

$ SMTP-& gt; sendmail($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$ mail type);

SendMail.php code:

Smtp class

{

/* public variable */

Var $ smtp _ port// mail server port

Var $ time _ out// timeout

Var $ host _ name// host name

Var $ log _ file// log file

var $ relay _ host//

var $ debug

var $ auth

Var $ users; //User name

Var $ pass// password

var $ sock

/* constructor */

Function smtp($relay_host, $smtp_port, $auth, $user, $pass)

{

// echo $relay_host。 " ".$smtp_port。 " ".$auth。 "".$ user. " ".$ pass

$ this-& gt; debug = FALSE

$ this-& gt; smtp _ port = $ smtp _ port

$ this-& gt; Relay host = $ relay host;

$ this-& gt; Time _ out = 30// Used in fsockopen ()

$ this-& gt; auth = $ auth//auth

$ this-& gt; user = $ user

$ this-& gt; pass = $ pass

$ this-& gt; Host _ name = "localhost// used in the HELO command.

$ this-& gt; log _ file =

$ this-& gt; sock = FALSE

}

/* Main function */

Sendmail($to, $from, $subject = "",$body = "",$mailtype, $cc = "",$bcc = "",$additional_headers = "").

{

$ mail _ from = $ this-& gt; get _ address($this-& gt; strip _ comment($ from));

$ body = ereg_replace("(^|(\r\n))(\.)", "\ 1.\3”,$ body);

$header。 = " MIME-Version: 1.0 \ r \ n ";

if($mailtype=="HTML"){

$header。 = " Content-Type:text/html \ r \ n ";

}

$header。 = "Here:". $ to. ”\ r \ n”;

If ($cc! = "") {

$header。 = "CC:". $cc。 ”\ r \ n”;

}

$header。 = " From:$ From & lt; . $from。" & gt\ r \ n ";

$header。 = "Subject:". $subject。 ”\ r \ n”;

$header。 = $ additional _ headers

$header。 = "Date:". Date ("r"). ”\ r \ n”;

$header。 = "X-Mailer:By Redhat (PHP/"。 phpversion()。 ”)\ r \ n”;

list($msec,$sec) = explode(",microtime());

$header。 = "Message ID: <; . Date ("YmdHis", $sec). " ."。 ($msec* 1000000)。" ."。 $mail_from。" & gt\ r \ n ";

$TO = explode(",",$ this-& gt; strip _ comment($ to));

If ($cc! = "") {

$TO = array_merge($TO,explode(",",$ this-& gt; strip _ comment($ cc)));

}

If ($ bcc! = "") {

$TO = array_merge($TO,explode(",",$ this-& gt; strip _ comment($ bcc)));

}

$ sent = TRUE

foreach ($TO as $rcpt_to) {

$ rcpt _ to = $ this-& gt; get _ address($ rcpt _ to);

If (! $ this-& gt; smtp_sockopen($rcpt_to)) {

$ this-& gt; Log_write ("Error: Unable to send e-mail to". $rcpt_to。 " \ n ");

$ sent = FALSE

Continue;

}

If ($ this->; SMTP _ send($ this-& gt; Host name, $ e-mail sender, $ recipient, $ title, $ body) {

$ this-& gt; Log_write ("email has been sent to < $rcpt_to。" & gt\ n ");

}

Otherwise {

$ this-& gt; Log_write ("Error: Unable to send e-mail to < $rcpt_to。" & gt\ n ");

$ sent = FALSE

}

fclose($ this-& gt; Socks);

$ this-& gt; Log_write ("Disconnected from the remote host \ n");

}

return $ sent

}

/* Private function */

The function smtp_send($helo, $from, $to, $header, $body = "").

{

If (! $ this-& gt; smtp_putcmd("HELO ",$helo)) {

return $ this-& gt; Smtp_error ("send HELO command");

}

If ($ this->; auth){

If (! $ this-& gt; smtp_putcmd("AUTH LOGIN ",base64 _ encode($ this-& gt; User)) {

return $ this-& gt; Smtp_error ("send HELO command");

}

If (! $ this-& gt; smtp_putcmd(",base64 _ encode($ this-& gt; Pass)) {

return $ this-& gt; Smtp_error ("send HELO command");

}

}

If (! $ this-& gt; smtp_putcmd("MAIL "," FROM:& lt; . $from。" >)) {

return $ this-& gt; Smtp_error ("Send mail from command");

}

If (! $ this-& gt; Smtp_putcmd("RCPT ","Recipient: < $ to. " >)) {

return $ this-& gt; Smtp_error ("Send rcpt to command");

}

If (! $ this-& gt; smtp_putcmd("DATA")) {

return $ this-& gt; Smtp_error ("send data command");

}

If (! $ this-& gt; smtp_message($header,$body)) {

return $ this-& gt; Smtp_error ("send mail");

}

If (! $ this-& gt; smtp_eom()) {

return $ this-& gt; Smtp_error ("send & ltCR & gt& ltLF & gt. & ltCR & gt& ltLF & gt[EOM]”;

}

If (! $ this-& gt; smtp_putcmd("QUIT")) {

return $ this-& gt; Smtp_error ("send exit command");

}

Return TRUE

}

Function smtp_sockopen($address)

{

If ($ this->; relay_host == "") {

return $ this-& gt; SMTP _ sock open _ MX($ address);

} Otherwise {

return $ this-& gt; SMTP _ sock open _ relay();

}

}

The function smtp_sockopen_relay ()

{

$ this-& gt; Log_write ("attempt". $ this-& gt; Relay _ host. ":".$ this-& gt; smtp_port。 " \ n ");

$ this-& gt; sock = @ fsockopen($ this-& gt; relay_host,$ this-& gt; smtp_port,$errno,$errstr,$ this-& gt; time _ out);

If (! ($ this-& gt; Socks & amp& amp$ this-& gt;; smtp_ok())) {

$ this-& gt; Log_write ("Error: Unable to connect to the relay host". $ this-& gt; Relay _ host. " \ n ");

$ this-& gt; Log_write ("Error:". $errstr。 " (".$errno”)\ n”);

Returns FALSE

}

$ this-& gt; Log_write ("connect to the relay host". $ this-& gt; Relay _ host. " \ n ");

Return TRUE

}

Function smtp_sockopen_mx($address)

{

$domain = ereg_replace("^.+@([^@]+)$”,“\ 1”,$ address);

If (! @getmxrr($domain,$MXHOSTS)) {

$ this-& gt; Log_write ("Error: Unable to parse MX \"). $domain。 \ " \ n ");

Returns FALSE

}

foreach ($MXHOSTS as $host) {

$ this-& gt; Log_write ("attempt". $host。 ":".$ this-& gt; smtp_port。 " \ n ");

$ this-& gt; sock = @fsockopen($host,$ this-& gt; smtp_port,$errno,$errstr,$ this-& gt; time _ out);

If (! ($ this-& gt; Socks & amp& amp$ this-& gt;; smtp_ok())) {

$ this-& gt; Log_write ("Warning: Unable to connect to mx host". $host。 " \ n ");

$ this-& gt; Log_write ("Error:". $errstr。 " (".$errno”)\ n”);

Continue;

}

$ this-& gt; Log_write ("connect to mx host". $host。 " \ n ");

Return TRUE

}

$ this-& gt; Log_write ("Error: Unable to connect to any mx host (". Implosion (",",$MXHOSTS). ”)\ n”);

Returns FALSE

}

Function smtp_message($header, $body)

{

fputs($ this-& gt; Socks, $ head. \r\n。 $ body);

$ this-& gt; smtp_debug(" >。 str_replace("\r\n "," \n "。" >,$header。 " \ n & gt"。 $body。 " \ n & gt"));

Return TRUE

}

Function smtp_eom ()

{

fputs($ this-& gt; sock," \ r \ n . " \ r \ n ");

$ this-& gt; smtp_debug("。 【EOM】\ n”);

return $ this-& gt; SMTP _ ok();

}

The function smtp_ok ()

{

$response = str_replace("\r\n ","",fgets($ this-& gt; sock,5 12));

$ this-& gt; SMTP _ debug($response。 " \ n ");

If (! ereg("^[23]",$response)) {

fputs($ this-& gt; sock," QUIT \ r \ n ");

fgets($ this-& gt; Socks, 512);

$ this-& gt; Log_write ("Error: The remote host returned \" ". $response。 ”\ " \ n ");

Returns FALSE

}

Return TRUE

}

Function smtp_putcmd($cmd, $arg = "")

{

if ($arg! = "") {

if($ cmd = = " ")$ cmd = $ arg;

else $cmd = $cmd。 " ".$ arg

}

fputs($ this-& gt; Socks, cmd. " \ r \ n ");

$ this-& gt; smtp_debug(" >。 $cmd。" \ n ");

return $ this-& gt; SMTP _ ok();

}

Function smtp_error($string)

{

$ this-& gt; Log_write("Error: the error occurred at ". $string。 ".\ n ");

Returns FALSE

}

Function log_write($message)

{

$ this-& gt; SMTP _ debug($ message);

If ($ this->; log_file == "") {

Return TRUE

}

$message = date("M d H:i:s ")。 Get the current user (). "[".getmypid()。 "]: ".$ message

If (! @ file _ exists($this-& gt; log_file) ||! ($ FP = @ fopen($ this-& gt; log_file," a"))) {

$ this-& gt; Smtp_debug ("Warning: Unable to open the log file \" ". $ this-& gt; log_file。 \ " \ n ");

Returns FALSE;;

}

flock($fp,LOCK _ EX);

fputs($fp,$ message);

fclose($ FP);

Return TRUE

}

Function Bar _ Comment ($ Address)

{

$ comment = " \([^()]*\)";

while (ereg($comment,$address)) {

$address = ereg_replace($comment,"",$ address);

}

Returns the $ address;

}

Function get_address($address)

{

$ address = ereg _ replace("([\ t \ r \ n])+","",$ address);

$address = ereg_replace("^.* & lt(.+)& gt; . *$ "," \ 1 ",$ address);

Returns the $ address;

}

Function smtp_debug($message)

{

If ($ this->; Debugging) {

//echo $ message;

}

}

}