For security reasons, outbound connections to port 25 are blocked on all UNIX virtual hosting servers. You must therefore use an alternative port number, which you must obtain from the owner of the SMTP server. If you want to send via our SMTP server, use port 26.

An example of authorized email sending via an SMTP server.


<?php

 $mhSmtpMail_Server     
"smtp.domain.tld";       // Enter the SMTP server address
 
$mhSmtpMail_Port       "26";                  // SMTP-Server Port.
 
$mhSmtpMail_Username   "postmaster@domain.tld"// Mailbox (user) name
 
$mhSmtpMail_Password   "password";              // and password
 
$mhSmtpMail_From       "Имя отправителя";       //Sender's name in the From field

function MailSmtp($to$subject$message$headers)

{

  global 
$mhSmtpMail_Server$mhSmtpMail_Port$mhSmtpMail_Username$mhSmtpMail_Password;


  
$mhSmtpMail_localhost  "localhost";
  
$mhSmtpMail_newline    "\r\n";
  
$mhSmtpMail_timeout    "30";

  
$smtpConnect fsockopen($mhSmtpMail_Server$mhSmtpMail_Port$errno$errstr$mhSmtpMail_timeout);

  
$smtpResponse fgets($smtpConnect515);

  if(empty(
$smtpConnect))
    {
      
$output "Failed to connect: $smtpResponse";

      return 
$output;
    }
  else
    {
      
$logArray['connection'] = "Connected: $smtpResponse";
    }

  
fputs($smtpConnect,"AUTH LOGIN" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['authrequest'] = "$smtpResponse";

  
fputs($smtpConnectbase64_encode($mhSmtpMail_Username) . $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['authmhSmtpMail_username'] = "$smtpResponse";

  
fputs($smtpConnectbase64_encode($mhSmtpMail_Password) . $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['authmhSmtpMail_password'] = "$smtpResponse";

  
fputs($smtpConnect"HELO $mhSmtpMail_localhost" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['heloresponse'] = "$smtpResponse";

  
fputs($smtpConnect"MAIL FROM: $mhSmtpMail_Username" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['mailmhSmtpMail_fromresponse'] = "$smtpResponse";

  
fputs($smtpConnect"RCPT TO: $to" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['mailtoresponse'] = "$smtpResponse";

  
fputs($smtpConnect"DATA" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$logArray['data1response'] = "$smtpResponse";

  
fputs($smtpConnect"Subject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");


  
$smtpResponse fgets($smtpConnect515);
  
$logArray['data2response'] = "$smtpResponse";

  
fputs($smtpConnect,"QUIT" $mhSmtpMail_newline);

  
$smtpResponse fgets($smtpConnect515);
  
$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. Let's look at an example of the simplest form in PHP:


<?

if ($_POST['submit'])

 {

  
$to      $_POST['to'];
  
$subject $_POST['subj'];
  
$message $_POST['msg'];

  
  
// Message headers, which define the message encoding, From, To fields, etc.
  
$headers "MIME-Version: 1.0\r\n";
  
$headers .= "Content-type: text/html; charset=windows-1251\r\n";
  
$headers .= "To: $to\r\n";
  
$headers .= "From: Имя отправителя <postmaster@domain.tld>";

  
//  mail ($to, $subject, $message, $headers);
 
  
require_once "smtpauth.php";
  
MailSmtp ($to"=?windows-1251?B?".base64_encode($subject)."?="$message$headers);

}


?>

<form action="" method="post">
  <pre>
    To:   <input type="text" name="to">
    Subj: <input type="text" name="subj">

    Msg:  <input type="text" name="msg">
    <input type="submit" value="Send mail!" name="submit">
  </pre>
</form>    

In order for this form to work without the mail() function, we included the smtpauth.php file via require_once and called the MailSmtp() function described in it, with arguments similar to mail(). At the same time, we commented out the mail() call itself in the code to avoid errors when executing the script.

Răspunsul a fost util? 26 utilizatori au considerat informația utilă (95 Voturi)