【问题标题】:How to send multiple attachments in single email using SendGrid Api?如何使用 SendGrid Api 在一封电子邮件中发送多个附件?
【发布时间】:2017-08-25 12:14:44
【问题描述】:

我可以发送带有单个附件的电子邮件,但我找不到任何可以在单个邮件中发送多个附件的东西

        try {
            SendGrid sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD);

            SendGrid.Email email = new SendGrid.Email();

            // Get values from edit text to compose email
            // TODO: Validate edit texts
            email.addTo(mTo);
            email.setFrom(mFrom);
            email.setSubject(mSubject);
            email.setText(mText);

            // Attach image
            if (mUri != null) {
                email.addAttachment(mAttachmentName, mAppContext.getContentResolver().openInputStream(mUri));
            }

            // Send email, execute http request
            SendGrid.Response response = sendgrid.send(email);
            mMsgResponse = response.getMessage();

            Log.d("SendAppExample", mMsgResponse);

        } catch (SendGridException | IOException e) {
            Log.e("SendAppExample", e.toString());
        }

        return null;
    }

【问题讨论】:

  • 您可以简单地调用 addAttachment() 以添加单个文件的相同方式。我通过一个基于文件路径数组的 foreach 来完成此操作。

标签: android sendgrid-api-v3


【解决方案1】:
$files = ['/home/pawr/testAttach1.txt','/home/pawr/testAttach2.txt'];
foreach($files as $f) {
    // create attachment and add to email
    $fn = $this->createSendGridAttachment($f);
    $email->addAttachment($fn);
}
// code in custom function createSendGridAttachment($file_path)
{
    // Note, you may need to use different encoding methods for setContent() and
    // MIME types with setType() depending on your use case.
    $content = file_get_contents($file_path);
    $content = base64_encode($content);
    $attachment = new \SendGrid\Mail\Attachment();
    $attachment->setContent($content);
    $attachment->setFilename(basename($file_path));

    return $attachment;
}

【讨论】:

  • 实际上,它是一个 android 标记的问题。无论如何谢谢@Pawr
  • @RahulChandrabhan,我的错!
猜你喜欢
  • 2012-09-07
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2016-10-23
相关资源
最近更新 更多