For security reasons, outgoing connections to port 25 are blocked on all servers of the virtual UNIX hosting, so you need to use an alternative port number, which you need to check with the owner of the SMTP server. If sending will be done via our SMTP server, use port 26.
An example of authorized mail sending using an smtp server.
$mhSmtpMail_Server = "smtp.domain.tld"; // Specify the SMTP server address
$mhSmtpMail_Port ="26"; // SMTP server port.
$mhSmtpMail_Username = "postmaster@domain.tld"; // Mailbox (user) name
$mhSmtpMail_Password = "password"; // and the password to it.
$mhSmtpMail_From ="Sender's name"; // Sender's name in the field From
function MailSmtp($to, $subject, $message, $headers)
{
global $mhSmtpMail_Server, $mhSmtpMail_Port, $mhSmtpMail_Username, $mhSmtpMail_Password;
$mhSmtpMail_localhost = "localhost";
$mhSmtpMail_newline = "rn";
$mhSmtpMail_timeout = "30";
$smtpConnect = fsockopen($mhSmtpMail_Server, $mhSmtpMail_Port, $errno, $errstr, $mhSmtpMail_timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
fputs($smtpConnect,"AUTH LOGIN" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($mhSmtpMail_Username) . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authmhSmtpMail_username'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($mhSmtpMail_Password) . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authmhSmtpMail_password'] = "$smtpResponse";
fputs($smtpConnect, "HELO $mhSmtpMail_localhost" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
fputs($smtpConnect, "MAIL FROM: $mhSmtpMail_Username" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailmhSmtpMail_fromresponse'] = "$smtpResponse";
fputs($smtpConnect, "RCPT TO: $to" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
fputs($smtpConnect, "DATA" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
fputs($smtpConnect, "Subject: $subjectrn$headersrnrn$messagern.rn");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
fputs($smtpConnect,"QUIT" . $mhSmtpMail_newline);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>
Download an example of a ready-made script with the MailSmtp() function: smtpauth.php.sample
You can use the MailSmtp() function described above to directly replace the mail() function. Consider an example of the simplest form in PHP:
if ($_POST['submit'])
{
$to = $_POST['to'];
$subject = $_POST['subj'];
$message = $_POST['msg'];
// Message headers, they define the encoding of the message, fields From, To, etc.
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=windows-1251rn";
$headers .= "To: $torn";
$headers .= "From: Sender's name " ;
// mail ($to, $subject, $message, $headers);
require_once "smtpauth.php";
MailSmtp ($to, "=?windows-1251?B?".base64_encode($subject)."?=", $message, $headers);
}
?>