0

PHPMailerを使用していますが、行数が多すぎて、もっと簡潔にできると思います。以下のパラメータの一部は、すべてが同じ電子メールアドレスから送信されるため、冗長です。

私は実際に、常に同じパラメーターを別の場所に配置できるかどうかを尋ねています(From、FromName、Username、Password、Host、Post、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   = "myemailaddress@gmail.com";  // GMAIL username
$mail->Password   = "mypassword";            // GMAIL password

$mail->From       = "myemailaddress@gmail.com";
$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("replyto@yourdomain.com","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
}
4

2 に答える 2

2

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

コハナが開発したサイトで次のクラスを使用しています。

<?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 = "smtp@gmail.com"; // 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();
于 2012-04-19T01:36:53.067 に答える
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   = "myemailaddress@gmail.com";  // GMAIL username
    $mail->Password   = "mypassword";            // GMAIL password

    $mail->From       = "myemailaddress@gmail.com";
    $mail->FromName   = "Admin";
    $mail->Subject    = $subject;
    $mail->AltBody    = $altBody; //Text Body
    $mail->WordWrap   = $wordwrap; // set word wrap

    $mail->MsgHTML($body);
    $mail->AddReplyTo("replyto@yourdomain.com","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
}

次に、その関数を再利用します。

于 2012-04-19T02:04:17.973 に答える