5

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

        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;
    }
4

1 回答 1

0
$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;
}
于 2019-10-03T16:19:25.243 回答