使用Flutter_email_sender中的电子邮件中添加图像

发布于 2025-02-09 05:58:41 字数 1115 浏览 4 评论 0原文

我正在使用Flutter/Dart创建一个Android应用程序,我想在其中发送带有嵌入式图像的电子邮件。

因此,我安装了Flutter_email_sender并尝试使用它。它可以发送带有文本的电子邮件,但是当我尝试添加图像时。它没有出现在电子邮件应用程序中。

这是我的代码:


// DataUser.pathImage is the path of the image (/data/src/0/cache/hi.jpg)
// extension(DataUser.pathImage).substring(1) => "jpg"
// DataUser.emailText.split("\n").join("<br>") is the text of the user that will be send
//                ex: "Hi\nYes\nNo" => "Hi<br>Yes<br>No"
final bytes = File(DataUser.pathImage).readAsBytesSync();
String image64 = base64.encode(bytes);
String result = "<p>" + DataUser.emailText.split("\n").join("<br>") + "<br>";
result += "<img src=\"data:image/${extension(DataUser.pathImage).substring(1)};base64," + image64;
result += "\" alt=\"image\" />";
result += "</p>";

final Email email = Email(
    body: result,
    subject: "Pointage",
    recipients: DataUser.adresse,
    attachmentPaths: DataUser.filePath, 
    isHTML: true,
);

await FlutterEmailSender.send(email);

有没有办法发送包含此扩展名的映像的电子邮件?

I am creating an Android application with flutter/dart and I want to send an email with an embedded image inside.

So I installed flutter_email_sender and tried to use it. It works to send an email with text, but when I tried to add an image. It doesn't appear in the email application.

Here is my code:


// DataUser.pathImage is the path of the image (/data/src/0/cache/hi.jpg)
// extension(DataUser.pathImage).substring(1) => "jpg"
// DataUser.emailText.split("\n").join("<br>") is the text of the user that will be send
//                ex: "Hi\nYes\nNo" => "Hi<br>Yes<br>No"
final bytes = File(DataUser.pathImage).readAsBytesSync();
String image64 = base64.encode(bytes);
String result = "<p>" + DataUser.emailText.split("\n").join("<br>") + "<br>";
result += "<img src=\"data:image/${extension(DataUser.pathImage).substring(1)};base64," + image64;
result += "\" alt=\"image\" />";
result += "</p>";

final Email email = Email(
    body: result,
    subject: "Pointage",
    recipients: DataUser.adresse,
    attachmentPaths: DataUser.filePath, 
    isHTML: true,
);

await FlutterEmailSender.send(email);

Is there a way to send an email containing an image with this extension?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2025-02-16 05:58:41

在电子邮件对象中,附件接受文件路径列表,即列表。您可以检查完整的示例在这里

创建一个附件的字符串列表:

  List<String> attachments = [];

现在将您的映像路径(字符串)添加到此列表中。

(我认为您通过添加.readasbyTessync()犯了一个错误,我认为这不是必要的)。

假设用户从画廊中选择图像:

    PickedFile? pick = await picker.getImage(source: ImageSource.gallery);
    if (pick != null) {
      setState(() {
        attachments.add(pick.path); //this line adds file path to attachments
      });
    }

现在将此附件列表传递给电子邮件对象。

Email(
    body: result,
    subject: "Pointage",
    recipients: DataUser.adresse,
    attachmentPaths: attachments,
    isHTML: true,
);

In Email object, attachmentPaths accepts list of file paths i.e. List. You can check the complete example here.

Create a list of strings for attachments file paths:

  List<String> attachments = [];

Now add your image path (string) to this list.

(I think you are doing a mistake by adding .readAsBytesSync(), I don't think that's necessary).

Let's say user picks an image from gallery:

    PickedFile? pick = await picker.getImage(source: ImageSource.gallery);
    if (pick != null) {
      setState(() {
        attachments.add(pick.path); //this line adds file path to attachments
      });
    }

Now pass this attachments list to the Email object.

Email(
    body: result,
    subject: "Pointage",
    recipients: DataUser.adresse,
    attachmentPaths: attachments,
    isHTML: true,
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文