2013-01-10 11 views
7

so come usare SMTP con PHPMailer:PHPMailer configurazione predefinita SMTP

$mail    = new PHPMailer(); 
$mail->IsSMTP(); // telling the class to use SMTP 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->Host  = "mail.yourdomain.com"; // sets the SMTP server 
$mail->Username = "[email protected]"; // SMTP account username 
$mail->Password = "yourpassword";  // SMTP account password 

e funziona benissimo. Ma la mia domanda è:

Come posso configurare PHPMailer per utilizzare queste impostazioni di default, in modo da non doverle specificare ogni volta che voglio inviare la posta?

+1

Se si tratta di wordpress il check -> wordpress \ wp-include il file \ class-phpmailer.php – swapnesh

risposta

14

Creare una funzione e includerla/utilizzarla.

function create_phpmailer() { 
    $mail    = new PHPMailer(); 
    $mail->IsSMTP(); // telling the class to use SMTP 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->Host  = "mail.yourdomain.com"; // sets the SMTP server 
    $mail->Username = "[email protected]"; // SMTP account username 
    $mail->Password = "yourpassword";  // SMTP account password 
    return $mail; 
} 

E chiamare create_phpmailer() per creare un nuovo oggetto PHPMailer.

Oppure si può ricavare la propria sottoclasse, che stabilisce i parametri:

class MyMailer extends PHPMailer { 
    public function __construct() { 
    parent::__construct(); 
    $this->IsSMTP(); // telling the class to use SMTP 
    $this->SMTPAuth = true;     // enable SMTP authentication 
    $this->Host  = "mail.yourdomain.com"; // sets the SMTP server 
    $this->Username = "[email protected]"; // SMTP account username 
    $this->Password = "yourpassword";  // SMTP account password 
    } 
} 

e utilizzare le nuove MyMailer().

+0

Non posso semplicemente modificare il file class.phpmailer.php? Di default (almeno la versione corrente) inizia con: 'classe PHPMailer { public $ Version = '5.2.9'; public $ Priorità = 3; public $ CharSet = 'iso-8859-1'; public $ ContentType = 'text/plain'; public $ Encoding = '8bit'; public $ ErrorInfo = ''; pubblico $ Da = 'root @ localhost'; public $ FromName = 'Root User'; '... quindi cosa succede se cambio il valore di' $ From' a, diciamo, 'myname @ example.com' – koljanep

1

Posso semplicemente modificare il file class.phpmailer.php?

È consigliabile non modificare direttamente i file di classe perché rende il codice più difficile da mantenere.

0

È anche possibile utilizzare questo gancio:

/** 
    * Fires after PHPMailer is initialized. 
    * 
    * @since 2.2.0 
    * 
    * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference. 
    */ 
    do_action_ref_array('phpmailer_init', array(&$phpmailer)); 

Dalla sorgente della funzione wp_mail stessa per modificare direttamente la classe phpmailer.

+0

La pagina del codice per questo è qui: https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init – shahar

Problemi correlati