2012-04-19 14 views
0

私はPHPMailerを使用していますが、それはあまりにも多くの行を取っていると私はそれをより簡潔にすることができると思う。以下のパラメータのいくつかは、すべてが同じ電子メールアドレスから送信されるため、冗長です。PHPMailerでメールを送信する方法はありますか?

私はいつもどこかに同じであるパラメータ置くことができる場合、私は実際に聞いてるのよ

(FROMNAME、から、ユーザー名、パスワード、ホスト、ポスト、SMTPSecure、SMTPAUTH)

私はそれをどのように行うことができますか?前もって感謝します。よろしく

私のコードは次のとおりです。

include("classes/phpmailer/class.phpmailer.php"); 
include("classes/phpmailer/class.smtp.php"); // note, this is optional 

$mail    = new PHPMailer(); 
$body    = 'This is the body'; 

$mail->IsSMTP(); 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 465;     // set the SMTP port 

$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "mypassword";   // GMAIL password 

$mail->From  = "[email protected]"; 
$mail->FromName = "Admin"; 
$mail->Subject = "Welcome"; 
$mail->AltBody = 'This is the body'; //Text Body 
$mail->WordWrap = 100; // set word wrap 

$mail->MsgHTML($body); 
$mail->AddReplyTo("[email protected]","Admin"); 
$mail->SMTPDebug = 1; 
$mail->AddAddress($email,$firstname." ".$surname); 

$mail->IsHTML(true); // send as HTML 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
// nothing is displayed 
} 
+0

コードを分割して再利用可能な部分に分割する方法や、行数を減らす方法を尋ねていますか? –

+0

こんにちは。私は実際にどこか他の場所(From、FromName、Username、Password、Host、Post、SMTPSecure、SMTPAuth)で常に同じパラメータを置くことができるかどうかを尋ねています。よろしくお願いします。mail();を使用するオプションが少なくてよければ、 – alexx0186

+0

よろしくお願いします。 –

答えて

2
function New_Mail($body, $subject, $altBody, $wordwrap) 
{ 
    $mail    = new PHPMailer(); 

    $mail->IsSMTP(); 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
    $mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
    $mail->Port  = 465;     // set the SMTP port 

    $mail->Username = "[email protected]"; // GMAIL username 
    $mail->Password = "mypassword";   // GMAIL password 

    $mail->From  = "[email protected]"; 
    $mail->FromName = "Admin"; 
    $mail->Subject = $subject; 
    $mail->AltBody = $altBody; //Text Body 
    $mail->WordWrap = $wordwrap; // set word wrap 

    $mail->MsgHTML($body); 
    $mail->AddReplyTo("[email protected]","Admin"); 
    $mail->SMTPDebug = 1; 
    $mail->IsHTML(true); // send as HTML 

    return $mail; 
} 

$mail = New_Mail("this is the body", "Welcome", "Another body", 100); 
if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
// nothing is displayed 
} 

今、あなたがその機能を再利用します。

+0

こんにちは、これは私が探していたようだ。ありがとうございました。よろしくお願いします。 – alexx0186

+0

PHPMailer Classファイルに記述してください。ありがとう – alexx0186

+0

いいえ...元のライブラリを変更しないでください。それを自分のコードに入れてください。 – Jack

2

すべてのデフォルト値を初期化し、唯一の変更値を設定phpmailerのクラスのラッパークラスを作成します。

私は、サイト開発私Kohanaの中に以下のクラスを使用し

<?php 
/** 
* @Author Mārtiņš Briedis 
* @Date: 2011-12-11 
*/ 

class Mail{ 
    /** 
    * Get an instance of PHPMailer 
    * @static 
    * @return PHPMailer 
    */ 
    public static function factory(){ 
     $inst = new PHPMailer(true); 

     // Initialize defaults 
     $conf = array(..); // Array with configuration data 
     $inst->SetFrom($conf['from_mail'], $conf['from_name']); 
     $inst->AddReplyTo($conf['from_mail'], $conf['from_name']); 
     $inst->Subject = $conf['subject']; 

     // If development environment, use GMAIL smtp server 
     $inst->IsSMTP(); // telling the class to use SMTP 
     $inst->Host = "[email protected]"; // SMTP server 
     $inst->SMTPAuth = true; // enable SMTP authentication 
     $inst->SMTPSecure = "ssl"; // sets the prefix to the servier 
     $inst->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server 
     $inst->Port = 465; // set the SMTP port for the GMAIL server 
     $inst->Username = $conf['mail']; // GMAIL username 
     $inst->Password = $conf['password']; // GMAIL password 

     return $inst; 
    } 

    /** 
    * To get an instance, one should use factory method, not constructor 
    */ 
    public function __construct(){ 
     throw new Exception('To get an instance, use static factory() method!'); 
    } 
} 

をそしてちょうど使用:

$mail = Mail::factory(); 
$mail->AddAddress($user->mail, $user->full_name); 
$mail->Subject = "New password"; 
$mail->Send(); 
+0

こんにちは、お返事ありがとうございます。私はそのKohannaフレームワークをインストールする必要があると思いますか?私はOPを編集しました。「私はいつも同じ(From、FromName、Username、Password、Host、Post、SMTPSecure、SMTPAuth)というパラメータを置くことができるかどうかを実際に尋ねています。これは私がそれをするのに役立つだろうか?ありがとうございます – alexx0186

+0

いいえ、あなたはコハナを必要としません。 – Jack

+0

コードを編集しました。いいえ、特定のフレームワークは必要ありません。私のようなシンプルなクラスを作成し、それを使用するだけです。 あなたはDRY(自分自身を繰り返さない)の原則(Googleだけ)を読むべきです。 –

関連する問題