2013-01-10 40 views
7

tôi biết làm thế nào để sử dụng SMTP với PHPMailer:PHPMailer cấu hình mặc định 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 

Và nó hoạt động tốt. Nhưng câu hỏi của tôi là:

Làm cách nào để cấu hình PHPMailer sử dụng các cài đặt này theo mặc định, để tôi không phải chỉ định chúng mỗi lần tôi muốn gửi thư?

+1

Nếu nó là về wordpress kiểm tra -> wordpress \ wp-include \ class-phpmailer.php tập tin – swapnesh

Trả lời

14

Tạo chức năng và bao gồm/sử dụng nó.

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

Và gọi create_phpmailer() để tạo đối tượng PHPMailer mới.

Hoặc bạn có thể lấy được lớp con riêng của bạn, thiết lập các thông số:

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

và sử dụng MyMailer mới().

+0

Tôi có thể không chỉ chỉnh sửa các tập tin class.phpmailer.php? Theo mặc định nó (ít nhất là phiên bản hiện tại) bắt đầu bằng: 'class PHPMailer { public $ Version = '5.2.9'; mức độ ưu tiên công khai $ = 3; công khai $ CharSet = 'iso-8859-1'; công khai $ ContentType = 'text/plain'; công khai $ Encoding = '8bit'; public $ ErrorInfo = ''; công khai $ From = 'root @ localhost'; public $ FromName = 'Root User'; '... vì vậy nếu tôi thay đổi giá trị của' $ From' thành, giả sử, 'myname @ example.com' – koljanep

1

Tôi không chỉ chỉnh sửa tệp class.phpmailer.php?

Tốt nhất là không tự chỉnh sửa các tệp lớp vì nó làm cho mã khó duy trì hơn.

0

Bạn cũng có thể sử dụng hook này:

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

Từ nguồn của hàm wp_mail bản thân để sửa đổi các lớp PHPMailer trực tiếp.

+0

Trang codex cho là ở đây: https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init – shahar

Các vấn đề liên quan