Easy to send email samples using the SMTP class in PHP

Source: Internet
Author: User
Tags auth ereg explode ini php file server port

When you are still in the tangle php built-in mail () function can not send mail, then you are very lucky now, this article can help you!

PHP uses the SMTP class to send mail is very good, I used a long time, basically did not have a problem. This blog backstage, when the blogger reply to the message, will automatically send a new reply to users of the message is also used in this method to achieve.

The way the SMTP class sends mail is actually very simple and stable, and the class is already written by someone else, and you just need to call on the line. A few lines of simple configuration can send mail, is not very looking forward to try it!

email.class.php file

The code is as follows Copy Code


<?php
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;

* Private Variables * *
var $sock;

* Constractor * *

function smtp ($relay _host = "", $smtp _port = =, $auth = False, $user, $pass)

{

$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->host_name = "localhost"; is used in HELO command
$this->log_file = "";

$this->sock = FALSE;

}

/* Main Function/*

function SendMail ($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $BCC = "", $additional _headers = "")

{

$mail _from = $this->get_address ($this->strip_comment ($from));

$body = ereg_replace ("^|" ( RN)) (.) "," 1.3 ", $body);

$header = "Mime-version:1.0rn";

if ($mailtype = = "HTML") {

$header. = "CONTENT-TYPE:TEXT/HTMLRN";

}

$header. = "To:". $to. " RN ";

if ($cc!= "") {

$header. = "Cc:". $cc. " RN ";

}

$header. = "From: $from <". $from. " >rn ";

$header. = "Subject:". $subject. " RN ";

$header. = $additional _headers;

$header. = "Date:". Date ("R"). " RN ";

$header. = "X-mailer:by Redhat (php/). Phpversion ().") RN ";

List ($msec, $sec) = Explode ("", Microtime ());

$header. = "Message-id: <". Date ("Ymdhis", $sec). ($msec *1000000). ".". $mail _from. " >rn ";

$TO = Explode (",", $this->strip_comment ($to));

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, $header, $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 Hostn");

}

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 <CR><LF>.<CR><LF> [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;

}
Focus and PHP Learning http://www.daixiaorui.com welcome your visit

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. " RN ". $body);

$this->smtp_debug (">". Str_replace ("RN", "n".) > ", $header." N> ". $body." N> "));

return TRUE;

}

function Smtp_eom ()

{

Fputs ($this->sock, "Rn.rn");

$this->smtp_debug (". [Eom]n ");

return $this->SMTP_OK ();

}

function Smtp_ok ()

{

$response = Str_replace ("rn", "" ", Fgets ($this->sock, 512));

$this->smtp_debug ($response.) n ");

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

Fputs ($this->sock, "quitrn");

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. " RN ");

$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, "a"))) {

$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 ("([trn]) +", "", $address);

$address = Ereg_replace ("^.*< (. +) >.*$", "1", $address);

return $address;

}

function Smtp_debug ($message)

{

if ($this->debug) {

Echo $message;

}

}

}

?>

PHP file to send mailbox

The code is as follows Copy Code

<?php

Require_once "email.class.php";

Configuration Information ********************************

$smtpserver = "smtp.126.com";//SMTP server

$smtpserverport =25;//SMTP Server Port

$smtpusermail = "new2008oh@126.com";//smtp user mailboxes for the server

$smtpemailto = $_post[' toemail '];//sent to whom

$smtpuser = "New2008oh";//smtp user account for the server

$smtppass = "Your mailbox password";//smtp user password for the server

$mailtitle = $_post[' title '];//message Theme

$mailcontent = "

$mailtype = "HTML";//Message Format (html/txt), TXT to text message

Configuration Information ****************************

$SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass);//One of the true in this is to use authentication, otherwise authentication is not used.

$SMTP->debug = false;//whether to display the debug information sent

$state = $smtp->sendmail ($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);


echo "<div style= ' width:300px; MARGIN:36PX auto; ' > ";

if ($state = = "") {

echo "Sorry, Mail failed!" Please check that the mailbox is filled in incorrectly. ";

echo "<a href= ' index.html ' > Dot this return </a>";

Exit ();

}

Echo, Congratulations! Mail sent successfully!! ";

echo "<a href= ' index.html ' > Dot this return </a>";

echo "</div>";

?>

Effect screenshot Appreciation:


Fill in the recipient, title and content after submitting

Back to Success Tips


Instantly received the message

SMTP class cannot send message resolution


Accidentally found my website background automatically send mail function can not use, reported this error:

Trying to smtp.126.com:25 Error:cannot connenct to relay host smtp.126.com Error: () error:cannot send email to Web@daix Iaorui.com State


Probably means: Unable to connenct the relay host smtp.126.com error: () error £ º Unable to send e-mail to web@daixiaorui.com

After finding more than n data on the Internet, we finally found the solution, not the SMTP class problem, but the Linux configuration. The Fsockopen function is disabled by the php.ini of the server.


Open the space under the php.ini file, Linux space can generally be customized php.ini, so the root directory will generally have this file.


There are two places where this function may be disabled:

1. Allow_url_fopen = on view equals after on, and if off, the function is disabled

2. disable_functions = Fsockopen Pfsockopen (This is my case) this should remove the front "fsockopen". To make it into: Disable_functions = Pfsockopen


After you have changed, save and refresh the page, you will find that you can successfully use the SMTP class to send e-mail messages under Linux. Thanks to the method of sharing friends, the problem has finally been solved.


Easy to send email sample downloads using the SMTP class in PHP: Http://file.111cn.net/upload/2013/12/13751999009017.zip

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.