我正在使用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
}
答案 0 :(得分: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();
答案 1 :(得分: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
}
现在您重复使用该功能。