cvs: pear /PECL/smtp smtp.c

From: Date: Sat, 01 Dec 2001 01:09:51 +0000
Subject: cvs: pear /PECL/smtp smtp.c
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1441@lists.php.net to get a copy of this message
heyesr Fri Nov 30 20:09:51 2001 EDT Modified files: /pear/PECL/smtp smtp.c Log: Reworked functions to allow for future additions.

Index: pear/PECL/smtp/smtp.c diff -u pear/PECL/smtp/smtp.c:1.1 pear/PECL/smtp/smtp.c:1.2 --- pear/PECL/smtp/smtp.c:1.1 Fri Nov 30 12:21:51 2001 +++ pear/PECL/smtp/smtp.c Fri Nov 30 20:09:51 2001 @@ -35,11 +35,6 @@ #include <netdb.h> #include <string.h> - -/******************** -** DEFINITIONS -********************/ - #define TRUE 1 #define FALSE 0 @@ -61,18 +56,23 @@ } smtp_resource; void smtp_resource_destructor(smtp_resource *handle); -int _php_smtp_validate_response(char *buffer, const char *code); -int _php_smtp_send_data(smtp_resource *smtp, const char *data); -int _php_smtp_get_data(smtp_resource *smtp, char *buffer); -char* _php_smtp_escape(char *data); + +smtp_resource *_php_smtp_connect(const char *server_addr, unsigned short int server_port, const char *helo_host); +int _php_smtp_close(smtp_resource *smtp); +int _php_smtp_cmd_mail(smtp_resource *smtp, const char *address); +int _php_smtp_cmd_rcpt(smtp_resource *smtp, const char *address); +int _php_smtp_cmd_data(smtp_resource *smtp, const char *data); + +int _php_smtp_validate_response(char *buffer, const char *code); +int _php_smtp_send_data(smtp_resource *smtp, const char *data); +int _php_smtp_get_data(smtp_resource *smtp, char *buffer); +char* _php_smtp_escape(const char *data); /* True global resources - no need for thread safety here */ static int le_smtp; /* {{{ smtp_functions[] - * - * Every user visible function must have an entry in smtp_functions[]. */ function_entry smtp_functions[] = { PHP_FE(smtp_connect, NULL) @@ -123,7 +123,6 @@ } /* }}} */ -/* Remove if there's nothing to do at request start */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(smtp) @@ -168,16 +167,12 @@ PHP_FUNCTION(smtp_connect) { smtp_resource *smtp; - struct hostent *server_addr_resolved; - struct sockaddr_in server; zval **server_addr; zval **port; + zval **hostname; + char *helo_host = (char *)emalloc(256); int argc; unsigned short int server_port; - char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); - char *hostname = (char *)emalloc(256); - char *helo_string; - /* ** Get the arguments @@ -188,6 +183,8 @@ zend_get_parameters_ex(1, &server_addr); convert_to_string_ex(server_addr); server_port = SMTP_DEFAULT_PORT; + if(gethostname(helo_host, 256) == -1) + goto ERROR; break; case 2: @@ -195,29 +192,199 @@ convert_to_string_ex(server_addr); convert_to_long_ex(port); server_port = (unsigned short int)Z_LVAL_PP(port); + if(gethostname(helo_host, 256) == -1) + goto ERROR; + break; + + case 3: + zend_get_parameters_ex(2, &server_addr, &port, &hostname); + convert_to_string_ex(server_addr); + convert_to_string_ex(hostname); + convert_to_long_ex(port); + server_port = (unsigned short int)Z_LVAL_PP(port); + helo_host = Z_STRVAL_PP(hostname); break; default: WRONG_PARAM_COUNT; } + smtp = (smtp_resource *)emalloc(sizeof(smtp_resource)); + memset(smtp, 0, sizeof(smtp_resource)); + smtp = _php_smtp_connect((const char *)Z_STRVAL_PP(server_addr), server_port, helo_host); + + if(smtp == FALSE){ + efree(smtp); + goto ERROR; + } + + ZEND_REGISTER_RESOURCE(return_value, smtp, le_smtp); + return; + +ERROR: + efree(helo_host); + RETURN_FALSE; +} +/* }}} */ + + +/* {{{ proto bool smtp_close(resource smtp_resource) + Close connection to server*/ +PHP_FUNCTION(smtp_close) +{ + smtp_resource *smtp; + zval **resource_id; + + /* + ** Get the argument + */ + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &resource_id) == FAILURE){ + WRONG_PARAM_COUNT; + } + + ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); + + if(_php_smtp_close(smtp) == TRUE){ + zend_list_delete(Z_LVAL_PP(resource_id)); + RETURN_TRUE; + }else{ + RETURN_FALSE; + } +} +/* }}} */ + + +/* {{{ proto bool smtp_cmd_mail(resource smtp_resource, string return_address) + Sends MAIL FROM:<email> command*/ +PHP_FUNCTION(smtp_cmd_mail) +{ + smtp_resource *smtp; + zval **resource_id; + zval **address; + + /* + ** Get the arguments + */ + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &address) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(address); + + ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); + + /* + ** Check the state + */ + if(smtp->state != SMTP_STATE_SEND_MAIL) + RETURN_FALSE; + + if(_php_smtp_cmd_mail(smtp, Z_STRVAL_PP(address)) == TRUE){ + RETURN_TRUE; + }else{ + RETURN_FALSE; + } +} +/* }}} */ + + +/* {{{ proto bool smtp_cmd_rcpt(resource smtp_resource, string recipient_address) + Sends RCPT TO:<email> command*/ +PHP_FUNCTION(smtp_cmd_rcpt) +{ + smtp_resource *smtp; + zval **resource_id; + zval **address; + + /* + ** Get the arguments + */ + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &address) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(address); + + ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); + + /* + ** Check the state + */ + if(smtp->state != SMTP_STATE_SEND_RCPT && smtp->state != SMTP_STATE_SEND_DATA) + RETURN_FALSE; + + if(_php_smtp_cmd_rcpt(smtp, Z_STRVAL_PP(address)) == TRUE){ + RETURN_TRUE; + }else{ + RETURN_FALSE; + } +} +/* }}} */ + + +/* {{{ proto bool smtp_cmd_data(resource smtp_resource, string email) + Sends DATA command and then the email itself*/ +PHP_FUNCTION(smtp_cmd_data) +{ + smtp_resource *smtp; + zval **resource_id; + zval **email; + + /* + ** Get the arguments + */ + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &email) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(email); + + ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); + + /* + ** Check the state + */ + if(smtp->state != SMTP_STATE_SEND_DATA) + RETURN_FALSE; + + if(_php_smtp_cmd_data(smtp, Z_STRVAL_PP(email)) == TRUE){ + RETURN_TRUE; + }else{ + RETURN_FALSE; + } +} +/* }}} */ + + + +/* +** Below are the functions which actually perform the functions of the above public functions. +*/ + + + +/* {{{ proto smtp_resource *_php_smtp_connect(const char *server_addr, unsigned short int server_port) + Actions the php function smtp_connect() */ +smtp_resource *_php_smtp_connect(const char *server_addr, unsigned short int server_port, const char *helo_host){ + + smtp_resource *smtp; + struct hostent *server_addr_resolved; + struct sockaddr_in server; + char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); + char *helo_string; + /* ** Get ip of server to connect to */ - if ((server_addr_resolved = gethostbyname(Z_STRVAL_PP(server_addr))) == NULL){ + if ((server_addr_resolved = gethostbyname(server_addr)) == NULL){ /* ** Do something useful here? */ } - - /* ** Setup the server struct */ server.sin_family = AF_INET; - server.sin_port = htons((unsigned short int)server_port); + server.sin_port = htons(server_port); server.sin_addr.s_addr = inet_addr((char *)inet_ntoa(*((struct in_addr *)server_addr_resolved->h_addr))); memset(&(server.sin_zero), '\0', 8); @@ -234,7 +401,7 @@ if(connect(smtp->socket_descriptor, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1){ close(smtp->socket_descriptor); efree(smtp); - RETURN_FALSE; + return FALSE; } /* @@ -243,60 +410,44 @@ if(_php_smtp_get_data(smtp, buffer) == TRUE && _php_smtp_validate_response(buffer, "220") != TRUE){ close(smtp->socket_descriptor); efree(smtp); - RETURN_FALSE; - } - - /* - ** Get my hostname - */ - if(gethostname(hostname, 256) == -1){ - RETURN_FALSE; + return FALSE; } /* ** Format the HELO ... string */ - helo_string = (char *)ecalloc(1, 5 + strlen(hostname) + 1); - sprintf(helo_string, "HELO %s", hostname); - efree(hostname); + helo_string = (char *)ecalloc(1, 5 + strlen(helo_host) + 1); + sprintf(helo_string, "HELO %s", helo_host); /* ** Send the HELO string */ _php_smtp_send_data(smtp, helo_string); _php_smtp_get_data(smtp, buffer); - efree(helo_string); + + if(_php_smtp_validate_response(buffer, "250") != TRUE){ efree(buffer); - RETURN_FALSE; + return FALSE; } efree(buffer); smtp->state = SMTP_STATE_SEND_MAIL; - ZEND_REGISTER_RESOURCE(return_value, smtp, le_smtp); + return smtp; + } /* }}} */ -/* {{{ proto bool smtp_close(resource smtp_resource) - Close connection to server*/ -PHP_FUNCTION(smtp_close) -{ - smtp_resource *smtp; - zval **resource_id; - char *quit_string; - /* - ** Get the argument - */ - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &resource_id) == FAILURE){ - WRONG_PARAM_COUNT; - } +/* {{{ proto int _php_smtp_close(smtp_resource *smtp) + Function that actions the php function smtp_close()*/ +int _php_smtp_close(smtp_resource *smtp){ - ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); + char *quit_string; /* ** Send the quit command @@ -305,43 +456,24 @@ _php_smtp_send_data(smtp, quit_string); efree(quit_string); - zend_list_delete(Z_LVAL_PP(resource_id)); - RETURN_TRUE; + return TRUE; } /* }}} */ -/* {{{ proto bool smtp_cmd_mail(resource smtp_resource, string return_address) - Sends MAIL FROM:<email> command*/ -PHP_FUNCTION(smtp_cmd_mail) -{ - smtp_resource *smtp; - char *mail_string; - char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); - zval **resource_id; - zval **address; - /* - ** Get the arguments - */ - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &address) == FAILURE) { - convert_to_string_ex(address); - WRONG_PARAM_COUNT; - } +/* {{{ proto int _php_smtp_cmd_mail(smtp_resource *smtp, const char *address) + Actions the php function smtp_cmd_mail() */ +int _php_smtp_cmd_mail(smtp_resource *smtp, const char *address){ - ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); - - /* - ** Check the state - */ - if(smtp->state != SMTP_STATE_SEND_MAIL) - RETURN_FALSE; + char *mail_string; + char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); /* ** Format the MAIL FROM:<...> string */ - mail_string = (char *)ecalloc(1, 12 + Z_STRLEN_PP(address) + 1); - sprintf(mail_string, "MAIL FROM:<%s>", Z_STRVAL_PP(address)); + mail_string = (char *)ecalloc(1, 12 + strlen(address) + 1); + sprintf(mail_string, "MAIL FROM:<%s>", address); /* ** Send the MAIL string @@ -354,45 +486,27 @@ if(_php_smtp_validate_response(buffer, "250") == TRUE){ efree(buffer); smtp->state = SMTP_STATE_SEND_RCPT; - RETURN_TRUE; + return TRUE; } - RETURN_FALSE; + return FALSE; } /* }}} */ -/* {{{ proto bool smtp_cmd_rcpt(resource smtp_resource, string recipient_address) - Sends RCPT TO:<email> command*/ -PHP_FUNCTION(smtp_cmd_rcpt) -{ - smtp_resource *smtp; - char *rcpt_string; - char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); - zval **resource_id; - zval **address; - - /* - ** Get the arguments - */ - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &address) == FAILURE) { - convert_to_string_ex(address); - WRONG_PARAM_COUNT; - } - ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); +/* {{{ proto int _php_smtp_cmd_rcpt(smtp_resource *smtp, const char *address) + Actions the php function smtp_cmd_rcpt() */ +int _php_smtp_cmd_rcpt(smtp_resource *smtp, const char *address){ - /* - ** Check the state - */ - if(smtp->state != SMTP_STATE_SEND_RCPT && smtp->state != SMTP_STATE_SEND_DATA) - RETURN_FALSE; + char *rcpt_string; + char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); /* ** Format the RCPT TO:<...> string */ - rcpt_string = (char *)ecalloc(1, 10 + Z_STRLEN_PP(address) + 1); - sprintf(rcpt_string, "RCPT TO:<%s>", Z_STRVAL_PP(address)); + rcpt_string = (char *)ecalloc(1, 10 + strlen(address) + 1); + sprintf(rcpt_string, "RCPT TO:<%s>", address); /* ** Send the RCPT string @@ -405,41 +519,23 @@ if(_php_smtp_validate_response(buffer, "250") == TRUE){ efree(buffer); smtp->state = SMTP_STATE_SEND_DATA; - RETURN_TRUE; + return TRUE; } - RETURN_FALSE; + return FALSE; } /* }}} */ -/* {{{ proto bool smtp_cmd_data(resource smtp_resource, string email) - Sends DATA command and then the email itself*/ -PHP_FUNCTION(smtp_cmd_data) -{ - smtp_resource *smtp; + +/* {{{ proto int _php_smtp_cmd_data(smtp_resource *smtp, const char *data) + Actions the php function smtp_cmd_data() */ +int _php_smtp_cmd_data(smtp_resource *smtp, const char *data){ + char *data_string; char *buffer = (char *)emalloc(SMTP_READ_BUFFER_SIZE); char *email; char *email_final; - zval **resource_id; - zval **arg_email; - - /* - ** Get the arguments - */ - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &resource_id, &arg_email) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(arg_email); - - ZEND_FETCH_RESOURCE(smtp, smtp_resource *, resource_id, -1, "SMTP Connection ID", le_smtp); - - /* - ** Check the state - */ - if(smtp->state != SMTP_STATE_SEND_DATA) - RETURN_FALSE; /* ** Send the DATA string @@ -454,9 +550,9 @@ /* ** Send the email itself */ - email = estrdup(Z_STRVAL_PP(arg_email)); - email = _php_smtp_escape(email); - email_final = (char *)ecalloc(1, strlen(email) + 4); + //email = estrdup(email)); + email = _php_smtp_escape(data); + email_final = (char *)ecalloc(1, strlen(email) + 3 + 1); sprintf(email_final, "%s\r\n.", email); _php_smtp_send_data(smtp, email_final); @@ -468,19 +564,20 @@ efree(buffer); efree(data_string); smtp->state = SMTP_STATE_EMAIL_SENT; - RETURN_TRUE; + return TRUE; } } efree(email); efree(buffer); efree(data_string); - RETURN_FALSE; + return FALSE; } /* }}} */ -/* {{{ proto int smtp_validate_repsonse(char *buffer, const char *code) + +/* {{{ proto int smtp_validate_response(char *buffer, const char *code) Checks the server reponse corresonds to supplied reponse code*/ int _php_smtp_validate_response(char *buffer, const char *code){ @@ -493,6 +590,7 @@ /* }}} */ + /* {{{ proto int smtp_send_data(smtp_resource *smtp, const char *data) Sends a commend to the server after appending a \r\n*/ int _php_smtp_send_data(smtp_resource *smtp, const char *data){ @@ -541,7 +639,7 @@ /* {{{ proto char *smtp_escape(char *data) Replaces \n. with \n.. in the email body*/ -char *_php_smtp_escape(char *data){ +char *_php_smtp_escape(const char *data){ char *buffer = (char *)ecalloc(1, 2 * strlen(data)); char *p = buffer;
« previous php.pear.cvs (#1441) next »