Mail

Work with e-mail based on a plug-in PHPMailer. Connection DIAFAN.CMS and plugin provides a function send_mail, described in the file includes/mail.php. In CMS this function is mainly used to notify users of any action, such as purchasing a product (modules/cart/cart.action.php).

Методы

boolean send_mail (string|array $recipient, string $subject, string $body, [string $from = '']) – Отправляет электронное письмо.

  • string|array $recipient: получатель/получатели
  • string $subject: тема письма
  • string $body: содержание письма
  • string $from: адрес отправителя

Example:

// send Vasya a notice on the mailbox each time you view it product
// file modules/shop/shop.model.php method id()
$sent = send_mail(
    
"vasya@mail.ru", // recipient
    
"Someone watching your product on the website ".BASE_PATH_HREF, // subject
    
"Hey, someone has come to your product page
    "
.BASE_PATH_HREF.$this->diafan->_route->current_link(), // message
); // === true, if a letter has been sent successfully

Example

For example, the store sell button "Tell a friend", when clicked, a form appears with the input field email.

Example:

Create file modules/shop/views/shop.view.email_form.php

<div class="modal-wrap" id="shop-modal-wrap">
   <div class="modal" id="shop-modal">
       <div class="window" style="width:300px">
           <div class="window_title">
               Send to frend
           </div>
           <form method="POST" enctype="multipart/form-data" action="" class="ajax">
               <input type="hidden" name="module" value="shop">
               <input type="hidden" name="action" value="mail">
               <input type="hidden" name="good_id" value="<?php echo $result['id'];?>">
               <table style="width:300px">
                   <tr>
                       <td>E-mail:</td>
                       <td>
                           <input type="email" name="mail">
                           <div class="errors error_mail" style="display:none"></div>
                       </td>
                   </tr>
               </table>
               <input type="submit" value="Send">
           </form>
           <div class="errors error"></div>
           <div class="modal-close close"></div>
       </div>
   </div>
</div>

In the file modules/shop/views/shop.view.id.php write:

$this->get('email_form', 'shop', $result);

In controller modules/shop/shop.php add:

switch ($_POST['action'])
{
    case
'mail':
    return
$this->action->send_friends();
    
// ...
}

Now add handler of POST-requests in the file modules/shop/shop.action.php:

public function send_friends()
{
    if (empty(
$_POST['mail']))
    {
        
$this->result['errors']['mail'] = 'Please, enter e-mail.';
    }
    else
    {
        
$this->valid_email($_POST['mail'], "mail");
    }
    
$row = DB::query_fetch_array("SELECT [name], site_id, cat_id, id FROM {shop} WHERE id=%d LIMIT 1", $_POST['good_id']);
    if (empty(
$row))
    {
        
$this->result['errors'][0] = 'Error.';
    }

    if (
$this->result())
            return;

    
$row['link'] = BASE_PATH_HREF.$this->diafan->_route->link($row["site_id"], "shop", $row["id"]);

    
Custom::inc(ABSOLUTE_PATH.'includes/mail.php');
    
$subject = "I recommend to watch";
    
$message = 'Hello! Your friend recommends you to view <a href="'.$row['link'].'">'.$row['name'].'</a>.';
    
$from_mail = $this->diafan->configmodules("emailconf", 'shop') ? $this->diafan->configmodules("email", 'shop') : '';

    
$this->result['errors'][0] = send_mail($_POST['mail'], $subject, $message, $from_mail) ? 'Sent' : 'Error';
}