简体   繁体   English

我如何从PHP表单发送电子邮件

[英]How do i email from php form

I am trying to insert the name, email, company, phone and country from the array into the email message, but all I get is an empty email. 我正在尝试将数组中的名称,电子邮件,公司,电话和国家/地区插入电子邮件,但是我得到的只是一封空电子邮件。 The subject, to and from all works, but I am not getting the message in the email. 主题,所有作品,以及所有作品,但我没有收到电子邮件中的消息。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?

<? php
public function contact_mail() {

    $data = array(
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'company' => $this->input->post('company'),
        'phone' => $this->input->post('phone'),
        'country' => $this->input->post('country')
    );

    $success = $this->data->save_contact_info($data);
    if ($success) {
        $data = array('success_message' => 'Thank you. We\'ll be in touch.');
        $this->load->view('ajax/json', array('json' => $data));

        $this->load->library('email');
        $config['mailtype'] = 'html';
        $this->email->initialize($config);
        $this->email->from("noreply@example.com", "Example Notification Service");
        $this->email->to(array('mha@example.com'));
        $this->email->subject('Contact form');
        $this->email->message($data);
        $this->email->send();

    }
    else {
        $this->load->view('ajax/error', array('error_msg' => 'Please fill out all fields.'));
    }
}
?>

Change this line: 更改此行:

$this->load->view('ajax/json', array('json' => $data));

to this: 对此:

$view = $this->load->view('ajax/json', array('json' => $data));

Then instead of sending $data through which is just an array , you send through $view which is the HTML that includes the array. 然后,通过发送包含数组的HTML的$view而不是通过array发送$data

Also, you're using $data to get all of the POST variables, but then use data later to set a flash message. 另外,您正在使用$data获取所有POST变量,但随后使用data来设置即显消息。

I'm not sure whether you mean to overwrite $data or not, but it's always best to keep the variable names different for different things. 我不确定您是否要覆盖$data ,但始终最好使变量名在不同情况下保持不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM