Include_once ("class. phpmailer. php ");
- /**
- * Define email module preparation information
- */
- Define ("SMTP_HOST", "smtp.mail.yahoo.com"); // SMTP host
- Define ("SMTP_MAIL", "XXXX@yahoo.cn"); // SMTP user email
- Define ("SMTP_PASS", "XXXX"); // SMTP password
Define ("SERVICE_MAIL", "XXXX@yahoo.cn"); // SMTP user email
- Define ("SERVICE_NAME", "PHPBOOK mail Test"); // SMTP name
/**
- * Use the phpmailer mail 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 (); // you can specify whether to use SMTP.
- $ Mail-> Host = SMTP_HOST; // you can specify the SMTP server address.
- $ Mail-> SMTPAuth = true; // enable SMTP permission verification
- $ Mail-> Username = SMTP_MAIL; // SMTP Username
- $ Mail-> Password = SMTP_PASS; // SMTP server Password
$ Mail-> From = SERVICE_MAIL; // you can specify the sender address.
- $ Mail-> FromName = SERVICE_NAME; // you can specify the sender name.
- $ Mail-> AddAddress ($ email, $ user); // add the recipient address
- $ Mail-> AddReplyTo (SERVICE_MAIL, SERVICE_NAME); // you can specify a reply address.
$ Mail-> WordWrap = 50; // Set the display format
- $ Mail-> IsHTML (true); // you can specify html for an email.
- $ Mail-> Subject = $ subject;
- $ Mail-> Body = $ body;
- $ Mail-> AltBody = ""; // text-type email
If (! $ Mail-> Send ())
- {
- Return $ mail-> ErrorInfo;
- }
- Return true;
- }
// Start sending the test email ng: fsockopen () [function. fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in/var/www/xiehui/admin/mail/class. smtp. php on line 89
- $ Tomail = "XXXX@126.com ";
- $ User = "XXXXlinux ";
- $ _ MailSubject = "email test example! "; // The Mail title group sent to the user
- $ _ MailBody = "xinlang net"; // email content group
- SendMail ($ tomail, $ user, $ _ mailSubject, $ _ mailBody );
- ?>
Experiment proves that yahoo's smtp is very easy to use, and sina is actually not easy to use. I am stuck for a long time. Method 4To the encapsulation class for the program written by the socket to send emails using the socket:
- Class sendmail {
- Var $ lastmessage; // record the last response
- Var $ lastact; // The final action, in the string format
- Var $ welcome; // used after HELO. welcome
- Var $ debug; // whether to display debugging information
- Var $ smtp; // smtp server
- Var $ port; // smtp port number
- Var $ fp; // socket handle
- // Email sending function
- Function send_mail ($ smtp, $ welcome = "", $ debug = false ){
- If (empty ($ smtp) die ("SMTP cannot be blank! ");
- $ 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 debugging 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 handling
- Function send ($ to, $ from, $ subject, $ message ){
- // Connect to the 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-& gt; do_command ($ this-& gt; lastact, "250 ")){
- Fclose ($ this-> fp );
- Return false;
- }
- $ This-> lastact = "mail from: $ from". "\ n ";
- If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
- Fclose ($ this-> fp );
- Return false;
- }
- $ This-> lastact = "rcpt to: $ to". "\ n ";
- If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
- Fclose ($ this-> fp );
- Return false;
- }
- // Start sending the email body
- $ This-> lastact = "DATA \ n ";
- If (! $ This-& gt; do_command ($ this-& gt; lastact, "354 ")){
- Fclose ($ this-> fp );
- Return false;
- }
- // Start to process the subject header
- $ Head = "Subject: $ subject \ n ";
- If (! Empty ($ subject )&&! Ereg ($ head, $ message )){
- $ Message = $ head. $ message;
- }
- // Start to process the mail From header
- $ Head = "From: $ from \ n ";
- If (! Empty ($ from )&&! Ereg ($ head, $ message )){
- $ Message = $ head. $ message;
- }
- // Start To process the mail To header
- $ Head = "To: $ to \ n ";
- If (! Empty ($ )&&! Ereg ($ head, $ message )){
- $ Message = $ head. $ message;
- }
- // Process the 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-& gt; do_command ($ this-& gt; lastact, "250 ")){
- Fclose ($ this-> fp );
- Return false;
- }
- }
- Return true;
- } Else {
- $ This-> show_debug ("connection failed !! "," In ");
- Return false;
- }
- }
- }
- ?>
Example of sending an email using socket:
- Include ("./sendmail. class. php ");
- $ Mail = new sendmail ();
- $ Email = "Hello, this is a test email! ";
- $ Sendmail = new send_mail ("smtp.mail.126.com", "PHPBOOK", true); // Display The Call Information
- If ($ mail-> send ("XXXX@126.com", "XXXX@126.com", "test SOCKET mail", $ email )){
- Echo "sent successfully!
";
- } Else {
- Echo "failed to send!
";
- }
- ?>
|