【问题标题】:Attach file to email using sendgrid使用 sendgrid 将文件附加到电子邮件
【发布时间】:2017-08-21 01:40:54
【问题描述】:

我正在使用 sendgrid 邮件程序,但无法将 csv 文件附加到邮件功能。 下面是编写邮件的代码...

$resumeName = $_FILES['resume']['name'];

$resumePath = $_FILES['resume']['tmp_name'];

需要在发件人邮件中下载此文件...

  public static function sendMailwithAttachment($mail_type, $mail_variable = array(), $subject, $from, $to, $resumeName, $resumePath) {

    $CI = & get_instance();

    if ($mail_type !== NULL) { 
        $CI->db->select('tpl');
        $CI->db->from('tblMailTypes');
        $CI->db->where('id', $mail_type);
        $query = $CI->db->get();

        $mailIdres = $query->result_array();
        if (!empty($mailIdres)) {
            $message = $mailIdres[0]['tpl'];
            if (!empty($mail_variable)) {
                foreach ($mail_variable as $key => $val) {
                    $message = str_ireplace($key, $val, $message); // select message format from table
                }
            }
        }
    }

    $from = new SendGrid\Email(null, $from);

    $to = new SendGrid\Email(null, $to);

    $content = new SendGrid\Content("text/html", $message); 
    exit();



    $mail = new SendGrid\Mail($from, $subject, $to, $content);

    $apiKey = 'ABCD..................HHHHFFFRRDSE'; // Sendgrid API key
    $sg = new \SendGrid($apiKey);

   addAttachment($resumePath, $resumeName); 


   $response = $sg->client->mail()->send()->post($mail);
}

请大家帮忙

【问题讨论】:

    标签: php codeigniter email email-attachments sendgrid


    【解决方案1】:

    这段代码完美运行...

    public static function sendEMailwithAttachment($mail_type, $mail_variable = array(), $subject, $from, $mailto,$username, $fileName, $filePath){
    
           // If you are using Composer (recommended)
    require 'vendor/autoload.php';
    
     // If you are not using Composer
     // require("path/to/sendgrid-php/sendgrid-php.php");
    
        $CI = & get_instance();
    
        if ($mail_type !== NULL) { 
            $CI->db->select('tpl');
            $CI->db->from('tblMailTypes');
            $CI->db->where('id', $mail_type);
            $query = $CI->db->get();
    
            $mailIdres = $query->result_array();
            if (!empty($mailIdres)) {
                $message = $mailIdres[0]['tpl'];
                if (!empty($mail_variable)) {
                    foreach ($mail_variable as $key => $val) {
                        $message = str_ireplace($key, $val, $message);
                    }
                }
            }
        }
    
     $from = new SendGrid\Email("User", $from);
    
     $to = new SendGrid\Email($username,$mailto);
     $content = new SendGrid\Content("text/html", $message);
     $file = $filePath;
     $file_encoded = base64_encode(file_get_contents($file));
     $attachment = new SendGrid\Attachment();
     $attachment->setContent($file_encoded);
     $attachment->setType("application/text");
     $attachment->setDisposition("attachment");
     $attachment->setFilename($fileName);
    
     $mail = new SendGrid\Mail($from, $subject, $to, $content);
     $mail->addAttachment($attachment);
    
     $apiKey = 'ABCD..................HHHHFFFRRDSE'; // Sendgrid API key
    
     $sg = new \SendGrid($apiKey);
    
     $response = $sg->client->mail()->send()->post($mail);
    
    
    
    // If you want response
    
    //echo $response->statusCode();
    //echo $response->headers();
    //echo $response->body();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      相关资源
      最近更新 更多