PHP mail sender without native mail()


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // modified to provide authenticated logins
  2. function mymail($to,$subject,$message,$headers)
  3. {
  4.  
  5. // set as global variable
  6. global $GLOBAL;
  7.  
  8. // get From address
  9. if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/", $headers, $froms) ) {
  10. preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $froms[0], $fromarr);
  11. $from = $fromarr[0];
  12. }
  13.  
  14. // Open an SMTP connection
  15. $cp = fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr, 1);
  16. if (!$cp)
  17. return "Failed to even make a connection";
  18. $res=fgets($cp,256);
  19. if(substr($res,0,3) != "220") return "Failed to connect";
  20.  
  21. // Say hello...
  22. fputs($cp, "HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
  23. $res=fgets($cp,256);
  24. if(substr($res,0,3) != "250") return "Failed to Introduce";
  25.  
  26. // perform authentication
  27. fputs($cp, "auth login\r\n");
  28. $res=fgets($cp,256);
  29. if(substr($res,0,3) != "334") return "Failed to Initiate Authentication";
  30.  
  31. fputs($cp, base64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
  32. $res=fgets($cp,256);
  33. if(substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";
  34.  
  35. fputs($cp, base64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
  36. $res=fgets($cp,256);
  37. if(substr($res,0,3) != "235") return "Failed to Authenticate";
  38.  
  39. // Mail from...
  40. fputs($cp, "MAIL FROM: <$from>\r\n");
  41. $res=fgets($cp,256);
  42. if(substr($res,0,3) != "250") return "MAIL FROM failed";
  43.  
  44. // Rcpt to...
  45. fputs($cp, "RCPT TO: <$to>\r\n");
  46. $res=fgets($cp,256);
  47. if(substr($res,0,3) != "250") return "RCPT TO failed";
  48.  
  49. // Data...
  50. fputs($cp, "DATA\r\n");
  51. $res=fgets($cp,256);
  52. if(substr($res,0,3) != "354") return "DATA failed";
  53.  
  54. // Send To:, From:, Subject:, other headers, blank line, message, and finish
  55. // with a period on its own line (for end of message)
  56. fputs($cp, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
  57. $res=fgets($cp,256);
  58. if(substr($res,0,3) != "250") return "Message Body Failed";
  59.  
  60. // ...And time to quit...
  61. fputs($cp,"QUIT\r\n");
  62. $res=fgets($cp,256);
  63. if(substr($res,0,3) != "221") return "QUIT failed";
  64.  
  65. return true;
  66. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.