我正在尝试在我的邮件表单的主题行中使用一些PHP代码。我使用的代码如下:
$email_subject .= $words[$lang]["CONTACT_EMIL_SUBJECT"];
表单工作正常,但我没有收到任何电子邮件。 如果我只输入这样的文本:$ email_subject =“来自我的邮件表单的消息”; 然后我收到了电子邮件。
任何人都可以告诉我,这不起作用吗?
此代码:$ words [$ lang] [“CONTACT_EMIL_SUBJECT”从包含在页面顶部的文件中获取值。我在我的页面上的其他地方使用相同类型的代码,这是正常的。我正在使用它来翻译我的网站,所以如果用户正在查看我的网站上有英文翻译,那么我收到的电子邮件中的表格值也将是英文的,如果用户使用瑞典语翻译则更为明智电子邮件中的值将以瑞典语显示。
谢谢。
答案 0 :(得分:0)
您可以通过检查是否可以找到该值来使用后备选项。如果没有,请显示替代方案。
<?php
$to = "hello@domain.com";
$subject = isset($words[$lang]['CONTACT_EMAIL_SUBJECT']) ? $words[$lang]['CONTACT_EMAIL_SUBJECT'] : "Fallback subject, couldn't find correct lang.";
$message = isset($_POST['message']) ? $_POST['message'] : "No message";
if ( mail($to, $subject, $message) ) {
echo "Thanks, your message was sent!";
} else {
echo "Oops, we hit an error.
}
?>
答案 1 :(得分:0)
你宣布了global $words;
吗?
检查本地与全局变量的想法 http://php.net/manual/en/language.variables.scope.php
例如:
include "/langs/".$lang."/words.php";
//this you can make a folder langs with multiple languages folder defined by $lang
//make a words.php in the $lang folder. Some Examples are /langs/en_us/words.php or /langs/en_gb/words.php for great britan and so on
//inside words.php you add
//$words[$lang]["CONTACT_EMIL_SUBJECT"] = "Email Subject"; for english
//$words[$lang]["CONTACT_EMIL_SUBJECT"] = "Kontakt E-Mail Betreff"; for german
//and so on.
//adding include at top of page is a global variable so when you add to your code
global $words; //this says to look for the variable when you added it to the file in include.
echo $words[$lang]["CONTACT_EMIL_SUBJECT"];
所以一个完整的例子:
<?PHP
include "/langs/".$lang."/words.php"; //include your language
function yourfunction(){
//some code....
global $words; //finds the $words global this can be used in anyplace that includes words.php
$email_subject .= $words[$lang]["CONTACT_EMIL_SUBJECT"];
echo $email_subject;
}