关于如何使用 codeigniter 在电子邮件中附加文件的问题
Having trouble about how to attach file in email using codeigniter
这是我的视图表单。这里是用户将发送附件 file/s 和电子邮件的地方。(这是我的视图表单的快捷方式。问题是附件。)
<form action="<?php echo base_url(); ?>hr_mailer/create" method="POST">
<table class="table" style="margin-left: 15px; text-align: left; width: 870px;">
<tr>
<td style="font-weight: bold;">Attachment/s: </td>
<td colspan="4"><input type="file" name="upload" id="file" /><label for="file">Choose a file</label>
</tr>
<tr>
<td colspan="4"><input type="submit" name="submit" value="Send Email" class="button1" style="float: right;" onclick="return confirm('You are about send the email, are you sure?')">
<button type="reset" value="Reset" class="button1" style="float: right; margin-right: 5px;">Clear</button></td>
</tr>
</table>
</form>
这是捕捉表单动作的函数
public function create() {
if ($this->input->post('submit')) {
$this->load->library('email');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload('upload');
$upload_data = $this->upload->data();
$form = array(
'sender' => $this->input->post('hr'),
'date' => $this->input->post('email_date')
);
$to = $this->input->post('to');
$cc = $this->input->post('cc');
$bcc = $this->input->post('bcc');
$message = $this->input->post('message');
$subj = $this->input->post('subject');
$this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
$this->mailer->insert($form);
$this->session->set_flashdata('msg', 'Email Sent!');
redirect(base_url().'hr_mailer/');
} else {
$data['uid'] = $this->user->get_uid();
$content = $this->load->view("hr_mailer/create", $data, true);
$this->render("", "", "", $content);
}
}
这是我的控制器中的另一个功能。
private function notify($to, $cc, $bcc, $message, $subj,$upload_data) {
$this->user = new Employee($this->session->userdata('username'));
$var = "cebuhr@1and1.com";
$name = "HR Announcement";
// $config['charset'] = 'iso-8859-1';
// $config['wordwrap'] = FALSE;
// $config['mailtype'] = 'html';
$this->load->library('email');
// $this->email->initialize($config);
//for testing purposes only. Must not be pushed to production
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($var, $name);
$this->email->to($to);
$this->email->cc($cc);
$this->email->bcc($bcc);
$this->email->subject($subj);
$this->email->message($message);
$this->email->attach($upload_data['full_path']);
if($this->email->send()){
echo 'SUCCESS';
} else {
show_error($this->email->print_debugger());
}
return true;
}
我是 CI 的新手,有些问题。我来自其他框架。在我的研究中,要放在 attach() 部分的变量是上传文件的完整路径名,这是我觉得很难的地方。
在你的函数中创建...
public function create() {
if ($this->input->post('submit')) {
$this->load->library('email');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload('upload');
$upload_data = $this->upload->data();
$file_path = $config['upload_path'].$upload_data[file_name]; // get path
$form = array(
'sender' => $this->input->post('hr'),
'date' => $this->input->post('email_date')
);
$to = $this->input->post('to');
$cc = $this->input->post('cc');
$bcc = $this->input->post('bcc');
$message = $this->input->post('message');
$subj = $this->input->post('subject');
// $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
$this->notify($to, $cc, $bcc, $message, $subj,$file_path );
// pass the generated path..
$this->mailer->insert($form);
$this->session->set_flashdata('msg', 'Email Sent!');
redirect(base_url().'hr_mailer/');
} else {
$data['uid'] = $this->user->get_uid();
$content = $this->load->view("hr_mailer/create", $data, true);
$this->render("", "", "", $content);
}
}
然后更改最后一个参数并在通知函数中附加var
private function notify($to, $cc, $bcc, $message, $subj,$file_path) {
$this->user = new Employee($this->session->userdata('username'));
$var = "cebuhr@1and1.com";
$name = "HR Announcement";
// $config['charset'] = 'iso-8859-1';
// $config['wordwrap'] = FALSE;
// $config['mailtype'] = 'html';
$this->load->library('email');
// $this->email->initialize($config);
//for testing purposes only. Must not be pushed to production
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($var, $name);
$this->email->to($to);
$this->email->cc($cc);
$this->email->bcc($bcc);
$this->email->subject($subj);
$this->email->message($message);
$this->email->attach($file_path);
if($this->email->send()){
echo 'SUCCESS';
} else {
echo "<pre>";
print_r($this->email->print_debugger());
EXIT;
}
return true;
}
用这个替换你的代码...
你没有正确捕获路径并且你没有在任何变量中捕获调试器..它在打印之前就消失了..在创建函数......你的位置得到重定向..你没有通知函数的任何条件..
这是我的视图表单。这里是用户将发送附件 file/s 和电子邮件的地方。(这是我的视图表单的快捷方式。问题是附件。)
<form action="<?php echo base_url(); ?>hr_mailer/create" method="POST">
<table class="table" style="margin-left: 15px; text-align: left; width: 870px;">
<tr>
<td style="font-weight: bold;">Attachment/s: </td>
<td colspan="4"><input type="file" name="upload" id="file" /><label for="file">Choose a file</label>
</tr>
<tr>
<td colspan="4"><input type="submit" name="submit" value="Send Email" class="button1" style="float: right;" onclick="return confirm('You are about send the email, are you sure?')">
<button type="reset" value="Reset" class="button1" style="float: right; margin-right: 5px;">Clear</button></td>
</tr>
</table>
</form>
这是捕捉表单动作的函数
public function create() {
if ($this->input->post('submit')) {
$this->load->library('email');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload('upload');
$upload_data = $this->upload->data();
$form = array(
'sender' => $this->input->post('hr'),
'date' => $this->input->post('email_date')
);
$to = $this->input->post('to');
$cc = $this->input->post('cc');
$bcc = $this->input->post('bcc');
$message = $this->input->post('message');
$subj = $this->input->post('subject');
$this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
$this->mailer->insert($form);
$this->session->set_flashdata('msg', 'Email Sent!');
redirect(base_url().'hr_mailer/');
} else {
$data['uid'] = $this->user->get_uid();
$content = $this->load->view("hr_mailer/create", $data, true);
$this->render("", "", "", $content);
}
}
这是我的控制器中的另一个功能。
private function notify($to, $cc, $bcc, $message, $subj,$upload_data) {
$this->user = new Employee($this->session->userdata('username'));
$var = "cebuhr@1and1.com";
$name = "HR Announcement";
// $config['charset'] = 'iso-8859-1';
// $config['wordwrap'] = FALSE;
// $config['mailtype'] = 'html';
$this->load->library('email');
// $this->email->initialize($config);
//for testing purposes only. Must not be pushed to production
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($var, $name);
$this->email->to($to);
$this->email->cc($cc);
$this->email->bcc($bcc);
$this->email->subject($subj);
$this->email->message($message);
$this->email->attach($upload_data['full_path']);
if($this->email->send()){
echo 'SUCCESS';
} else {
show_error($this->email->print_debugger());
}
return true;
}
我是 CI 的新手,有些问题。我来自其他框架。在我的研究中,要放在 attach() 部分的变量是上传文件的完整路径名,这是我觉得很难的地方。
在你的函数中创建...
public function create() {
if ($this->input->post('submit')) {
$this->load->library('email');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload('upload');
$upload_data = $this->upload->data();
$file_path = $config['upload_path'].$upload_data[file_name]; // get path
$form = array(
'sender' => $this->input->post('hr'),
'date' => $this->input->post('email_date')
);
$to = $this->input->post('to');
$cc = $this->input->post('cc');
$bcc = $this->input->post('bcc');
$message = $this->input->post('message');
$subj = $this->input->post('subject');
// $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
$this->notify($to, $cc, $bcc, $message, $subj,$file_path );
// pass the generated path..
$this->mailer->insert($form);
$this->session->set_flashdata('msg', 'Email Sent!');
redirect(base_url().'hr_mailer/');
} else {
$data['uid'] = $this->user->get_uid();
$content = $this->load->view("hr_mailer/create", $data, true);
$this->render("", "", "", $content);
}
}
然后更改最后一个参数并在通知函数中附加var
private function notify($to, $cc, $bcc, $message, $subj,$file_path) {
$this->user = new Employee($this->session->userdata('username'));
$var = "cebuhr@1and1.com";
$name = "HR Announcement";
// $config['charset'] = 'iso-8859-1';
// $config['wordwrap'] = FALSE;
// $config['mailtype'] = 'html';
$this->load->library('email');
// $this->email->initialize($config);
//for testing purposes only. Must not be pushed to production
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($var, $name);
$this->email->to($to);
$this->email->cc($cc);
$this->email->bcc($bcc);
$this->email->subject($subj);
$this->email->message($message);
$this->email->attach($file_path);
if($this->email->send()){
echo 'SUCCESS';
} else {
echo "<pre>";
print_r($this->email->print_debugger());
EXIT;
}
return true;
}
用这个替换你的代码... 你没有正确捕获路径并且你没有在任何变量中捕获调试器..它在打印之前就消失了..在创建函数......你的位置得到重定向..你没有通知函数的任何条件..