PHP - 在电子邮件中使用数组值
我正在使用脚本创建电子邮件,但由于某种原因,它无法读取变量...... 现在,这不是普通的 php 语法,因为在我得到的示例中,他们使用了这样的代码:
$messageproper =
"New message:\n" .
" \n" .
"Full name: $fullname\n" .
"Email: $email\n" ;
现在,我尝试了这个:
$messageproper =
"New placed order:\n".
"\n".
"Ticket type: $orderdata['tickettype']\n".
"From: $orderdata['from']\n".
"To: $orderdata['to']\n";
但由于某种原因,它在每一行中都给出了错误。 所以我尝试了这个:
$messageproper =
"New placed order:\n".
"\n".
"Ticket type: ".$orderdata['tickettype']."\n".
"From: ".$orderdata['from']."\n".
"To: ".$orderdata['to']."\n";
但由于某种原因,只是将每个变量留空。
因为我使用函数 processOrder() (在其中声明并设置所有变量)和 sendOrder() (在其中组装电子邮件),所以我已经尝试将 $orderdata 数组设置为全局,但这也不起作用。
这是发送电子邮件的脚本的重要部分:
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
// ---------------------------- preparing the headers ----------------------------
$headers =
"From: \"$fullname\" <$email>" . $headersep . "Reply-To: \"$fullname\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.15.0" .
$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
// ---------------------------- sending the email ----------------------------
if ($use_envsender) {
mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;
我在这里可能做错了什么? 另外,有人可以向我解释一下 $messageproper 的语法是什么吗? 我以前从未见过这样不合逻辑的语法(但是,现在我想到了,我确实这样做了:在 MySQL 查询中,你在请求中放置一个 php 变量。)
I'm using a script to create an email message, but for some reason, it can't read the variables...
Now, this isnt ordinary php syntax, because in the example I got it from, they used code like this:
$messageproper =
"New message:\n" .
" \n" .
"Full name: $fullname\n" .
"Email: $email\n" ;
Now, I tried this:
$messageproper =
"New placed order:\n".
"\n".
"Ticket type: $orderdata['tickettype']\n".
"From: $orderdata['from']\n".
"To: $orderdata['to']\n";
But for some reason it gives errors in every line.
So I tried this:
$messageproper =
"New placed order:\n".
"\n".
"Ticket type: ".$orderdata['tickettype']."\n".
"From: ".$orderdata['from']."\n".
"To: ".$orderdata['to']."\n";
But for some reason that just leaves every variable empty.
Because Im using a function processOrder() (where I declare and set all the variables) and sendOrder() (where I assemble the email), I already tried making the $orderdata array global, but that didn't work either.
This is the important part of the script that sends the email:
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
// ---------------------------- preparing the headers ----------------------------
$headers =
"From: \"$fullname\" <$email>" . $headersep . "Reply-To: \"$fullname\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.15.0" .
$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
// ---------------------------- sending the email ----------------------------
if ($use_envsender) {
mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;
What could I be doing wrong here?
Also, could someone explain to me what kind of syntax this is, for the $messageproper ? I've never seen such unlogic syntax before (however, now I think of it, I do: in MySQL queries, where you put a php variable in the request.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于在字符串中使用它,请使用
{$orderdata['tickettype']}
而不是$orderdata['tickettype']
。至于连接不起作用,请检查您是否在范围内定义了数据。 (将global $orderdata;
放入两个函数中)。As for using it inside string, use
{$orderdata['tickettype']}
instead of$orderdata['tickettype']
. As for concatenating not working, check if you have your data defined in scope. (putglobal $orderdata;
in both functions).