Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有比我更简洁的方法,用PHPMailer发送邮件?_Php_Email_Phpmailer - Fatal编程技术网

有没有比我更简洁的方法,用PHPMailer发送邮件?

有没有比我更简洁的方法,用PHPMailer发送邮件?,php,email,phpmailer,Php,Email,Phpmailer,我正在使用PHPMailer,但我认为它占用了太多的行,我觉得我可以使它更简洁。下面的一些参数是多余的,因为所有内容都将从相同的电子邮件地址发送 实际上,我是在问我是否可以将始终相同的参数放在其他地方(From、FromName、Username、Password、Host、Post、SMTPSecure、SMTPAuth) 我怎么做?提前谢谢。问候 我的代码是: include("classes/phpmailer/class.phpmailer.php"); include("classes

我正在使用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
}

为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!');
    }
}

为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!');
    }
}
现在您可以重用该函数



现在您可以重用该函数。

您是在询问如何将代码分解为可重用的部分,还是询问是否可以减少行数?您好。我实际上是在问,我是否可以将始终相同的参数放在其他地方(From、FromName、Username、Password、Host、Post、SMTPSecure、SMTPAuth)。如果您想要更少的选项,请使用mail()@Dagon mail()充满了标头注入安全问题。不过,swiftmailer()非常棒,而且非常简洁。您是在问如何将代码分解为可重用的部分,还是询问是否可以减少行数?您好。我实际上是在问,我是否可以将始终相同的参数放在其他地方(From、FromName、Username、Password、Host、Post、SMTPSecure、SMTPAuth)。如果您想要更少的选项,请使用mail()@Dagon mail()充满了标题注入安全问题。不过,swiftmailer()很棒,而且非常简洁。您好,谢谢您的回复。我想我必须安装Kohanna框架?我编辑了我的OP,说:
我实际上是在问我是否可以输入始终相同的参数(From,FromName,Username,Password,Host,Post,SMTPSecure,SMTPAuth)
。这能帮我做那件事吗?谢谢,洛蒂编辑了这段代码。不,您不需要特定的框架。您可以创建一个类似于我的类并使用它。你应该读一下DRY(不要重复你自己)原理(只需谷歌一下)。哦,好吧,所以我想我需要这样写:
$conf=array(From=>)myemail@gmail.com,FromName=>Admin')
等。。如果我错了,请告诉我。感谢一个lotNo,只需像他那样创建一个类,放置所有未更改的变量(name、pword等),然后通过从主页面调用方法返回该实例。我想我必须安装Kohanna框架?我编辑了我的OP,说:
我实际上是在问我是否可以输入始终相同的参数(From,FromName,Username,Password,Host,Post,SMTPSecure,SMTPAuth)
。这能帮我做那件事吗?谢谢,洛蒂编辑了这段代码。不,您不需要特定的框架。您可以创建一个类似于我的类并使用它。你应该读一下DRY(不要重复你自己)原理(只需谷歌一下)。哦,好吧,所以我想我需要这样写:
$conf=array(From=>)myemail@gmail.com,FromName=>Admin')
等。。如果我错了,请告诉我。感谢一个lotNo,只需像他那样创建一个类,放置所有未更改的变量(name、pword等),然后通过从主页面调用方法返回该实例。这似乎就是我要找的。谢谢你。Regards我应该把它放在phpMail类文件中吗?谢谢你。。。不要修改原始库。您可以将
SMTPSecure
Host
Port
组合到
Host
属性中,如下所示:
$mail->Host=tls://smtp.gmail.com:587';
。另外,设置
发件人
发件人姓名
发件人
可以压缩为:
$mail->setFrom('myemailaddress@gmail.com","行政",SMTPSecure
Host
Port
组合到
Host
属性中,如下所示:
$mail->Host=tls://smtp.gmail.com:587';
。另外,设置
发件人
发件人姓名
发件人
可以压缩为:
$mail->setFrom('myemailaddress@gmail.com","行政",
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
}