2013-01-10 10 views
7

Ich weiß, wie SMTP verwenden, um mit PHPMailer:PHPMailer Standardkonfiguration 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 

Und es funktioniert gut. Aber meine Frage ist:

Wie kann ich PHPMailer konfigurieren, um diese Einstellungen standardmäßig zu verwenden, so dass ich sie nicht jedes Mal angeben muss, die ich senden möchte?

+1

Wenn es um Wordpress geht, kann die Check -> WordPress \ wp-includes \ class-phpmailer.php Datei – swapnesh

Antwort

14

Erstellen Sie eine Funktion und fügen Sie sie hinzu.

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; 
} 

Und rufen Sie create_phpmailer() auf, um ein neues PHPMailer-Objekt zu erstellen.

Oder Sie können Ihre eigene Unterklasse ableiten, die die Parameter setzt:

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 
    } 
} 

und nutzen neue MyMailer().

+0

Kann ich nicht nur die Datei class.phpmailer.php bearbeiten? Standardmäßig startet es (zumindest die aktuelle Version) mit: 'class PHPMailer { public $ Version = '5.2.9'; öffentliche $ Priorität = 3; public $ CharSet = 'iso-8859-1'; public $ ContentType = 'text/plain'; public $ Encoding = '8bit'; public $ ErrorInfo = ''; public $ From = 'root @ localhost'; public $ FromName = 'Root User'; '... also, wenn ich den Wert von' $ From' zB in 'myname @ example.com' ändere – koljanep

1

Kann ich nicht nur die Datei class.phpmailer.php bearbeiten?

Es ist am besten, die Klassendateien nicht selbst zu bearbeiten, weil dadurch der Code schwerer zu pflegen ist.

0

Sie können auch diese Haken verwenden:

/** 
    * 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)); 

Von der Quelle der wp_mail Funktion selbst direkt die phpmailer Klasse zu ändern.

+0

Die Codex-Seite dafür ist hier: https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init – shahar

Verwandte Themen