Балерина: Как отправить вложение по электронной почте - PullRequest
0 голосов
/ 08 октября 2018

Я использую пакет wso2 / gmail для отправки уведомления по электронной почте.Согласно документации (https://central.ballerina.io/wso2/gmail) мы можем отправить вложение вместе с письмом через пакет. Однако, когда я пытаюсь определить пути вложения в качестве параметра, я получаю сообщение об ошибке следующим образом.

incompatible types: expected 'wso2/gmail:0.9.7:AttachmentPath', found 'string'

Что такое тип AttachmentPath? Можем ли мы проанализировать строковый массив путей вложения к AttachmentPath? Вот моя функция для отправки почты.

import wso2/gmail;
import ballerina/io;
import ballerina/log;
import ballerina/config;
import ballerina/http;

function sendErrorLogMail(string senderEmail, string recipientEmail, string subject, string messageBody) returns boolean {
    endpoint gmail:Client gmailErrorClient {
        clientConfig:{
            auth:{
                accessToken:config:getAsString("gmailApiConfiguration.accessToken"),
                refreshToken:config:getAsString("gmailApiConfiguration.refreshToken"),
                clientId:config:getAsString("gmailApiConfiguration.clientId"),
                clientSecret:config:getAsString("gmailApiConfiguration.clientSecret")
            }
        }
    };

    gmail:MessageRequest messageRequest;
    messageRequest.recipient = recipientEmail;
    messageRequest.sender = senderEmail;
    messageRequest.subject = subject;
    messageRequest.messageBody = messageBody;
    messageRequest.contentType = gmail:TEXT_HTML;

    //What is the attachment path?
    AttachmentPath attachmentPath = "./org/wso2/logs/loginfo.txt";

    messageRequest.attachmentPaths = attachmentPath;

    var sendMessageResponse = gmailErrorClient->sendMessage(senderEmail, untaint messageRequest);
    string messageId;
    string threadId;
    match sendMessageResponse {
        (string, string) sendStatus => {
            (messageId, threadId) = sendStatus;
            log:printInfo("Sent email to " + recipientEmail + " with message Id: " + messageId + " and thread Id:"
                    + threadId);
            return true;
        }
        gmail:GmailError e => {
            log:printInfo(e.message);
            return false;
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 08 октября 2018

Да.Как упомянул @pasan, AttachmentPath является записью.Ниже приведен обновленный код, если кто-то хочет обратиться.

function sendErrorLogMail(string senderEmail, string recipientEmail, string subject, string messageBody) returns boolean {
    endpoint gmail:Client gmailErrorClient {
        clientConfig:{
            auth:{
                accessToken:config:getAsString("gmailApiConfiguration.accessToken"),
                refreshToken:config:getAsString("gmailApiConfiguration.refreshToken"),
                clientId:config:getAsString("gmailApiConfiguration.clientId"),
                clientSecret:config:getAsString("gmailApiConfiguration.clientSecret")
            }
        }
    };

    gmail:AttachmentPath attachmentPath= {
        attachmentPath:"./org/wso2/logs/loginfo.txt",
        mimeType:"Text/plain"
    };

    gmail:MessageRequest messageRequest;
    messageRequest.recipient = recipientEmail;
    messageRequest.sender = senderEmail;
    messageRequest.subject = subject;
    messageRequest.messageBody = messageBody;
    messageRequest.contentType = gmail:TEXT_HTML;

    messageRequest.attachmentPaths = [attachmentPath];

    var sendMessageResponse = gmailErrorClient->sendMessage(senderEmail, untaint messageRequest);
    string messageId;
    string threadId;
    match sendMessageResponse {
        (string, string) sendStatus => {
            (messageId, threadId) = sendStatus;
            log:printInfo("Sent email to " + recipientEmail + " with message Id: " + messageId + " and thread Id:"
                    + threadId);
            return true;
        }
        gmail:GmailError e => {
            log:printInfo(e.message);
            return false;
        }
    }
}
0 голосов
/ 08 октября 2018

AttachmentPath определяется в [wso2/gmail][1] как запись.Поле attachmentPaths нуждается в массиве таких AttachmentPath объектов.Следующее должно работать.

gmail:AttachmentPath attachmentPath= {
        attachmentPath:"./org/wso2/logs/loginfo.txt",
        mimeType:"text/plain"
};

messageRequest.attachmentPaths = [attachmentPaths];
...