Visualizzazione dei risultati da 1 a 5 su 5

Discussione: invio mail da form

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2011
    Messaggi
    10

    invio mail da form

    salve,

    ho un problema, ho compilato la funzione mail() di php per l'invio ma non va, così mi son deciso ad impostare (tramite la classe PHPMAILER) il tutto manualmente, ma probabilmente ho fatto qualche casino...

    mi sapreste aiutare

    Codice PHP:
        include('class.phpmailer.php');
        
    $mittente $_POST['mail']; 
        
    $oggetto $_POST['oggetto'];
        
    $testo $_POST['corpo'];

        
    $destinatario="MIA MAIL";
        
        if (
    smtpmailer($destinatario$destinatario$mittente$oggetto$testo)) {
        echo 
    "inviata";
        }

     
        function 
    smtpmailer($to$from$from_name$subject$body) {
            global 
    $error;
            
    $mail = new PHPMailer();  // create a new object
            
    $mail->IsSMTP(); // enable SMTP
            
    $mail->SMTPDebug 0;  // debugging: 1 = errors and messages, 2 = messages only
            
    $mail->SMTPAuth true;  // authentication enabled
            
    $mail->SMTPSecure 'ssl'// secure transfer enabled REQUIRED for Gmail
            
    $mail->Host 'smtp.gmail.com';
            
    $mail->Port 465;
            
    $mail->Username "MIA MAIL";
            
    $mail->Password "MIA PASSWORD";
            
    $mail->SetFrom($from$from_name);
            
    $mail->Subject $subject;
            
    $mail->Body $body;
            
    $mail->AddAddress($to);
            if(!
    $mail->Send()) {
                
    $error 'Mail error: '.$mail->ErrorInfo;
                return 
    false;
            } else {
                
    $error 'Message sent!';
                return 
    true;
            }
        } 



    in pratica il form (una volta recuperati i parametri) dovrebbe inviare una mail al mio indirizzo di gmail (autoinviata). la mail dovrebbe contenere:
    -oggetto: oggetto definito dal mittente
    -corpo: indirizzo mail del mittente e corpo del messaggio.



    grazie mille

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,503
    E il problema che riscontri qual'è?
    Un erorre o non ti arrivano le email? Hai controllato se arrivano nello spam?

    Codice PHP:
    if(!$mail->Send()) {
       
    $error 'Mail error: '.$mail->ErrorInfo;
       return 
    false;
    } else { 
    $error = 'Mail error: '.$mail->ErrorInfo;

    è inutile, perché non la stampi e se c'è un errore non ti viene notificato.
    Togli il return e metti
    echo $error;
    exit;

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2014
    Messaggi
    1
    Ciao, come ha detto @Alhazred, nel caso di un errore questo non viene stampato, prova questo:
    Codice PHP:
    <?PHP
        define
    ("my_email""");
        
    define("my_password""");
        
        require_once(
    "class.phpmailer.php");
        
    $error "";
        function 
    sendMail($to$from$from_name$subject$body){
            global 
    $error;
            
    $mail = new PHPMailer();
            
    $mail->IsSMTP();
            
    $mail->SMTPDebug    0;
            
    $mail->SMTPAuth     true;
            
    $mail->SMTPSecure     "ssl";
            
    $mail->Host            "smtp.gmail.com";
            
    $mail->Port            465;
            
    $mail->Username        my_email;
            
    $mail->Password        my_password;
            
    $mail->SetFrom($from$from_namefalse);
            
    $mail->Subject        $subject;
            
    $mail->Body            $body;
            
    $mail->AddAddress($to);
            if(!
    $mail->Send()){
                
    $error .= $mail->ErrorInfo;
                return 
    false;
            }
            return 
    true;
        }
        
    $sendMail sendMail("to@gmail.com","from@gmail.com","fromName","subject","body");
        if(
    $sendMail === true)
            echo 
    "Mail inviata.";
        else
            echo 
    "C'è stato un errore durante l'invio della mail: {$error}.";

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2011
    Messaggi
    10
    ho provato ma mi restituisce questo errore:

    SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host. C'è stato un errore durante l'invio della mail: SMTP Error: Could not connect to SMTP host..

    settato tutto (apparentemente) in maniera corretta. non ho blocchi di firewall o altro.

  5. #5
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,503
    Se ti dice così, vuol dire che il mailserver di Google non ha accettato la connessione, rivedi i parametri.
    Sei in locale? Controlla se hai ssl attivo sul tuo php.ini

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.