芭蕾舞女演员:如何通过电子邮件发送附件

时间:2018-10-08 09:14:33

标签: http gmail ballerina

我正在使用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 :(得分:1)

是的。正如@pasan提到的那样,AttachmentPath是一条记录。如果有人要参考,下面是更新的代码。

header("Content-Type: image/png");

require "vendor/autoload.php";

use Endroid\QrCode\QrCode;
$user = 'https://example.org/mitglieder/' . $_GET['user']. '';
$username = $_GET['user']; 


$qrcode = new Endroid\QrCode\QrCode;
$qrcode->setText($user);
$qrcode->setSize(400);
$qrcode->setForegroundColor(['r' => 33, 'g' => 117, 'b' => 194, 'a' => 0]);
$qrcode->setBackgroundColor(['r' => 254, 'g' => 203, 'b' => 96, 'a' => 0]);
$qrcode->setLabel($username, 10);

$qrcode->writeFile($username.'.png');
echo $qrcode->writeString(); 

答案 1 :(得分:0)

AttachmentPath[wso2/gmail][1]中定义为一条记录。 attachmentPaths字段需要这样的AttachmentPath对象的数组。因此,以下方法应该有效。

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

messageRequest.attachmentPaths = [attachmentPaths];