Phpsocket basic mail-PHP source code

Source: Internet
Author: User
Phpsocket basic mail class php socket basic mail class, fsocketopen pfsocketopen is generally disabled by space service providers.
In this case, you can use the SMTP protocol to connect and communicate with the mail server using socket_create and socket_connect.
For smtp, you can search for the format of a sent email.
A problem that you have noticed when writing this code. do not use single quotation marks when sending commands with the mail service. for example, use double quotation marks"
Otherwise, the email server does not recognize the command. For example, the command "ehlo helo \ r \ n ", this is because \ r \ n must be added to each command. If \ r \ n is enclosed in single quotes, it will be sent as a character without a carriage return.
This class has been integrated into the phpx (self-written PHP lightweight framework) framework,

If you are interested, you can get the local test. the status code and description returned by the email server when you communicate with the email server are displayed during running.

Smtp status code and description http://my.oschina.net/apeng/blog/119627

 Smtp = $ array [0]; $ this-> port = $ array [1]; $ this-> from = $ array [2]; $ this-> pass = $ array [3]; $ this-> to = $ array [4];} public function send ($ header = null, $ body = null) {$ this-> socket = socket_create (AF_INET, SOCK_STREAM, getprotobyname ('tcp '); if (! $ This-> socket) {$ this-> log ('failed to create socket ', true);} if (socket_connect ($ this-> socket, $ this-> smtp, $ this-> port) {$ this-> log ('server connection response :'. socket_strerror (socket_last_error ();} else {$ this-> log ('socket connection failed', true );} $ this-> data = "ehlo helo \ r \ n"; $ this-> do_send (); $ this-> data = "auth login \ r \ n "; $ this-> do_send (); $ this-> data = base64_encode ($ this-> from ). "\ r \ n"; $ this-> do_send (); $ this-> data = base64_encode ($ this-> pass ). "\ r \ n"; $ this-> do_send (); $ this-> data = "mail from: <". $ this-> from. "> \ r \ n"; $ this-> do_send (); $ this-> data = "rcpt to: <". $ this->. "> \ r \ n"; $ this-> do_send (); $ this-> data = "DATA \ r \ n"; $ this-> do_send (); $ this-> data = "From: Anonymous <". $ this-> from. "> \ r \ n"; $ this-> data. = "Subject :". $ header. "\ r \ n"; $ this-> data. = $ body. "\ r \ n. \ r \ n "; $ this-> do_send (); $ this-> data =" QUIT \ r \ n "; $ this-> do_send (); socket_close ($ this-> socket);} public function do_send () {socket_write ($ this-> socket, $ this-> data, strlen ($ this-> data )); $ this-> log ('Client sending :'. $ this-> data); $ this-> log ('server response :'. socket_read ($ this-> socket, 1024 )).'
';} Public function log ($ args = null, $ exit = false) {echo $ args .'
'; If ($ exit = true) {exit ;}}?>

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.