noalyss Version-9
sendmail.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@noalyss.eu
21
22/*!
23 * \file
24 * \brief Send email for Noalyss after checking if it is possible : if cannot be sent if the limit of max email
25 * is reached,
26 * @see sendmail_core
27 */
28/*!
29 * \class Sendmail
30 * \brief Send email for Noalyss after checking if it is possible : if cannot be sent if the limit of max email
31 * is reached,
32 * @see sendmail_core
33 */
34
36{
37 /**
38 * @brief Send the message
39 * @throws Exception
40 */
41 function send()
42 {
43 if ( $this->can_send() == false ) throw new Exception(_('Email non envoyé'),EMAIL_LIMIT);
44
45 if (!mail($this->mailto, $this->subject, $this->content,$this->header))
46 {
47 throw new Exception('send failed');
48 }
49 // Increment email amount
50 $repo =new Database();
51 $date=date('Ymd');
52 $http=new HttpInput();
53 $dossier=$http->request("gDossier","string", -1);
55 }
56 /**
57 * @brief Check if email can be sent from a folder
58 * @return boolean
59 */
60 function can_send() {
61 /**
62 * if send from a dossier , then check limit of this dossier
63 * otherwise send true
64 */
65 $http=new HttpInput();
66 $dossier=$http->request("gDossier","string", -1);
67 if ($dossier == -1 ) return true;
68
69 /**
70 * Compare max value in repo
71 */
72 $repo =new Database();
73 $date=date('Ymd');
74 // get max email
75 $max_email = $this->get_max_email($repo,$dossier);
76 //
77 // This folder cannot send email
78 if ($max_email == 0 ) return false;
79 //
80 // This folder has unlimited email
81 if ($max_email == -1 ) return true;
82
83 // get email sent today from account_repository
84 $email_sent = $this->get_email_sent($repo,$dossier , $date);
85 //
86 if ( $email_sent >= $max_email) return false;
87
88 return true;
89 }
90 /**
91 *@brief return max email the folder can send
92 * @param $p_repo Database
93 * @param $p_dossier_id int
94 */
95 function get_max_email(Database $p_repo,$p_dossier_id)
96 {
97 $max_email = $p_repo->get_value("select dos_email from ac_dossier where dos_id=$1",
98 array($p_dossier_id));
99 if ($max_email == "") return 0;
100 return $max_email;
101 }
102 /**
103 * @brief Return the amount of send emails for the date (YYYYMMDD)
104 * @param Database $p_repo
105 * @param $p_dossier_id int
106 * @param $p_date string YYYYMMDD
107 */
108 function get_email_sent(Database $p_repo,$p_dossier_id,$p_date)
109 {
110
111 $email_sent = $p_repo->get_value ('select de_sent_email from dossier_sent_email where dos_id = $1 and de_date=$2',
112 array($p_dossier_id,$p_date));
113 if ($email_sent == "") return 0;
114 return $email_sent;
115
116
117 }
118
119 /**
120 * @brief check if there is a mandatory domain, if yes
121 * @return void
122 * @throws Exception
123 */
124 function verify()
125 {
126 try {
127 parent::verify();
128 } catch (\Exception $e) {
129 throw $e;
130 }
131 if ( defined('ALLOWED_EMAIL_DOMAIN') )
132 {
133 if ( ALLOWED_EMAIL_DOMAIN != "")
134 {
135 $as_domain=explode(",", ALLOWED_EMAIL_DOMAIN);
136 $valid=0;
137 foreach ($as_domain as $domain) {
138 $domain="@$domain";
139 if (strpos($this->from,$domain) != 0 ) {
140 $valid=1;
141 break;
142 }
143 }
144 if ( $valid == 0) {
145 throw new Exception("Domaine email {$this->from} interdit",EXC_INVALID);
146
147 }
148
149 }
150
151 }
152
153 }
154
155 /**
156 * @brief Add $p_amount_email to email sent
157 * @param $p_repo Database
158 * @param $p_dossier int id of the folder (dossier.dos_id)
159 * @param $p_date string (YYYYMMDD)
160 */
161 function increment_mail(Database $p_repo,$p_dossier,$p_date)
162 {
163 if ( $p_dossier == -1) return ;
164 $email_sent = $this->get_email_sent($p_repo,$p_dossier,$p_date);
165 if ( $email_sent == 0 ){
166 $p_repo->exec_sql("insert into public.dossier_sent_email(de_sent_email,dos_id,de_date) values($1,$2,$3)",
167 array(1,$p_dossier,$p_date));
168 return;
169 } else {
170 // update + sp_emaoun_email
171 $p_repo->exec_sql("update dossier_sent_email set de_sent_email=de_sent_email+1 where dos_id=$1 and de_date=$2",
172 array($p_dossier,$p_date));
173 }
174 }
175
176}
$anc_grandlivre from
if(isset($_REQUEST['gDossier']) && $http->request("gDossier","number", 0) !=0) $repo
else $card content[$j]['j_montant']
$dossier
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
exec_sql($p_string, $p_array=null)
send a sql string to the database
contains the class for connecting to Noalyss
manage the http input (get , post, request) and extract from an array
Description of Sendmail.
mailto($p_mailto)
set the recipient
Send email for Noalyss after checking if it is possible : if cannot be sent if the limit of max email...
get_email_sent(Database $p_repo, $p_dossier_id, $p_date)
Return the amount of send emails for the date (YYYYMMDD)
verify()
check if there is a mandatory domain, if yes
send()
Send the message.
increment_mail(Database $p_repo, $p_dossier, $p_date)
Add $p_amount_email to email sent.
can_send()
Check if email can be sent from a folder.
get_max_email(Database $p_repo, $p_dossier_id)
return max email the folder can send
const EXC_INVALID
Definition: constant.php:346
const EMAIL_LIMIT
Exception.
Definition: constant.php:342
$valid
Definition: recover.php:57