net.manuellemos.mimemessage smtp_message_class @(#) $Id: smtp_message.php,v 1.36 2011/03/09 07:48:52 mlemos Exp $ Copyright © (C) Manuel Lemos 1999-2004 MIME E-mail message composing and sending via SMTP Manuel Lemos mlemos-at-acm.org en Implement an alternative message delivery method via SMTP protocol, overriding the method of using the PHP mail() function implemented by the base class. This class should be used exactly the same way as the base class for composing and sending messages. Just create a new object of this class as follows and set only the necessary variables to configure details of the SMTP delivery. require('email_message.php');
require('smtp.php');
require('smtp_message.php');

$message_object = new smtp_message_class;
- Requirements You need the SMTP E-mail sending class http://freshmeat.net/projects/smtpclass/ to perform the actual message delivery via the SMTP protocol. - SMTP connection Before sending a message by relaying it to a given SMTP server you need set the smtp_host variable to that server address. The localhost variable needs to be set to the sending computer address. You may also adjust the time the class will wait for establishing a connection by changing the timeout variable. - Secure SMTP connections with SSL Some SMTP servers, like for instance Gmail, require secure connections via SSL. In that case it is necessary to set the smtp_ssl variable to 1. In the case of Gmail, it is also necessary to set the connection port changing the smtp_port variable to 465. SSL support requires at least PHP 4.3.0 with OpenSSL extension enabled. - Secure SMTP connections starting TLS after connections is established Some SMTP servers, like for instance Hotmail, require starting the TLS protocol after the connection is already established to exchange data securely. In that case it is necessary to set the smtp_start_tls variable to 1. Starting TLS protocol on an already established connection requires at least PHP 5.1.0 with OpenSSL extension enabled. - Authentication Most servers only allow relaying messages sent by authorized users. If the SMTP server that you want to use requires authentication, you need to set the variables smtp_user, smtp_realm and smtp_password. The way these values need to be set depends on the server. Usually the realm value is empty and only the user and password need to be set. If the server requires authentication via NTLM mechanism (Windows or Samba), you need to set the smtp_realm to the Windows domain name and also set the variable smtp_workstation to the user workstation name. Some servers require that the authentication be done on a separate server using the POP3 protocol before connecting to the SMTP server. In this case you need to specify the address of the POP3 server setting the smtp_pop3_auth_host variable. - Sending urgent messages with direct delivery If you need to send urgent messages or obtain immediate confirmation that a message is accepted by the recipient SMTP server, you can use the direct delivery mode setting the direct_delivery variable to 1. This mode can be used to send a message to only one recipient. To use this mode, it is necessary to have a way to determine the recipient domain SMTP server address. The class uses the PHP getmxrr() function, but on some systems like for instance under Windows, this function does not work. In this case you may specify an equivalent alternative by setting the smtp_getmxrr variable. See the SMTP class page for available alternatives. - Troubleshooting and debugging If for some reason the delivery via SMTP is not working and the error messages are not self-explanatory, you may set the smtp_debug to 1 to make the class output the SMTP protocol dialog with the server. If you want to display this dialog properly formatted in an HTML page, also set the smtp_debug to 1. - Optimizing the delivery of messages to many recipients When sending messages to many recipients, this class can hinted to optimize its behavior by using the SetBulkMail function. After calling this function passing 1 to the SetBulkMail on argument, when the message is sent this class opens a TCP connection to the SMTP server but will not close it. This avoids the overhead of opening and closing connections. When the delivery of the messages to all recipients is done, the connection may be closed implicitly by calling the SetBulkMail function again passing 0 to the SetBulkMail on argument.
{/metadocument} */ class smtp_message_class extends email_message_class { /* Private variables */ var $smtp; var $line_break="\r\n"; var $delivery = 0; /* Public variables */ /* Allow Self Signed Certificate */ var $smtp_certificate = 0; // @flydev: https://processwire.com/talk/topic/5704-wiremailsmtp/page-5#entry113290 /* @horst: Allow Connections without Authentication */ var $allow_without_authentication = 0; /* @horst: Allow to define the crypto method for TLS connections */ var $smtp_tls_crypto_method = ''; /* @horst: Allow to define the crypto method for SSL connections */ var $smtp_ssl_crypto_method = ''; /* {metadocument} localhost Specify the domain name of the computer sending the message. This value is used to identify the sending machine to the SMTP server. When using the direct delivery mode, if this variable is set to a non-empty string it used to generate the Recieved header to show that the message passed by the specified host address. To prevent confusing directly delivered messages with spam, it is strongly recommended that you set this variable to you server host name. {/metadocument} */ var $localhost=""; /* {metadocument} smtp_host STRING Specify the address of the SMTP server. Set to the address of the SMTP server that will relay the messages. This variable is not used in direct delivery mode. {/metadocument} */ var $smtp_host="localhost"; /* {metadocument} smtp_port INTEGER 25 Specify the TCP/IP port of SMTP server to connect. Most servers work on port 25 . Certain e-mail services use alternative ports to avoid firewall blocking. Gmail uses port 465. {/metadocument} */ var $smtp_port=25; /* {metadocument} smtp_ssl BOOLEAN 0 Specify whether it should use secure connections with SSL to connect to the SMTP server. Certain e-mail services like Gmail require SSL connections. {/metadocument} */ var $smtp_ssl=0; /* {metadocument} smtp_start_tls BOOLEAN 0 Specify whether it should use secure connections starting TLS protocol after connecting to the SMTP server. Certain e-mail services like Hotmail require starting TLS protocol after the connection to the SMTP server is already established. {/metadocument} */ var $smtp_start_tls=0; /* {metadocument} smtp_http_proxy_host_name STRING Specify name of the host when the connection should be routed via an HTTP proxy. Leave empty if no proxy should be used. {/metadocument} */ var $smtp_http_proxy_host_name=''; /* {metadocument} smtp_http_proxy_host_port INTEGER 3128 Specify proxy port when the connection should be routed via an HTTP proxy. Change this variable if you need to use a proxy with a specific port. {/metadocument} */ var $smtp_http_proxy_host_port=3128; /* {metadocument} smtp_socks_host_name STRING Specify name of the host when the connection should be routed via a SOCKS protocol proxy. Leave empty if no proxy should be used. {/metadocument} */ var $smtp_socks_host_name = ''; /* {metadocument} smtp_socks_host_port INTEGER 1080 Specify proxy port when the connection should be routed via a SOCKS protocol proxy. Change this variable if you need to use a proxy with a specific port. {/metadocument} */ var $smtp_socks_host_port = 1080; /* {metadocument} smtp_socks_version STRING Specify protocol version when the connection should be routed via a SOCKS protocol proxy. Change this variable if you need to use a proxy with a specific SOCKS protocol version. {/metadocument} */ var $smtp_socks_version = '5'; /* {metadocument} smtp_direct_delivery BOOLEAN 0 Boolean flag that indicates whether the message should be sent in direct delivery mode. Set this to 1 if you want to send urgent messages directly to the recipient domain SMTP server. {/metadocument} */ var $smtp_direct_delivery=0; /* {metadocument} smtp_getmxrr STRING getmxrr Specify the name of the function that is called to determine the SMTP server address of a given domain. Change this to a working replacement of the PHP getmxrr() function if this is not working in your system and you want to send messages in direct delivery mode. {/metadocument} */ var $smtp_getmxrr="getmxrr"; /* {metadocument} smtp_exclude_address STRING Specify an address that should be considered invalid when resolving host name addresses. In some networks any domain name that does not exist is resolved as a sub-domain of the default local domain. If the DNS is configured in such way that it always resolves any sub-domain of the default local domain to a given address, it is hard to determine whether a given domain does not exist. If your network is configured this way, you may set this variable to the address that all sub-domains of the default local domain resolves, so the class can assume that such address is invalid. {/metadocument} */ var $smtp_exclude_address=""; /* {metadocument} smtp_user STRING Specify the user name for authentication. Set this variable if you need to authenticate before sending a message. {/metadocument} */ var $smtp_user=""; /* {metadocument} smtp_realm STRING Specify the user authentication realm. Set this variable if you need to authenticate before sending a message. {/metadocument} */ var $smtp_realm=""; /* {metadocument} smtp_workstation STRING Specify the user authentication workstation needed when using the NTLM authentication (Windows or Samba). Set this variable if you need to authenticate before sending a message. {/metadocument} */ var $smtp_workstation=""; /* {metadocument} smtp_authentication_mechanism STRING Specify the user authentication mechanism that should be used when authenticating with the SMTP server. Set this variable if you need to force the SMTP connection to authenticate with a specific authentication mechanism. Leave this variable with an empty string if you want the authentication mechanism be determined automatically from the list of mechanisms supported by the server. {/metadocument} */ var $smtp_authentication_mechanism=""; /* {metadocument} smtp_password STRING Specify the user authentication password. Set this variable if you need to authenticate before sending a message. {/metadocument} */ var $smtp_password=""; /* {metadocument} smtp_pop3_auth_host STRING Specify the server address for POP3 based authentication. Set this variable to the address of the POP3 server if the SMTP server requires POP3 based authentication. {/metadocument} */ var $smtp_pop3_auth_host=""; /* {metadocument} smtp_debug BOOLEAN 0 Specify whether it is necessary to output SMTP connection debug information. Set this variable to 1 if you need to see the progress of the SMTP connection and protocol dialog when you need to understand the reason for delivery problems. {/metadocument} */ var $smtp_debug=0; /* {metadocument} smtp_html_debug BOOLEAN 0 Specify whether the debug information should be outputted in HTML format. Set this variable to 1 if you need to see the debug output in a Web page. {/metadocument} */ var $smtp_html_debug=0; /* {metadocument} esmtp BOOLEAN 1 Specify whether the class should try to use Enhanced SMTP protocol features. It is recommended to leave this variable set to 1 so the class can take advantage of Enhanced SMTP protocol features. {/metadocument} */ var $esmtp=1; /* {metadocument} timeout INTEGER 25 Specify the connection timeout period in seconds. Change this value if for some reason the timeout period seems insufficient or otherwise it seems too long. {/metadocument} */ var $timeout=25; /* {metadocument} invalid_recipients ARRAY Return the list of recipient addresses that were not accepted by the SMTP server. Check this variable after attempting to send a message to figure whether there were any recipients that were rejected by the SMTP server. {/metadocument} */ var $invalid_recipients=array(); /* {metadocument} mailer_delivery smtp $Revision: 1.36 $ Specify the text that is used to identify the mail delivery class or sub-class. This text is appended to the X-Mailer header text defined by the mailer variable. Do not change this variable. {/metadocument} */ var $mailer_delivery='smtp $Revision: 1.36 $'; /* {metadocument} maximum_bulk_deliveries INTEGER 100 Specify the number of consecutive bulk mail deliveries without disconnecting. Lower this value if you have enabled the bulk mail mode but the SMTP server does not accept sending more than a number of messages within the same SMTP connection. Set this value to 0 to never disconnect during bulk mail mode unless an error occurs. {/metadocument} */ var $maximum_bulk_deliveries=100; Function SetRecipients(&$recipients,&$valid_recipients) { for($valid_recipients=$recipient=0,Reset($recipients);$recipientsmtp->SetRecipient($address)) $valid_recipients++; else $this->invalid_recipients[$address]=$this->smtp->error; } return(1); } Function ResetConnection($error) { if(IsSet($this->smtp)) { if(!$this->smtp->Disconnect() && strlen($error) == 0) $error = $this->smtp->error; UnSet($this->smtp); } if(strlen($error)) $this->OutputError($error); return($error); } Function StartSendingMessage() { if(function_exists("class_exists") && !class_exists("smtp_class")) return("the smtp_class class was not included"); if(IsSet($this->smtp)) return(""); $this->smtp=new smtp_class; $this->smtp->localhost=$this->localhost; $this->smtp->host_name=$this->smtp_host; $this->smtp->host_port=$this->smtp_port; $this->smtp->ssl=$this->smtp_ssl; $this->smtp->smtp_ssl_crypto_method=$this->smtp_ssl_crypto_method; // @horst $this->smtp->start_tls=$this->smtp_start_tls; $this->smtp->smtp_tls_crypto_method=$this->smtp_tls_crypto_method; // @horst $this->smtp->http_proxy_host_name=$this->smtp_http_proxy_host_name; $this->smtp->http_proxy_host_port=$this->smtp_http_proxy_host_port; $this->smtp->socks_host_name=$this->smtp_socks_host_name; $this->smtp->socks_host_port=$this->smtp_socks_host_port; $this->smtp->socks_version=$this->smtp_socks_version; $this->smtp->timeout=$this->timeout; $this->smtp->debug=$this->smtp_debug; $this->smtp->html_debug=$this->smtp_html_debug; $this->smtp->direct_delivery=$this->smtp_direct_delivery; $this->smtp->getmxrr=$this->smtp_getmxrr; $this->smtp->exclude_address=$this->smtp_exclude_address; $this->smtp->pop3_auth_host=$this->smtp_pop3_auth_host; $this->smtp->user=$this->smtp_user; $this->smtp->realm=$this->smtp_realm; $this->smtp->workstation=$this->smtp_workstation; $this->smtp->authentication_mechanism=$this->smtp_authentication_mechanism; $this->smtp->password=$this->smtp_password; $this->smtp->esmtp=$this->esmtp; $this->smtp->smtp_certificate = $this->smtp_certificate; // @flydev: https://processwire.com/talk/topic/5704-wiremailsmtp/page-5#entry113290 $this->smtp->allow_without_authentication = $this->allow_without_authentication; // @horst if($this->smtp->Connect()) { $this->delivery = 0; return(""); } return($this->ResetConnection($this->smtp->error)); } Function SendMessageHeaders($headers) { $header_data=""; $date=date("D, d M Y H:i:s T"); if($this->smtp_direct_delivery && strlen($this->localhost)) { $local_ip=gethostbyname($this->localhost); $header_data.=$this->FormatHeader("Received","FROM ".$this->localhost." ([".$local_ip."]) BY ".$this->localhost." ([".$local_ip."]) WITH SMTP; ".$date)."\r\n"; } for($message_id_set=$date_set=0,$header=0,$return_path=$from=$to=$recipients=array(),Reset($headers);$headerGetRFC822Addresses($headers[$header_name],$from); break; case "to": $error=$this->GetRFC822Addresses($headers[$header_name],$to); break; case "cc": case "bcc": $this->GetRFC822Addresses($headers[$header_name],$recipients); break; case "date": $date_set=1; break; case "message-id": $message_id_set=1; break; } if(strcmp($error,"")) return($this->ResetConnection($error)); if(strtolower($header_name)=="bcc") continue; $header_data.=$this->FormatHeader($header_name,$headers[$header_name])."\r\n"; } if(count($from)==0) return($this->ResetConnection("it was not specified a valid From header")); Reset($return_path); Reset($from); $this->invalid_recipients=array(); if(!$this->smtp->MailFrom(count($return_path) ? Key($return_path) : Key($from))) return($this->ResetConnection($this->smtp->error)); $r = 0; if(count($to)) { if(!$this->SetRecipients($to,$valid_recipients)) return($this->ResetConnection($this->smtp->error)); $r += $valid_recipients; } if(!$date_set) $header_data.="Date: ".$date."\r\n"; if(!$message_id_set && $this->auto_message_id) { $sender=(count($return_path) ? Key($return_path) : Key($from)); $header_data.=$this->GenerateMessageID($sender)."\r\n"; } if(count($recipients)) { if(!$this->SetRecipients($recipients,$valid_recipients)) return($this->ResetConnection($this->smtp->error)); $r += $valid_recipients; } if($r==0) return($this->ResetConnection("it were not specified any valid recipients")); if(!$this->smtp->StartData() || !$this->smtp->SendData($header_data."\r\n")) return($this->ResetConnection($this->smtp->error)); return(""); } Function SendMessageBody($data) { return($this->smtp->SendData($this->smtp->PrepareData($data)) ? "" : $this->ResetConnection($this->smtp->error)); } Function EndSendingMessage() { return($this->smtp->EndSendingData() ? "" : $this->ResetConnection($this->smtp->error)); } Function StopSendingMessage() { ++$this->delivery; if($this->bulk_mail && !$this->smtp_direct_delivery && ($this->maximum_bulk_deliveries == 0 || $this->delivery < $this->maximum_bulk_deliveries)) return(""); return($this->ResetConnection('')); } Function ChangeBulkMail($on) { if($on || !IsSet($this->smtp)) return(1); return($this->smtp->Disconnect() ? "" : $this->ResetConnection($this->smtp->error)); } }; /* {metadocument}
{/metadocument} */ ?>