我正在创建一个插件,将电子邮件显示为帖子。我使用以下PHP访问这些电子邮件:
<?php
class Email_reader {
// imap server connection
public $conn;
// inbox storage and inbox message count
private $inbox;
private $msg_cnt;
// email login credentials
private $server = \'website.com\';
private $user = \'[email protected]\';
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);
现在,我创建了一个自定义帖子类型,我想将这些电子邮件分配到自定义帖子类型,我如何才能实现它。我使用的代码是
//// Register Post Type.
if(!function_exists(\'my_email_custom_post_type\')){
function my_email_custom_post_type() {
$labels = array(
\'name\' => __( \'E-mail Inbox\',\'domain\'),
\'singular_name\' => __( \' E-mail\',\'domain\'),
\'menu_name\' => __( \'E-mail Inbox\',\'domain\'),
\'parent_item_colon\' => __( \'Parent tickets\',\'domain\'),
\'all_items\' => __( \'All E-mail tickets\',\'domain\'),
\'view_item\' => __( \'View E-mail tickets\',\'domain\'),
\'search_items\' => __( \'Search E-mail tickets\',\'domain\'),
\'not_found\' => __( \'Not Found\',\'domain\'),
\'supports\' =>__( \'title\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'revisions\', \'page-attributes\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\',\'domain\')
);
$args = array(
\'label\' => __( \'E-mail Inbox\',\'domain\'),
\'description\' => __( \'E-mail\',\'domain\'),
\'labels\' => $labels,
\'supports\' => array( \'title\'),
\'capabilities\' => array(
\'create_posts\' => false, // Removes support for the "Add New" function ( use \'do_not_allow\' instead of false for multisite set ups )
),
\'public\' => true,
\'hierarchical\' => false,
\'show_ui\' => true,
\'menu_icon\' => \'dashicons-feedback\',
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'has_archive\' => true,
\'can_export\' => true,
\'exclude_from_search\' => false,
\'yarpp_support\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\'
);
register_post_type( \'post-email\', $args );
}
add_action( \'init\', \'my_email_custom_post_type\', 0 );
}
require_once plugin_dir_path( __FILE__ ) . \'emailpipe.php\';
$args = array(
\'post_type\' => \'my-email\',
);
$emails = new WP_query($args);
while($emails->have_posts()) {
$emails->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php
}
这肯定不行,你能告诉我吗。
最合适的回答,由SO网友:Ravi 整理而成
使用非常简单,可以使用wordpress默认功能wp_insert_post()
以编程方式创建post。看见this 了解更多详细信息。
下面是我修改的代码:
<?php
class Email_reader {
// imap server connection
public $conn;
// inbox storage and inbox message count
private $inbox;
private $msg_cnt;
// email login credentials
private $server = \'website.com\';
private $user = \'[email protected]\';
private $pass = \'PSSWORD\';
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;
}
function total_msg(){
return imap_num_msg($this->conn);;
}
}
$emails = new Email_reader;
$total = $emails->total_msg();
for ($j=1; $j <= $total; $j++) {
$mail = $emails->get($j);
$post_array = array(
\'post_content\' => $mail[\'body\'],
\'post_title\' => $mail[\'header\']->subject,
\'post_type\' => \'my-email\',
\'post_status\' => \'publish\',
\'meta_input\' => array(
\'to\' => $mail[\'header\']->toaddress,
\'email_date\' => $mail[\'header\']->Date, // add post meta as many as you want
),
);
wp_insert_post($post_array);
}
我更改了你的类并增加了一个函数来获取电子邮件总数。之后,我一个接一个地收到电子邮件,并使用
wp_insert_post()
现在,您可以使用代码读取它:
$args = array(
\'post_type\' => \'my-email\',
);
$emails = new WP_query($args);
while($emails->have_posts()) {
$emails->the_post();
?>
<h3><?php the_title(); ?></h3>
我没有添加摘录,但您可以根据需要修改我的代码。非常感谢。