簡體   English   中英

WordPress-頁面加載后如何發送電子郵件通知?

[英]Wordpress - How to send an email notification when page is loaded?

我目前正在尋找一種通過WordPress網站發送電子郵件通知的方法。 因此,例如,當用戶訪問頁面A時,將在后台發送一封電子郵件通知。

我沒有在WordPress環境中進行Web開發的豐富經驗,所以有人可以在這里給我一個指導嗎? 我應該從哪里開始?

謝謝。

編輯:

我已經嘗試過mail()和wp_mail()函數,但它們似乎都不適合我。 當我訪問該頁面時,沒有發送電子郵件。 我還檢查了該頁面的模板,這只是默認模板。 也許我編輯的文件錯誤?

EDIT2:

我想托管提供商可能尚未啟用郵件功能。

這是發送HTML電子郵件的非常基本的php代碼。

<?php
if(is_page(123))
{

$fromName = 'Auto email notification system';

$subject = 'Confirmed';

/* Mail Address */
$toAddr = 'me@domain.com'; 
$bccAddr = 'bccperson@domain.com'; 
$fromAddr = 'no-reply@domain.com';
/* End Mail Address */


/* Mail Body */
$msg = '
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
my messages about confirmation...
</body>
</html>
';

$msg = wordwrap($msg, 70);
/* End Mail Body */


/* Mail Headers Setup */
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: ".$fromName." <".$fromAddr.">";
$headers[] = "Bcc: <".$bccAddr.">";
/* End Mail Headers Setup */


mail($toAddr, $subject, $msg, implode("\r\n", $headers));

}
?>

我將上面的代碼放在header.php文件的底部,對我有用。

謝謝大家的所有建議和幫助。

您可能希望PHP的mail函數發送電子郵件,而WordPress is_page()之類的東西可以在您希望發送電子郵件時標識頁面,因此

<?php 
if(is_page()) :
  mail('email@address.com','My Subject','My Message');
endif;
?>

另外,請查看is_page底部的“相關”部分-可能是您想對要發送電子郵件的哪一頁進行其他確定。

嘿@woodykiddy為頁面創建一個模板,並將此代碼放入頁面中。 每次加載頁面時,此條件返回true。

// Example using the array form of $headers
// assumes $to, $subject, $message have already been defined earlier...

$headers[] = 'From: Me Myself <me@example.net>';
$headers[] = 'Cc: Aravind B Codex <abc@wordpress.org>';
$headers[] = 'Cc: iluvwp@wordpress.org'; // note you can just use a simple email address

<?php 
if(is_page()) :
    wp_mail( $to, $subject, $message, $headers );
endif;
?>

http://codex.wordpress.org/Function_Reference/wp_mail

希望這會幫助你。

創建模板很容易。 創建一個新頁面my-template.php將此代碼放在頂部。

<?php
    /*
    Template Name: My New Template
    */
    ?>

但這取決於您的主題。 我為你編輯了二十本。 它會給您一個創建模板的想法。

<?php
    /*
    Template Name: My New Template
    */

    get_header();

    $headers[] = 'From: Me Myself <me@example.net>';
    $headers[] = 'Cc: Aravind B Codex <abc@wordpress.org>';
    $headers[] = 'Cc: iluvwp@wordpress.org'; // note you can just use a simple email address
?>

<?php 
if(is_page()) :
    wp_mail( $to, $subject, $message, $headers );
endif;
?>


<div id="container">
    <div id="content" role="main">

    <?php
    /* Run the loop to output the page.
     * If you want to overload this in a child theme then include a file
     * called loop-page.php and that will be used instead.
     */
    get_template_part( 'loop', 'page' );
    ?>

    </div><!-- #content -->

</div><!-- #container -->

將電子郵件代碼放入其中。 將其保存在模板目錄中。

轉到管理面板並添加/編輯頁面。 頁面右側有一個選項(模板)。 您的模板將顯示在下拉菜單中。 選擇模板並保存頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM