使用 IMAP/SMTP 在 Gmail 中创建消息(即草稿)?

2022-01-17 00:00:00 imap smtp gmail php

我已经通过 PHP 中的 IMAP 函数对 Gmail 进行了相当多的收件箱操作,但我还没有找到一种创建邮件的方法.我不确定是否需要 IMAP 或 SMTP,但我想使用 PHP 创建一个新消息(特别是草稿),该消息存储在我的收件箱中,所有内容都可以在以后发送.我该怎么办?

I've done quite a bit of inbox manipulation with Gmail via IMAP functions in PHP, but one thing I haven't found is a way to create messages. I'm not sure if IMAP or SMTP is required, but I would like to use PHP to create a new message (specifically a draft) that is stored in my inbox with everything ready to hit send at a later date. How do I go about this?

推荐答案

你可能想看看 imap_mail_compose()

You might want to look at imap_mail_compose()

编辑这不会在服务器上创建消息.您还需要使用 imap_append().

Edit This doesn't create the message on the server. You need to use imap_append() also.

进一步编辑这似乎工作正常:

<?php 
$rootMailBox = "{imap.gmail.com:993/imap/ssl}";
$draftsMailBox = $rootMailBox . '[Google Mail]/Drafts';

$conn = imap_open ($rootMailBox, "sdfsfd@gmail.com", "password") or die("can't connect: " . imap_last_error());

$envelope["to"]  = "test@test.com";
$envelope["subject"]  = "Test Draft";

$part["type"] = TYPETEXT;
$part["subtype"] = "plain";
$part["description"] = "part description";
$part["contents.data"] = "Testing Content";

$body[1] = $part;

$msg = imap_mail_compose($envelope, $body);

if (imap_append($conn, $draftsMailBox, $msg) === false) {
        die( "could not append message: " . imap_last_error() )  ;
}

相关文章