In linux, php cannot send emails.
Last Update:2017-05-14
Source: Internet
Author: User
In linux, php Mail cannot be sent. I use the following code to send an email in linux, prompting that the email has been sent successfully, but in fact no email has been sent. Let's see why.
Code for the mail. inc. php file:
Mail. inc. php started -------------------------------------------------
// Email
Class smtp
{
/* Public Variables */
Var $ smtp_port;
Var $ time_out;
Var $ host_name;
Var $ log_file;
Var $ relay_host;
Var $ debug;
Var $ auth;
Var $ user;
Var $ pass;
Var $ from;
Var $ test = "test ";
/* Private Variables */
Var $ sock;
/* Constractor */
Function smtp ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass, $ from)
{
$ This-> debug = FALSE;
$ This-> smtp_port = $ smtp_port;
$ This-> relay_host = $ relay_host;
$ This-> time_out = 30; // is used in fsockopen ()
#
$ This-> auth = $ auth; // auth
$ This-> user = $ user;
$ This-> pass = $ pass;
$ This-> from = $ from;
#
$ This-> host_name = "localhost"; // is used in HELO command
$ This-> log_file = "";
$ This-> sock = FALSE;
}
/* Main Function */
Function sendmail ($ to, $ fromname = "", $ subject = "", $ body = "", $ mailtype, $ cc = "", $ bcc = "", $ additional_headers = "")
{
$ Mail_from = $ this-> get_address ($ this-> strip_comment ($ this-> 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. = "To:". $ to. "\ r \ n ";
If ($ cc! = ""){
$ Header. = "Cc:". $ cc. "\ r \ n ";
}
If ($ fromname! = "") $ Header. = "From:". $ fromname. "<". $ this-> from. "> \ r \ n ";
If ($ fromname = "") $ header. = "From: <". $ this-> from. "> \ 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 ";
$ Utfheader = iconv ("GBK", "UTF-8 // IGNORE", $ header );
List ($ msec, $ sec) = explode ("", microtime ());
$ Header. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> \ r \ n ";
$ TO = explode (",", $ this-> strip_comment ($ ));
If ($ cc! = ""){
$ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ cc )));
}
If ($ bcc! = ""){
$ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ bcc )));
}
$ Sent = TRUE;
Foreach ($ TO as $ rcpt_to ){
$ Rcpt_to = $ this-> get_address ($ rcpt_to );
If (! $ This-> smtp_sockopen ($ rcpt_to )){
$ This-> log_write ("Error: Cannot send email to". $ rcpt_to. "\ n ");
$ Sent = FALSE;
Continue;
}
If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ utfheader, $ body )){
$ This-> log_write ("E-mail has been sent to <". $ rcpt_to. "> \ n ");
} Else {
$ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> \ n ");
$ Sent = FALSE;
}
Fclose ($ this-> sock );
$ This-> log_write ("Disconnected from remote host \ n ");
}
Return $ sent;
}
/* Private Functions */
Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
{
If (! $ This-> smtp_putcmd ("HELO", $ helo )){
Return $ this-> smtp_error ("sending HELO command ");
}
# Auth
If ($ this-> auth ){
If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
Return $ this-> smtp_error ("sending HELO command ");
}
If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
Return $ this-> smtp_error ("sending HELO command ");
}
}
#
If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ")){
Return $ this-> smtp_error ("sending mail from command ");
}
If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ")){
Return $ this-> smtp_error ("sending rcpt to command ");
}
If (! $ This-> smtp_putcmd ("DATA ")){
Return $ this-> smtp_error ("sending DATA command ");
}
If (! $ This-> smtp_message ($ header, $ body )){
Return $ this-> smtp_error ("sending message ");
}
If (! $ This-> smtp_eom ()){
Return $ this-> smtp_error ("sending . [EOM] ");
}
If (! $ This-> smtp_putcmd ("QUIT ")){
Return $ this-> smtp_error ("sending QUIT command ");
}
Return TRUE;
}
Function smtp_sockopen ($ address)
{
If ($ this-> relay_host = ""){
Return $ this-> smtp_sockopen_mx ($ address );
} Else {
Return $ this-> smtp_sockopen_relay ();
}
}
Function smtp_sockopen_relay ()
{
$ This-> log_write ("Trying to". $ this-> relay_host. ":". $ this-> smtp_port. "\ n ");
$ This-> sock = @ fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
If (! ($ This-> sock & $ this-> smtp_ OK ())){
$ This-> log_write ("Error: Cannot connenct to relay host". $ this-> relay_host. "\ n ");
$ This-> log_write ("Error:". $ errstr. "(". $ errno. ") \ n ");
Return FALSE;
}
$ This-> log_write ("Connected to relay host". $ this-> relay_host. "\ n ");
Return TRUE;
}
Function smtp_sockopen_mx ($ address)
{
$ Domain = ereg_replace ("^. + @ ([^ @] +) $", "\ 1", $ address );
If (! @ Getmxrr ($ domain, $ MXHOSTS )){
$ This-> log_write ("Error: Cannot resolve MX \" ". $ domain." \ "\ n ");
Return FALSE;
}
Foreach ($ MXHOSTS as $ host ){
$ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port. "\ n ");
$ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
If (! ($ This-> sock & $ this-> smtp_ OK ())){
$ This-> log_write ("Warning: Cannot connect to mx host". $ host. "\ n ");
$ This-> log_write ("Error:". $ errstr. "(". $ errno. ") \ n ");
Continue;
}
$ This-> log_write ("Connected to mx host". $ host. "\ n ");
Return TRUE;
}
$ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS). ") \ n ");
Return FALSE;
}
Function smtp_message ($ header, $ body)
{
Fputs ($ this-> sock, $ header. "\ r \ n". $ body );
$ This-> smtp_debug ("> ". str_replace ("\ r \ n", "\ n ". ">", $ header. "\ n> ". $ body. "\ n> "));
Return TRUE;
}
Function smtp_eom ()
{
Fputs ($ this-> sock, "\ r \ n. \ r \ n ");
$ This-> smtp_debug (". [EOM] \ n ");
Return $ this-> smtp_ OK ();
}
Function smtp_ OK ()
{
$ Response = str_replace ("\ r \ n", "", fgets ($ this-> sock, 512 ));
$ This-> smtp_debug ($ response. "\ n ");
If (! Ereg ("^ [23]", $ response )){
Fputs ($ this-> sock, "QUIT \ r \ n ");
Fgets ($ this-> sock, 512 );
$ This-> log_write ("Error: Remote host returned \" ". $ response." \ "\ n ");
Return FALSE;
}
Return TRUE;
}
Function smtp_putcmd ($ cmd, $ arg = "")
{
If ($ arg! = ""){
If ($ cmd = "") $ cmd = $ arg;
Else $ cmd = $ cmd. "". $ arg;
}
Fputs ($ this-> sock, $ cmd. "\ r \ n ");
$ This-> smtp_debug (">". $ cmd. "\ n ");
Return $ this-> smtp_ OK ();
}
Function smtp_error ($ string)
{
$ This-> log_write ("Error: Error occurred while". $ string. ". \ n ");
Return FALSE;
}
Function log_write ($ message)
{
$ This-> smtp_debug ($ message );
If ($ this-> log_file = ""){
Return TRUE;
}
$ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
If (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, ""))){
$ This-> smtp_debug ("Warning: Cannot open log file \" ". $ this-> log_file." \ "\ n ");
Return FALSE ;;
}
Flock ($ fp, LOCK_EX );
Fputs ($ fp, $ message );
Fclose ($ fp );
Return TRUE;
}
Function strip_comment ($ address)
{
$ Comment = "\ ([^ ()] * \)";
While (ereg ($ comment, $ address )){
$ Address = ereg_replace ($ comment, "", $ address );
}
Return $ address;
}
Function get_address ($ address)
{
$ Address = ereg_replace ("([\ t \ r \ n]) +", "", $ address );
$ Address = ereg_replace ("^. * <(. +)>. * $", "\ 1", $ address );
Return $ address;
}
Function smtp_debug ($ message)
{
If ($ this-> debug ){
Echo $ message;
}
}
}
?>
Mail. inc. php Ended ------------------------------------------------
This is the mail. php file code:
-------------------------------------------------------
Include_once ("mail. inc. php ");
$ Smtp = new smtp ("smtp.qq.com", 25, true, "tangfengme@163.com", "781203tf", "593894955@qq.com"); // sender mailbox information
$ Smtp-> debug = false; // whether to display the sent debugging information FALSE or TRUE
$ Mailto = "53894955@qq.com"; // recipient's mailbox
$ Mailsubject = "web Message ";
$ Mailfrom = "HR ";
$ Mailbody = "Name =". $ _ POST ["q1"]."
";
$ Mailbody = $ mailbody. "Phone No =". $ _ POST ["q2"]."
";
$ Mailbody = $ mailbody. "Suburb =". $ _ POST ["q3"]."
";
$ Mailbody = $ mailbody. "email =". $ _ POST ["email"]."
";
$ Mailbody = $ mailbody. "Message =". $ _ POST ["q4"]."
";
// Other form items and so on
$ Mailtype = "HTML"; // The email format (HTML/TXT). TXT is a text email.
$ Mailsubject = '=? UTF-8? B? '. Base64_encode ($ mailsubject ).'? = '; // Email subject
$ Mailfrom = '=? UTF-8? B? '. Base64_encode ($ mailfrom ).'? = '; // Sender
$ Smtp-> sendmail ($ mailto, $ mailfrom, $ mailsubject, $ mailbody, $ mailtype );
Echo"