【发布时间】:2022-01-06 13:05:08
【问题描述】:
我找到了这个 PHP 代码来提取电子邮件。现在,我想将此电子邮件移至 WordPress 中的自定义帖子类型。我已将自定义帖子类型名称创建为电子邮件收件箱。 下面是我如何提取电子邮件的代码:
class Email_reader {
// imap server connection
public $conn;
// inbox storage and inbox message count
private $inbox;
private $msg_cnt;
// email login credentials
private $server = 'myserver.com';
private $user = 'myserver@mail.com';
private $pass = 'PASSWORD';
private $port = 993; // adjust according to server settings
// connect to the server and get the inbox emails
function __construct() {
$this->connect();
$this->inbox();
}
// close the server connection
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
imap_close($this->conn);
}
// open the server connection
// the imap_open function parameters will need to be changed for the particular server
// these are laid out to connect to a Dreamhost IMAP server
function connect() {
$this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
// move the message to a new folder
function move($msg_index, $folder='INBOX.Processed') {
// move on server
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
// re-read the inbox
$this->inbox();
}
// get a specific message (1 = first email, 2 = second email, etc.)
function get($msg_index=NULL) {
if (count($this->inbox) <= 0) {
return array();
}
elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
return $this->inbox[$msg_index];
}
return $this->inbox[0];
}
// read the inbox
function inbox() {
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
for($i = 1; $i <= $this->msg_cnt; $i++) {
$in[] = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'body' => imap_body($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
}
$this->inbox = $in;
}
}
$emails = new Email_reader;
echo "<pre>";
var_dump($emails);
现在我想将此电子邮件添加到我的 WordPress 自定义帖子类型。 提前致谢。
【问题讨论】:
-
如果您有 CPT,您可以使用
wp_insert_post插入该 CPT 的实例。如果您有每个实例的自定义元数据,您可以使用update_post_meta。你问的是这个吗? -
我会说您正在寻找电子邮件服务前端或 Web-UI。有些存在,但那些是应用程序,没有什么微不足道的。我看不出您如何将电子邮件客户端类用作 CMS 系统中的“页面”或“帖子”。两者之间需要一些 UI 逻辑。
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: php wordpress email plugins custom-post-type