gpt4 book ai didi

php - Codeigniter -> 附上电子邮件

转载 作者:可可西里 更新时间:2023-11-01 13:05:07 26 4
gpt4 key购买 nike

如何使用邮件->附件功能?

我不知道发生了什么,因为当我输入电子邮件代码时 -> 附加消息是空白的(邮件正文)并且没有附件。

如果我删除该代码行,一切都会恢复正常..

谢谢

我的 Controller (sendmail.php)

<?php

class Sendmail extends Controller {

function __construct() {
parent::Controller();
$this->load->library('email');
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('validation');
}

function index() {

$info = array (
'nome' => $this->input->post('nome'),
'mail' => $this->input->post('email'),
'motivo' => $this->input->post('motivo'),
'mensagem' => $this->input->post('mensagem'),
'anexo' => $this->input->post('upload'),
);

$this->load->library('email');
$this->email->set_newline('\r\n');

$this->email->clear();
$this->email->from($info['mail'], $info['nome']);
$this->email->to('example@mai.com');
/* $this->email->cc(''); # não é preciso */
$this->email->subject($info['motivo']);
$this->email->message($info['mensagem']);
$this->email->attach($info['anexo']);

if ($this->email->send() ) {
echo 'sent';
}

else {
$this->load->view('formulario');
# show_error( $this->email->print_debugger() );
}

}

}
?>

我的观点(formulario.php)

    <?php
echo form_open_multipart('davidslv/index.php/sendmail');
?>
<label for="nome">nome</label>
<input type="text" name="nome" id="nome" required />

<label for="email">email</label>
<input type="text" name="email" id="email" required />

<label for="assunto">assunto</label>
<select name="motivo">
<option value="motivo1">motivo1</option>
<option value="motivo2">motivo2</option>
<option value="motivo3">motivo3</option>
</select>

<p> <label for="mensagem">mensagem</label>
<textarea name="mensagem" id="mensagem" rows="8" cols="30" required></textarea>
</p>

<label for="upload">documento</label>
<input type="file" id="upload" name="upload" size="18"/>
<input type="submit" id="enviar" name="enviar" value="Enviar!" />

</form>

最佳答案

您不能直接将表单上传字段中的文件附加到电子邮件中。您只能从您的服务器将文件附加到您的电子邮件,因此您需要使用 CIs 上传库将文件从表单上传:$this->upload->do_upload() 到您的服务器的某个目录中。需要配置上传库,允许哪些文件类型等。如果上传成功,do_upload 函数将返回有关文件存储位置的大量数据。您可以使用数组中的“full_path”索引将此文件附加到电子邮件中。然后发送邮件。之后,您可以从您的服务器中删除该文件。以下是一些可能有帮助的代码片段。

$this->load->library('upload');

if($_FILES['upload']['size'] > 0) { // upload is the name of the file field in the form

$aConfig['upload_path'] = '/someUploadDir/';
$aConfig['allowed_types'] = 'doc|docx|pdf|jpg|png';
$aConfig['max_size'] = '3000';
$aConfig['max_width'] = '1280';
$aConfig['max_height'] = '1024';

$this->upload->initialize($aConfig);

if($this->upload->do_upload('upload'))
{
$ret = $this->upload->data();
} else {
...
}

$pathToUploadedFile = $ret['full_path'];
$this->email->attach($pathToUploadedFile);
...
$this->email->send();
...
}
...

希望这对您有所帮助...

关于php - Codeigniter -> 附上电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com