简体   繁体   English

使用CI发送邮件时出现不必要的附件?

[英]Unnecessary attachment is coming while sending a mail in CI?

Hi all i am sending two mail types for my application need. 大家好,我发送两种邮件类型以满足我的应用程序需求。 The scene is like this when user approve something i will send a mail to them stating that you approved and in other case after approving one invoice will be generated and it will send as an attachment to the mail. 当用户批准某事时,场景将是这样的,我将向他们发送一封邮件,说明您已批准,在其他情况下,批准后将生成一张发票,并将其作为邮件附件发送。

Now what is happening is these two mail send to the user in a minute a gap or less so for the first mail the attachment is coming unnecessary. 现在发生的事情是这两封邮件在一分钟或更短的时间内发送给用户,因此对于第一封邮件来说,不需要附件。 And one more thing for suppose another user also approved. 假设另一个用户也批准了另一件事。 By the way invoices are different for different users. 顺便说一下,不同用户的发票是不同的。 But for the second user first user and second user invoices both files are getting attached. 但是,对于第二个用户,第一个用户和第二个用户发票,两个文件都已附加。

I know these kind of things will happen that's why i am saving the whole mail copy every time which i am sending to any user in database. 我知道这类事情将会发生,这就是为什么我每次发送给数据库中的任何用户时都保存整个邮件副本的原因。 In database for the first case where i am seeing unnecessary attachment there is no attachment and in the second case where i am seeing two attachments only attachment is there that belongs to user 2 only in the database. 在数据库中,对于第一种情况,我看到不必要的附件,没有附件,而在第二种情况中,我看到两个附件,只有附件存在于数据库中,仅属于用户2。

Note: This whole mails are sending using CI email class in LAMP environment. 注意:整个邮件在LAMP环境中使用CI电子邮件类发送。

This below function i am using to send mail. 我正在使用以下功能发送邮件。 This is a helper function i will call in the whole application where ever i want. 这是一个帮助函数,我将在整个应用程序中任意调用它。

function mail_send($to, $email_text_key, $subject_key_values, $message_key_values,$file)
{
    $CI = & get_instance();
    $CI->config->load('email_text');
    //      echo $to;echo $email_text_key;print_r($subject_key_values);print_r($message_key_values);
    $subject = $CI->config->item($email_text_key."_email_subject");
    $message = $CI->config->item($email_text_key."_email_body");// print_r($subject);print_r($message);
    send_mail($to, $subject, $message, $subject_key_values, $message_key_values,$file);
}


function send_mail($to, $subject, $message, $subject_key_values, $message_key_values,$file)
{
    $CI = & get_instance();
    $CI->config->load('email_text');
//  print_r($message_key_values);   
    foreach ( $subject_key_values as $key => $val ) {
        $subject = str_replace ("%$key%", $val, $subject);
    }

    foreach ( $message_key_values as $key => $val ) {
        $message = str_replace ("%$key%", $val, $message);
    }
    $message = str_replace ("%FAQ%", base_url()."index.php/welcome/faq", $message);
    $message = str_replace ("%LOGO%", base_url()."images/logo.png", $message);
    $message = str_replace ("%LOGO%", base_url()."images/Icon.ico", $message);
    $CI->load->library('email');

    $CI->email->from($CI->config->item('from_email'), $CI->config->item('from_email_name'));
    $CI->email->to($to);

    $CI->email->subject($subject);
    $CI->email->message($message);
    if($file !='') {
    $CI->email->attach($file);
    $file_type='pdf';
    $file_size = filesize($file);
    $fp = fopen($file,'r');
    $content = fread($fp,$file_size);
    $content = addslashes($content);
    fclose($fp);
    $CI->email->send();
    } else {
     $CI->email->send();
    }
    //make mail copied
    $CI->load->model('email_model');
    $user_data = array(
                        'to_email' =>$to,
                        'subject' =>$subject,
                        'message' =>$message,
                        'date_time' => date("Y-m-d H:i:s"),
                        'file_name' =>$file,
                        'file_type' =>$file_type,
                        'file_size' =>$file_size,
                        'content' =>$content,
                    );
    $CI->email_model->make_email_copy($user_data);              
}

Note: Please check my code where i am attaching file to mail in if and else condition. 注意:请检查我是否在附加条件下将文件附加到邮件中的代码。

$this->email->clear() $ this->电子邮件-> clear()

Initializes all the email variables to an empty state. 将所有电子邮件变量初始化为空状态。 This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles. 如果您循环运行电子邮件发送功能,以允许在两个周期之间重置数据,则可以使用此功能。

foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('your@example.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}

If you set the parameter to TRUE any attachments will be cleared as well: 如果将参数设置为TRUE,则所有附件也会被清除:

$this->email->clear(TRUE);

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

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