我最近提出用他的欠发达网站帮助一位朋友,但我被困在联系表格中。虽然表单通过验证(理论上),但表单不会发送出去。 就像现在一样,我把它缩小到下面的函数,特别是第二个foreach循环......看起来字段值变空了...... 这是功能,完整......:
function send()
{
$new_post = array();
foreach ($_POST as $key => $post)
{
$new_post[str_replace('form_','',$key)] = $post;
}
$mymail = empty($this->form_parameters['myemail']) ? $new_post['myemail'] : $this->form_parameters['myemail'];
$myblogname = empty($this->form_parameters['myblogname']) ? $new_post['myblogname'] : $this->form_parameters['myblogname'];
$subject = empty($new_post['subject']) ? "New Message" : $new_post['subject'];
$default_from = parse_url(home_url());
//set the email adress
$from = "no-reply@wp-message.com";
$usermail = false;
//if(!empty($default_from['host'])) $from = "no-reply@".$default_from['host'];
if(!empty($this->autoresponder[0]))
{
$from = $_POST[$this->autoresponder[0]];
$usermail = true;
}
else
{
$email_variations = array( 'e-mail', 'email', 'mail' );
foreach($email_variations as $key)
{
foreach ($new_post as $current_key => $current_post)
{
if( strpos($current_key, $key) !== false)
{
$from = $new_post[$current_key];
$usermail = true;
break;
}
}
if($usermail == true) break;
}
}
$to = urldecode( $mymail );
$from = urldecode( $from );
$subject = urldecode( $subject );
$message = "";
foreach($this->form_elements as $key => $element)
{
if(!empty($new_post[str_replace(" ", "_", preg_replace("[^A-Za-z]", "" ,$key))]))
{
if($element['type'] == 'textarea') $message .= "<br/>";
$message .= $element['label'].": ".nl2br(urldecode($new_post[str_replace(" ", "_", preg_replace("[^A-Za-z]", "" ,$key))]))."<br/>";
if($element['type'] == 'textarea') $message .= "<br/>";
}
else { $message .= "error..."; }
}
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'. $from . " \r\n";
//wp_mail('gnikolopoulos@gmail.com', $subject, $message, $header);
if(!wp_mail($to, $subject, $message, $header)){
$this->errors = true;
return false;
}
if($usermail && !empty($this->form_parameters['autoresponder']))
{
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'. urldecode( $this->form_parameters['autoresponder_email']) . " \r\n";
$message = nl2br($this->form_parameters['autoresponder'])."<br/><br/><br/><strong>Your Message:</strong><br/><br/>".$message;
if(!wp_mail($from, $this->form_parameters['autoresponder_subject'], $message, $header)){
$this->errors = true;
return false;
}
}
return true;
}
任何帮助将不胜感激.... 仅供参考,我认为他从另一个主题得到的形式不是他正在使用的那个......
答案 0 :(得分:0)
看起来像
$new_post[str_replace('form_','',$key)] = $post;
线引起了这个问题!代码没有为表单字段的名称添加前缀,因此str_replace函数失败了....一旦我修改代码实际包含所需的前缀,一切工作都很好......不能相信我没看到那个......!