Spedire con SMTP autenticato tramite PHP 4


I server di posta oggi giorno sono diventi sempre più restrittivi e ci si trova molto spesso nella necesità che per poter spedire una mail dal nostro form dei contatti sia necessario usare un server SMTP che richiede autenticazione.

Spero quindi che questo codice lo possiate riadattare alle vostre esigenze in modo semplice.

In questa parte vi mostro cosa inserire in una pagina php per poter richiamare la nostra funzione per l’invio della mail:

<?php
$to = "caselladestinatario@dominiodestinatario.xxx";
$nameto = "Nome del destinatario";  
$from = "casellamittente@dominiomittente.xxx"; 
$namefrom = "Nome del mittente"; 
$subject = "Messaggio autenticato!";  
$message = "Messaggio di prova autenticato sulla porta 587!"; 
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);  
?>

Nella parte qui di seguito vi mostro invece la funzione authSendEmail:

<?php
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
 { 
 /* * * * CONFIGURAZIONE * * * */ 
$smtpServer = "server_smtp_autenticato";  //esempio: mail.191.it
$port = "587";  
$timeout = "30"; 
$username = "nome_utente_autenticato"; 
$password = "password_nome_utente_autenticato"; 
$localhost = "127.0.0.1";  //indirizzo del server che vi ospita
$newLine = "\r\n";  
/* * * * CONFIGURAZIONE * * * * */

$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout); 
$smtpResponse = fgets($smtpConnect, 515); 
if(empty($smtpConnect))  
{  
$output = "Connessione fallita: $smtpResponse"; 
return $output; 
}  
else 
{ 
$logArray['connection'] = "Connesso: $smtpResponse";
} 

//Richiesta di  Auth Login  
fputs($smtpConnect,"AUTH LOGIN" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authrequest'] = "$smtpResponse"; 

//Invio dello username 
fputs($smtpConnect, base64_encode($username) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authusername'] = "$smtpResponse"; 

//Invio della password
fputs($smtpConnect, base64_encode($password) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authpassword'] = "$smtpResponse"; 

//Salutiamo in nostro server SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['heloresponse'] = "$smtpResponse";  

//Email Da
fputs($smtpConnect, "MAIL FROM: $from" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['mailfromresponse'] = "$smtpResponse"; 

//Email A 
fputs($smtpConnect, "RCPT TO: $to" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['mailtoresponse'] = "$smtpResponse"; 
//Corpo del messaggio
fputs($smtpConnect, "DATA" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['data1response'] = "$smtpResponse"; 
//Construtto dell'intestazione
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine; 
$headers .= "To: $nameto &lt;$to&gt;" . $newLine; 
$headers .= "From: $namefrom &lt;$from&gt;" . $newLine; 
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['data2response'] = "$smtpResponse"; 
// Salutiamo il server SMTP 
fputs($smtpConnect,"QUIT" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['quitresponse'] = "$smtpResponse"; 

//Se volete visualizzare il log errori decommentate la riga sotto:
//print_r($logArray);
}  
?>

Spero di esservi stato utile. Buona email a tutti.

Clicca per votare questo articolo!
[Voti: 2 Media: 5]
Scritto da Alessandro Consorti

Rispondi a Pino Annulla risposta

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

4 commenti su “Spedire con SMTP autenticato tramite PHP

  • Pino

    Ciao e grazie della condivisione.
    L’ho provato e funziona, l’unica cosa è che nelle intestazioni di ritorno si lamentava la mancanza della data nell’header
    Ho risolto inserendo questa riga:
    $headers .= “Date: “.date(“r”).”” . $newLine;
    (e dopo lo spam-score e lo spam-level assegnato dal server è diminuito.)

    Poi mi riportava questo errore:
    X-Amavis-Alert: BAD HEADER, Duplicate header field: “From”

    ho risolto commentando queste 2 righe:
    //$headers .= “To: $nameto <$to>” . $newLine;
    //$headers .= “From: $namefrom <$from>” . $newLine;

    Non so se il problema è generato dalle impostazioni dei server di posta del destinatario o meno (come sistemista valgo pochino)

    Grazie ancora.

  • Pino

    alcuni server si rifiutavano l’invio,
    ho risolto in questo modo

    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
    {
    /* * * * CONFIGURAZIONE * * * */
    $smtpServer = “mail.dominio.com”; //esempio: mail.191.it
    $port = “25”;
    $timeout = “30”;
    $username = “info@dominio.com”;
    $password = “********************”;
    $localhost = “127.0.0.1”; //indirizzo del server che vi ospita
    $newLine = “\r\n”;
    /* * * * CONFIGURAZIONE * * * * */

    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);
    if(empty($smtpConnect))
    {
    $output = “Connessione fallita: $smtpResponse”;
    return $output;
    }
    else
    {
    $logArray[‘connection’] = “Connesso: $smtpResponse”;
    }

    //Richiesta di Auth Login
    fputs($smtpConnect,”AUTH LOGIN” . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘authrequest’] = “$smtpResponse”;

    //Invio dello username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘authusername’] = “$smtpResponse”;

    //Invio della password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘authpassword’] = “$smtpResponse”;

    //Salutiamo in nostro server SMTP
    fputs($smtpConnect, “HELO $localhost” . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘heloresponse’] = “$smtpResponse”;

    //Email Da
    fputs($smtpConnect, “MAIL FROM: $from” . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘mailfromresponse’] = “$smtpResponse”;

    //Email A
    fputs($smtpConnect, “RCPT TO: $to” . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘mailtoresponse’] = “$smtpResponse”;
    //Corpo del messaggio
    fputs($smtpConnect, “DATA” . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘data1response’] = “$smtpResponse”;
    //Construtto dell’intestazione
    $headers = “MIME-Version: 1.0” . $newLine;
    $headers .= “Content-type: text/html; charset=iso-8859-1” . $newLine;
    //$headers .= “To: $nameto <$to>” . $newLine;
    //$headers .= “To: $to” . $newLine;
    //$headers .= “From: $namefrom <$from>” . $newLine;
    //$headers .= “From: $from” . $newLine;

    $headers .= “X-Mailer: Form-sito-web” . $newLine;
    $headers .= “Date: “.date(“r”).”” . $newLine;

    //fputs($smtpConnect, “To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n”);
    fputs($smtpConnect, “To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\r\n.”.$newLine);

    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘data2response’] = “$smtpResponse”;
    // Salutiamo il server SMTP
    //fputs($smtpConnect,”QUIT” . $newLine);
    fputs($smtpConnect,”QUIT” . $newLine .”\r\n”);
    fputs($smtpConnect,”.”);
    fputs($smtpConnect,”\r\n”);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray[‘quitresponse’] = “$smtpResponse”;

    //Se volete visualizzare il log errori decommentate la riga sotto:
    //print_r($logArray);
    }

    ////////////////////END INVIO EMAIL////////////////////////////