メール送信
ステータスが公開になったときにメールを送信する。HTMLで記載された記事のタグと改行を削除。
改行が3個以上続くときは2個(最大1行の空白行)とする。
function.php
function sendmail_post_article($new_status, $old_status, $post) {
$active_posttype = 'news'; // 記事のカテゴリ
$mail_to = 'xxx@kowloonet.net';
$mail_from_name = "小黒のtechメモ(仮)";
$mail_from = 'yyy@kowloonet.net';
$from = mb_encode_mimeheader($mail_from_name)." <$mail_from>";
$mail_subject = '【NEWS】';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = "From: ".$from;
$headers[] = "Sender: ".$from;
$headers[] = "Reply-To: ".$mail_from;
$headers[] = "X-Sender: " . $mail_from;
$headers[] = "X-Priority: 3";
if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == $active_posttype) {
/** HTML to txt **/
$content = preg_replace('/
/i', "\n", $post->post_content);
$content = strip_tags($content);
$content = html_entity_decode($content);
$content = preg_replace("/\r\n|\r|\n/us", "\n", $content);
$content = preg_replace("/\n[^\S\n]+/us", "\n", $content);
$content = trim(preg_replace("/(\r\n){3,}|\r{3,}|\n{3,}/us", "\n\n", $content));
/**// HTML to txt **/
$message .= $content."\n\n詳しくはこちら↓\n";
$message .= get_permalink($post->ID)."\n\n\n";
$attachments = []; //添付ファイルがあれば
wp_mail($mail_to, $mail_subject, $message, $headers, $attachments);
}
}
add_action('transition_post_status','sendmail_post_article' , 10, 3)
エラーログを出力してデバック
error_log('$new_status: '.$new_status);
error_log('$post->post_type: '.$post->post_type);
error_log(print_r($post, true ));
wp-content/debug.logに出力される。
エラーログの出力設定は割愛。