【发布时间】:2016-05-31 16:58:54
【问题描述】:
当我使用模板函数中的值时,我遇到了发件人姓名、主题和发件人电子邮件地址为空的问题,但如果我直接在邮件函数中写入电子邮件和主题,它们就会出现。
编辑
我要做什么
1。创建可根据所需类型进行切换的电子邮件模板。
$type = 'account_verify';
$email = new email($type);
$email->send('user@domain.com');
2。有一个基于 $type 切换这些模板的方法。 我希望在每个模板中指定所有参数,例如“subject”、“from_email”、“sender_name”...,这样我就只使用用户电子邮件调用 $email->send('user_email') 方法,这些参数的其余部分应在指定的模板上找到。
参见下面的模板方法。
好的,这是每个模板的外观。
<?php
$token = self::token();
$user = self::user();
$today = date("M j - Y, g:i a");
$verify_link = ""
$from_email = 'department@domain.com';
$sender_name = 'department Name';
$department_name = 'department';
$subject = 'Verify your account';
ob_start();
?>
<!DOCTYPE html>
<html>
..........
<?php
?>
其余如下
public $senderName;
public $from_email;
public $dpt;
public $type;
public $subject;
private function template(){
include_once 'templates/accounts/account_verify.php';
//// the following variable like, "$from_email", are set inside the
///// included template. Every template should have its own
/////// parameters set.
$this->from_email = $from_email;
$this->senderName = $sender_name;
$this->dpt = $department_name;
$this->subject = $subject;
return ob_get_clean();
}
以及发送邮件的功能
public function send($to_email ,$color_scheme = NULL){
var_dump('SUBJECT : '.$this->subject);
///// here if I dump the $this->subject i can see it,
//// but the subject is empty on the email.
$subject = $this->from_email;
$from = "".$subject."<user@domain.com>";
$headers = "From:" . $from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to_email,$this->subject,self::template(),$headers);
}
【问题讨论】:
-
类方法不会采用这样的值。将这些值定义为常量并在类方法中使用。
-
@EdwinAlex,我将无法在运行时从 template() 更改常量;
-
显然,您的班级缺少 class 关键字和名称...
-
您为什么决定不使用 PHP
templating classes之一?我使用一种使用PHP作为模板语言的语言。他们已经为您完成了所有艰苦的工作。例如,Plates is a native PHP template system that’s fast, easy to use and easy to extend.。还有Twig。 -
@RyanVincent 这是我第一次尝试这样做。您能否给出一个答案,建议使用该模板引擎将如何简化此操作?
标签: php class methods properties html-email