Include_once ("class.phpmailer.php");
- /**
- * Define Message Module configuration information
- */
- Define ("Smtp_host", "smtp.mail.yahoo.com"); SMTP Host
- Define ("Smtp_mail", "XXXX@yahoo.cn"); SMTP User Email
- Define ("Smtp_pass", "XXXX"); The password used for SMTP
Define ("Service_mail", "XXXX@yahoo.cn"); SMTP User Email
- Define ("Service_Name", "Phpbook Mail Test"); The name used for SMTP
/**
- * Use Phpmailer Email module
- *
- * @param string $email
- * @param string $user
- * @param string $subject
- * @param string $body
- * @return BOOL
- */
- function SendMail ($email, $user, $subject, $body)
- {
- $mail = new Phpmailer ();
- $this;
- $mail->issmtp (); Set up to use SMTP
- $mail->host = smtp_host; Set the SMTP server address
- $mail->smtpauth = true; Turn on SMTP permission validation
- $mail->username = Smtp_mail; SMTP User Name
- $mail->password = Smtp_pass; SMTP Server Password
$mail->from = Service_mail; Set Sender Address
- $mail->fromname = service_name; Set Sender Name
- $mail->addaddress ($email, $user); Add recipient Address
- $mail->addreplyto (Service_mail, service_name); Set Reply Address
$mail->wordwrap = 50; Set display format
- $mail->ishtml (TRUE); Set up mail support HTML
- $mail->subject = $subject;
- $mail->body = $body;
- $mail->altbody = ""; Text type of message
if (! $mail->send ())
- {
- return $mail->errorinfo;
- }
- return true;
- }
Start sending test message Ng:fsockopen () [Function.fsockopen]: php_network_getaddresses:getaddrinfo failed:name or service not known I N/var/www/xiehui/admin/mail/class.smtp.php on line 89
- $tomail = "XXXX@126.com";
- $user = "Xxxxlinux";
- $_mailsubject = "Mail test sample!"; Message header Group to users
- $_mailbody = "Sina net"; Mail Content Group
- SendMail ($tomail, $user, $_mailsubject,$_mailbody);
- ?>
Copy CodeThe experiment proves that the SMTP of Yahoo is very easy to use, so-called Sina actually does not use, I stuck in a good long time. method Four , give the socket written by the program to use the socket to send mail package class:
- Class sendmail{
- var $lastmessage; Record the last returned response information
- var $lastact; The last action, the string form
- var $welcome; Use behind helo, welcome user
- var $debug; Whether to display debug information
- var $smtp; SMTP server
- var $port; SMTP port number
- var $fp; Socket handle
- Send mail function
- function Send_mail ($smtp, $welcome = "", $debug =false) {
- if (empty ($SMTP)) Die ("SMTP cannot be empty!") ");
- $this->smtp= $smtp;
- if (empty ($welcome)) {
- $this->welcome=gethostbyaddr ("localhost");
- }else
- $this->welcome= $welcome;
- $this->debug= $debug;
- $this->lastmessage= "";
- $this->lastact= "";
- $this->port= "25";
- }
- Display Debug Information
- function Show_debug ($message, $inout) {
- if ($this->debug) {
- if ($inout = = "in") {//Response information
- $m = ' << ';
- }else
- $m = ' >> ';
- if (!ereg ("\n$", $message))
- $message. = "
";
- $message =nl2br ($message);
- echo "${m}${message}";
- }
- }
- Execute the passed command
- function Do_command ($command, $code) {
- $this->lastact= $command;
- $this->show_debug ($this->lastact, "out");
- Fputs ($this->fp, $this->lastact);
- $this->lastmessage = fgets ($this->FP, 512);
- $this->show_debug ($this->lastmessage, "in");
- if (!ereg ("^ $code", $this->lastmessage))
- return false;
- Else
- return true;
- }
- Mail delivery processing
- function Send ($to, $from, $subject, $message) {
- Connecting to a server
- $this->lastact= "Connect";
- $this->show_debug ("Connect to SMTP server:". $this->smtp, "out");
- $this->FP = Fsockopen ($this->smtp, $this->port);
- if ($this->fp) {
- $this->set_socket_blocking ($this->fp, true);
- $this->lastmessage=fgets ($this->fp,512);
- $this->show_debug ($this->lastmessage, "in");
- if (! Ereg ("^220", $this->lastmessage)) {
- return false;
- }else{
- $this->lastact= "HELO". $this->welcome. "\ n";
- if (! $this->do_command ($this->lastact, "250")) {
- Fclose ($this->FP);
- return false;
- }
- $this->lastact= "MAIL from: $from". "\ n";
- if (! $this->do_command ($this->lastact, "250")) {
- Fclose ($this->FP);
- return false;
- }
- $this->lastact= "RCPT to: $to". "\ n";
- if (! $this->do_command ($this->lastact, "250")) {
- Fclose ($this->FP);
- return false;
- }
- Start sending message body
- $this->lastact= "data\n";
- if (! $this->do_command ($this->lastact, "354")) {
- Fclose ($this->FP);
- return false;
- }
- Start Processing Message topic header
- $head = "Subject: $subject \ n";
- if (!empty ($subject) &&!ereg ($head, $message)) {
- $message = $head. $message;
- }
- Start processing messages from header
- $head = "From: $from \ n";
- if (!empty ($from) &&!ereg ($head, $message)) {
- $message = $head. $message;
- }
- Start processing mail to Header
- $head = "to: $to \ n";
- if (!empty ($to) &&!ereg ($head, $message)) {
- $message = $head. $message;
- }
- Processing End String
- if (!ereg ("\n\.\n", $message))
- $message. = "\n.\n";
- $this->show_debug ($message, "out");
- Fputs ($this->fp, $message);
- $this->lastact= "quit\n";
- if (! $this->do_command ($this->lastact, "250")) {
- Fclose ($this->FP);
- return false;
- }
- }
- return true;
- }else{
- $this->show_debug ("Connection Failed!!", "in");
- return false;
- }
- }
- }
- ?>
Copy CodeExample of sending a message using the socket:
- Include ("./sendmail.class.php");
- $mail = new SendMail ();
- $email = "Hello, this is a test mail!" ";
- $sendmail = new Send_mail ("smtp.mail.126.com", "Phpbook", true); Display information
- if ($mail->send ("XXXX@126.com", "XXXX@126.com", "Test socket Mail", $email)) {
- echo "sent successfully!
";
- }else{
- echo "Send failed!
";
- }
- ?>
Copy Code |