2017-05-12 3 views
0

사용자가 양식을 작성하고 요청서를 제출하는 등의 특정 작업을 수행 할 때 이메일을 보내야합니다. 이는 다른 페이지에서 발생합니다.PHPMailer - 기능이있는 이메일 보내기

나는 PHPMailer의 기본 사용은 다음과 같습니다 알고

<?php 
require 'PHPMailerAutoload.php'; 
$mail = new PHPMailer; 
$mail->setFrom('[email protected]', 'Your Name'); 
$mail->addAddress('[email protected]', 'My Friend'); 
$mail->Subject = 'First PHPMailer Message'; 
$mail->Body  = 'Hi! This is my first e-mail sent through PHPMailer.'; 
if(!$mail->send()) { 
    echo 'Message was not sent.'; 
    echo 'Mailer error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
} 

가 가능 메일 기능마다에 사용자 이름, 암호 및 메시지를 다시 지정할 필요없이 메일() 클래스를 사용하는 것입니다 ?? 다음 PHPMailer의 인스턴스에 주어진 변수를 전달

sendMail($from, $to, $subject, $body); 

:

은 본질적으로는 같은 fuction를이 있을까?

this question과 유사합니다.

+1

메일러 클래스를 소유하고 한 번 개체를 얻고 사용자 이름, 암호 및 메시지를 한 번 설정 한 다음 개체를 sendMail에 다시 사용 하시겠습니까? – Edwin

+0

PHPMailer를 확장 한 메일 클래스를 만들 수 있다고 생각합니다. 사용자 이름과 암호를 한 번만 지정하면 설정 한 자격 증명을 사용하지 않고 어디에서나 자신의 클래스를 사용할 수 있습니다. –

+0

나는 이것을 시험해 볼 것이다. –

답변

1
require 'vendor/autoload.php'; 
class MyMail extends PHPMailer 
{ 
    private $_host = 'your stmp server name'; 
    private $_user = 'your smtp username'; 
    private $_password = 'your password'; 

    public function __construct($exceptions=true) 
    { 
     $this->Host = $this->_host; 
     $this->Username = $this->_user; 
     $this->Password = $this->_password; 
     $this->Port = 465; 
     $this->SMTPAuth = true; 
     $this->SMTPSecure = 'ssl'; 
     $this->isSMTP(); 
     parent::__construct($exceptions); 
    } 

    public function sendMail($from, $to, $subject, $body) 
    { 
     $this->setFrom($from); 
     $this->addAddress($to); 
     $this->Subject = $subject; 
     $this->Body = $body; 

     return $this->send(); 
    } 
} 
$m = new MyMail(); 
$result = $m->sendMail('[email protected]', '[email protected]', 'Test from script', 'test message from script'); 
+0

* "나는 생각한다"* 답변은 코멘트 여야합니다. –

+0

네, 괜찮아요, 내가 그것을 바꿔서 조언을했습니다 –

+0

쿨 :-) 나는 당신의 코드를 들여 썼습니다. –