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 类创建一个包装类,该类初始化所有默认值并仅设置更改的值。

我在我的 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 = "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 回答