Closed jberanek closed 1 year ago
This should be simple if you start from the latest version of MRBS in the trunk. Just add a new column to the entry and repeat tables called "email" or whatever. MRBS will automatically pick this up and add it to the booking form. Then in notifyAdminOnBooking() and again in notifyAdminOnDelete() in functions_mail.inc just add
if (!empty($data[$custom_fields['email']])) { $recipients[] = $data[$custom_fields['email']]; }
and that should be it! (I haven't tested this though, so I may have got it a bit wrong). You can also make the field private and/or mandatory by setting some config options. Note also that you'll be able to enter either a single email address or a comma separated list of email addresses.
For further refinement you could validate the email address as well. Have a look in edit_area_room.php to see how this is done.
Campbell
Original comment by: campbell-m
Original comment by: jeffldc
Attachments: https://sourceforge.net/p/mrbs/support-requests/_discuss/thread/a4122da1/aa24/attachment/emailfield.jpg
Thanks for the input on the mail function. However, when I added the email field to the tables, it added the email column as a text field on the booking forms. I attached a screenshot to clarify this. Do I need to edit the lang.en and edit_entry pages further?
Original comment by: jeffldc
If the SQL column is of type text or else a varchar of length > $text_input_max (a config variable, default 70) characters then the form field is a textarea (as in your screenshot) otherwise it's a text input (similar to the Brief Description). So either change the email column to be a varchar(70) or less, or else increase $text_input_max in the config file.
To change the label on the form you add a line in the appropriate lang file(s) of the format $vocab["tablename.columnname"] = "Field Description";
For example add the following to lang.en
$vocab["entry.email"] = "Email address";
Campbell
Original comment by: campbell-m
modded functions.mail.inc
Original comment by: jeffldc
Attachments: https://sourceforge.net/p/mrbs/support-requests/_discuss/thread/a4122da1/b5d4/attachment/functions_mail.inc
Campbell,
Since the project i'm working on doesn't require room admins, I replaced the code with the code snippet you suggested earlier for custom fields. In this case I created a custom field named "user_email" which when supplied should email that email. Just having a problem getting the system to email my new custom field.
I inserted the code here. In the config file I have all the settings = True. Room admin mail works fine when an email is supplied.
if ($mail_settings['room_admin_on_bookings']) { // Custom email ** if (!empty($data[$custom_fields['user_email']])) { $recipients[] = $data[$custom_fields['user_email']]; }
// Look for list of room admins email addresses // if ($new_entry) // { // $email = get_room_admin_email($new_id, ($rep_type != REP_NONE)); // if (!empty($email)) // { // $recipients[] = $email; // } // } // elseif (!empty($mail_previous['room_admin_email'])) // { // if this is an edited entry, we already have room_admin_email, // avoiding a database hit. // $recipients[] = $mail_previous['room_admin_email']; //} }
Original comment by: jeffldc
Sorry - my fault! As I said, I hadn't tested it and sure enough I had got it a bit wrong. The code you actually want is:
if (!empty($custom_fields['user_email']))
{
$recipients[] = $custom_fields['user_email'];
}
if (!$new_entry && !empty($mail_previous['user_email']))
{
$recipients[] = $mail_previous['user_email'];
}
The problem was that the email address is actually in the array $custom_fields, not the array $data[$custom_fields]. I got confused because in the branch provisional_bookings_new_style, which will soon be merged into the trunk, the code has been rewritten slightly and now everything is passed to the mail functions in the array $data and so $data[$custom_fields['user_email']] would be right.
And just to confuse matters even more, I am thinking at the moment that in due course this should be rewritten again so that everything is in $data with no sub arrays - ie the field you would need would be called $data['user_email'], ie working the same way as $mail_previous. So watch out for this change at some stage in the future.
Note also that in the snippet above I have added some code to mail the previous email address if the booking has been edited. This covers the possibility that someone has changed the email address during the edit, in which case presumably the previous email recipient would like to know about it? (Any duplicate addresses get stripped out later).
Campbell
Original comment by: campbell-m
Worked great. Thanks.
Original comment by: jeffldc
I'm guessing this is all a bit out of date with the latest version (eg I can't see a recipients string etc) - but I've tried regardless, and can't get this to send to custom email addresses - could anyone help me with the required code to send (I've created the [user_email] field with a varchar of 65, and that shows in the form fine, it's now a case of getting it to send bookings / notifications to that address), I attempted adapting the code here, but by no stretch being a dev, failed - horrificly!
Original comment by: rstrich
Hi!
I also tried this solution.. but i cannot receive mail.. the code:
if (!empty($custom_fields['email_add'])) { $recipients[] = $custom_fields['email_add']; } if (!$new_entry && !empty($mail_previous['email_add'])) { $recipients[] = $mail_previous['email_add']; }
will be in functions_mail.inc (notifyAdminonBooking and notifyAdminOnDelete) but no emails.. Please help;
Original comment by: *anonymous
Hi! i already followed this thread on how to set the email notification, but no luck. I still cannot receive email. I want the booker to receive the email, since i dont have room admin.. i already modified the functions_mail.inc, i dont know what went wrong. please see attached file. Can u pls help me to point out what i am missing to this? Advance thanks :)
Original comment by: jidpq
Attachments: https://sourceforge.net/p/mrbs/support-requests/_discuss/thread/a4122da1/7a81/8f28/attachment/functions_mail2.inc
same here no mails.
Original comment by: *anonymous
Hello, after 8 hrs.. i got the solution for this. do not include any function or code just add one line
thats it!!!!
all will receive emails. administrators/ booker / and the custom added email id
Original comment by: *anonymous
Could I please have some help to setup a custom email? We'd like to send an sms (via email) to notify people of bookings. I'd like to add a global custom field labelled mobile number, then if it's been populated, have MRBS send an email to mobilenumber@oursmsprovider.com when the booking is made .
The sms provider cant support sending of booking files so the email would need to contain text of the booking time and date plus location. I've updated to MRBS V 1.7 so whater will work witht he latest version please.
Original comment by: gwilsonau
Do you want to have one email going to the mobile number, with a short summary, and the standard one, with the booking attachment going to everybody else? In which case you need to modify functions_mail.inc and add something like the following code to notifyAdminOnBooking() just before the end, ie just before the return $result;
line:
if (!empty($data['mobile']))
{
$message = 'Your booking is confirmed. ' .
'Start time: ' . getMailDateString($data['start_time']) .
'Location: ' . $data['area_name'] . " - " . $data['room_name'];
sendMail(array('from' => $mail_settings['from'],
'to' => $data['mobile'] . '@oursmsprovider.com'),
'Subject',
$message,
null,
null);
}
I haven't tested this.
Original comment by: campbell-m
Excellent! Thank you very much. That's exactly what I need. I'll test this code today.
Original comment by: gwilsonau
I've added the column mobile as a int. It's displaying and populating ok. I've added the above code to functions_mail.inc but no additional emails are being sent (from mail log.). We're suing smtp mail, not sendMail. Does the code above need to be changed to reflect that? Thanks.
Original comment by: gwilsonau
No, it won't matter that you are using smtp: sendMail() is the MRBS function that sends mail and will use whatever you have the backend set to in your config file.
Are you getting ordinary emails?
I've added some debugging output to the code above and so it is now:
mail_debug("Mobile: " . print_r($data['mobile'], true));
if (!empty($data['mobile']))
{
$message = 'Your booking is confirmed. ' .
'Start time: ' . getMailDateString($data['start_time']) .
'Location: ' . $data['area_name'] . " - " . $data['room_name'];
mail_debug("About to call sendMail for SMS alert");
sendMail(array('from' => $mail_settings['from'],
'to' => $data['mobile'] . '@oursmsprovider.com'),
'Subject',
$message,
null,
null);
}
To turn on mail debugging set the following in your config file:
$mail_settings['debug'] = true;
By default the output will go to your PHP error log. If you don't know where that is then you can direct the debug output to your browser by adding:
$mail_settings['debug_output'] = 'browser';
Original comment by: campbell-m
Thanks for that. I turned onthe debug with the existing code, then added the extra debug lines to the mail file.
The email to the room admin shows in both debugs. In both cases no email is sent to the sms gateway as mobile variable is listed as blank in the debug.
The mobile number IS getting entered into the entry table but for some reason the mail code isn't seeing it.
Another small glitch is that the leading zero is getting stripped from mobile numbers when stored - ours are 10 digit numbers starting with 04. I've created the moilbe column as type Int. Do I need to change that to a varchar to record the leading zero.
Many thanks for your help.
Greg.
Original comment by: gwilsonau
I did wonder when you said your custom field was an integer...I'd say it should be a text/varchar instead, otherwise issues like dropping 0s (and not supporting spaces etc.) will occur.
Original comment by: jberanek
By the way, I just added a 'mobile' column to my mrbs_entry and mrbs_repeat tables (you added to both I assume?) and the following code in notifyAdminOnBooking() shows the mobile number entered:
error_log("Mobile is: ".$data['mobile']);
Original comment by: jberanek
Ok. I finally found my mistake. There are a number of return $result lines in the functions.mail.inc file. I hadn't realised that and just searched for return $result and put the code there.
I've now moved the code to the end of the file before the last return $result and it's working.
Many thanks for your perserverance with this one.
Cheers,
Greg.
Original comment by: *anonymous
John,
I now have sms flowing from email but it only contains "Y". i.e no content. I've been though the mail logs and that is what's being emailed out. I thought that the problem may be that you've re-used the varaible $message so I've changed that to $message1 in your code above. Still no luck. I've also added the line mail_debug($message1); to try and get the mail log to output the contents of the variable but this isn't doing anything.
Could you please take anothr look at the code for me?
I'm sorry to keep bugging you but I'm very close now.
Thanks again,
Greg.
Original comment by: *anonymous
I think the problem may be that sendMail is sending a Mime attachment which the SMS provider may not like.
So try replacing the code above with:
mail_debug("Mobile: " . print_r($data['mobile'], true));
if (!empty($data['mobile']))
{
$message = 'Your booking is confirmed. ' .
'Start time: ' . getMailDateString($data['start_time']) .
'Location: ' . $data['area_name'] . " - " . $data['room_name'];
mail_debug("About to send mail for SMS alert");
simpleSendMail($mail_settings['from'],
$data['mobile'] . '@oursmsprovider.com',
'Subject',
$message);
}
and adding this new function right at the bottom of functions_mail.inc:
function simpleSendMail($from, $to, $subject, $body)
{
global $mail_settings, $sendmail_settings, $smtp_settings;
$mail = new \PHPMailer;
switch ($mail_settings['admin_backend'])
{
case 'mail':
$mail->isMail();
break;
case 'qmail':
$mail->isQmail();
if (isset($mail_settings['qmail']['qmail-inject-path']))
{
$mail->Sendmail = $mail_settings['qmail']['qmail-inject-path'];
}
break;
case 'sendmail':
$mail->isSendmail();
$mail->Sendmail = $sendmail_settings['path'];
if (isset($sendmail_settings['args']) && ($sendmail_settings['args'] !== ''))
{
$mail->Sendmail .= ' ' . $sendmail_settings['args'];
}
break;
case 'smtp':
$mail->isSMTP();
$mail->Host = $smtp_settings['host'];
$mail->Port = $smtp_settings['port'];
$mail->SMTPAuth = $smtp_settings['auth'];
$mail->SMTPSecure = $smtp_settings['secure'];
$mail->Username = $smtp_settings['username'];
$mail->Password = $smtp_settings['password'];
if ($smtp_settings['disable_opportunistic_tls'])
{
$mail->SMTPAutoTLS = false;
}
$mail->SMTPOptions = array
(
'ssl' => array
(
'verify_peer' => $smtp_settings['ssl_verify_peer'],
'verify_peer_name' => $smtp_settings['ssl_verify_peer_name'],
'allow_self_signed' => $smtp_settings['ssl_allow_self_signed']
)
);
break;
default:
$mail->isMail();
trigger_error("Unknown mail backend '" . $mail_settings['admin_backend'] . "'." .
" Defaulting to 'mail'.",
E_USER_WARNING);
break;
}
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
}
Original comment by: campbell-m
Campbell,
I've been able to view the emails to the sms provider so I new they contained legible text, which was just "Subject Y".
I've managed to get the previous code working by removing the subject so this works for me:
mail_debug("Mobile: " . print_r($data['mobile'], true));
if (!empty($data['mobile']))
{
$message = 'Your booking is confirmed. ' .
'Start time: ' . getMailDateString($data['start_time']) .
'Location: ' . $data['area_name'] . " - " . $data['room_name'];
mail_debug("About to call sendMail for SMS alert");
sendMail(array('from' => $mail_settings['from'],
'to' => $data['mobile'] . '@oursmsprovider.com'),
$message,
null,
null);
}
Many thanks for your and John's help! This is a great addition to our calendar.
Cheers,
Greg.
Original comment by: *anonymous
Good, glad it's working. What you've actually done is put the message in as the subject, rather than remove the subject. It must be that your SMS provider likes the message to go in as the subject and it ignores the body.
Strictly speaking you should put another null in that call to sendMail so that you are giving it the right number of parameters. So the code should be:
mail_debug("Mobile: " . print_r($data['mobile'], true));
if (!empty($data['mobile']))
{
$message = 'Your booking is confirmed. ' .
'Start time: ' . getMailDateString($data['start_time']) .
'Location: ' . $data['area_name'] . " - " . $data['room_name'];
mail_debug("About to call sendMail for SMS alert");
sendMail(array('from' => $mail_settings['from'],
'to' => $data['mobile'] . '@oursmsprovider.com'),
$message,
null,
null,
null);
}
Original comment by: campbell-m
Ok. I've taken the time to examine the outgoing emails thoroughly. Prior to me removing Subject from the code, the emails were going out with the Subject "Subject" and the message body "Y". (My quotation marks.) I have no idea why $message was sending as Y. Now (Subject removed from code) the emails are going our with the $message as the subject and the body completely empty. As you said, these are being accepted by our SMS provider and the email's subject is being sent out. Althugh it's working ok it would be nice to get the emails formatted properly. I don't know enough php coding to change much. Any suggestions?
Original comment by: *anonymous
I've tried populating a variable $subj and putting that into the array after to but of course this did the same thing - the emails go out with the subject ok and the message body as "Y". For now, I've removed the subject line again and added the extra null into the array as you suggested. Emails are once again going out with the message as subject which is being converted to an SMS ok. It's a curly one!
Original comment by: *anonymous
It sounds like everything's working OK isn't it? I'm not quite sure what you mean by wanting to get the emails formatted properly. Surely there's no formatting in an SMS message - it's just plain text?
Original comment by: campbell-m
The SMS is working with the message is being emailed as a long subject and no body. Our sms gateway concatenates both the email subject and body as the SMS. I was just trying to work out why the php mail function was sending the email body as Y when the subject is populated.
Original comment by: *anonymous
I don't think it's the MRBS or PHP mail function sending the email body as 'Y'. I think it's the SMS provider interpreting it as 'Y'. You could check this by substituting a normal email address for the mobile email address, ie substitute abc@example.com
for $data['mobile'] . '@oursmsprovider.com'
in the code above and then using your email client's tools to examine the message source and headers when it arrives.
Original comment by: campbell-m
I am stuck with implementing sending an SMS confirmation to the booker. Reading the entire thread and following the suggestions to no success. For debugging purposes I send a normal email to myself to see the result, which is: the body contains only one character and it is the first character of the message text. The same happens when I mail it to the sms provider... the sms arrives in my handy but the text is only the first character of the message text.
I added the following code into functions_mail.inc after line 947:
// Send a confirmation SMS to the booker
//
mail_debug("Mobile: " . print_r($data['name'], true));
// if (!empty($data['MobileNr'])) // this does not work, returns an empty string only
// if (!empty($data[$custom_fields['MobileNr']])) // // this does not work too, returns an empty string only
if (!empty($data['name'])) // until above problem solved I use the name field to provide handy number
{
$subject = 'Res Club';
$message = 'This is the message text'; // simple textstring for debugging
// $message = 'Buchung bestaetigt: ' .
// 'Start time: ' . getMailDateString($data['start_time']) .
// 'Location: ' . $data['area_name'] . " - " . $data['room_name'];
mail_debug("Message: " . print_r($message, true));
mail_debug("About to call sendMail for SMS alert");
sendMail(array('from' => $mail_settings['from'],
// 'to' => $data['name'] . '@xxxxxxxxxx.inetworx.ch'),
'to' => 'club-r@bluewin.ch'), // to myself for debugging only
$subject,
$message,
null,
null);
}
which results in the following debug output:
[DEBUG] Mobile: 0792259649 [DEBUG] Message: This is the message text [DEBUG] About to call sendMail for SMS alert [DEBUG] Preparing to send email ... [DEBUG] Using backend 'smtp' [DEBUG] From: club-r@bluewin.ch [DEBUG] To: club-r@bluewin.ch [DEBUG] Cc: [DEBUG] Bcc: 2019-12-14 17:54:00 Connection: opening to mail.bluewin.ch:25, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => true, 'verify_peer_name' => true, 'allow_self_signed' => false, ),) 2019-12-14 17:54:00 Connection: opened 2019-12-14 17:54:00 SERVER -> CLIENT: 220 mail.lb.bluewin.ch vimdzmsp-mail01.bluewin.ch Swisscom AG ESMTP server ready 2019-12-14 17:54:00 CLIENT -> SERVER: EHLO 192.168.1.122 2019-12-14 17:54:00 SERVER -> CLIENT: 250-mail.lb.bluewin.ch hello [178.194.201.142], pleased to meet you250-SIZE 26214400250-ENHANCEDSTATUSCODES250-PIPELINING250-8BITMIME250 OK 2019-12-14 17:54:00 CLIENT -> SERVER: MAIL FROM:club-r@bluewin.ch 2019-12-14 17:54:00 SERVER -> CLIENT: 250 2.1.0 club-r@bluewin.ch sender ok 2019-12-14 17:54:00 CLIENT -> SERVER: RCPT TO:club-r@bluewin.ch 2019-12-14 17:54:00 SERVER -> CLIENT: 250 2.1.5 club-r@bluewin.ch recipient ok 2019-12-14 17:54:00 CLIENT -> SERVER: DATA 2019-12-14 17:54:00 SERVER -> CLIENT: 354 OK 2019-12-14 17:54:00 CLIENT -> SERVER: Date: Sat, 14 Dec 2019 18:54:00 +0100 2019-12-14 17:54:00 CLIENT -> SERVER: To: club-r@bluewin.ch 2019-12-14 17:54:00 CLIENT -> SERVER: From: club-r@bluewin.ch 2019-12-14 17:54:00 CLIENT -> SERVER: Subject: Res Club 2019-12-14 17:54:00 CLIENT -> SERVER: Message-ID: 9af7f3b30242d1296bcc58a6abbca5fe@192.168.1.122 2019-12-14 17:54:00 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.27 (https://github.com/PHPMailer/PHPMailer) 2019-12-14 17:54:00 CLIENT -> SERVER: Auto-Submitted: auto-generated 2019-12-14 17:54:00 CLIENT -> SERVER: MIME-Version: 1.0 2019-12-14 17:54:00 CLIENT -> SERVER: Content-Type: multipart/alternative; 2019-12-14 17:54:00 CLIENT -> SERVER: boundary="=_09957a2347982e23eb8cccca0489b6da"; charset=utf-8 2019-12-14 17:54:00 CLIENT -> SERVER: 2019-12-14 17:54:00 CLIENT -> SERVER: --=_09957a2347982e23eb8cccca0489b6da 2019-12-14 17:54:00 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2019-12-14 17:54:00 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii 2019-12-14 17:54:00 CLIENT -> SERVER: 2019-12-14 17:54:00 CLIENT -> SERVER: T 2019-12-14 17:54:00 CLIENT -> SERVER: --=_09957a2347982e23eb8cccca0489b6da-- 2019-12-14 17:54:00 CLIENT -> SERVER: 2019-12-14 17:54:00 CLIENT -> SERVER: . 2019-12-14 17:54:00 SERVER -> CLIENT: 250 2.0.0 gBS8iwpGGUvCzgBS8i2iYb mail accepted for delivery 2019-12-14 17:54:00 CLIENT -> SERVER: QUIT 2019-12-14 17:54:00 SERVER -> CLIENT: 221 2.0.0 mail.lb.bluewin.ch vimdzmsp-mail01.bluewin.ch Swisscom AG closing connection 2019-12-14 17:54:00 Connection: closed [DEBUG] Email sent successfully
and the email as captured and shown below:
Original comment by: hanspeterh
its not the sms provider.. see my problem description below.. , thanks
Original comment by: hanspeterh
Have you added the simpleSendMail() function above? Your code above is calling sendMail().
Original comment by: campbell-m
No, do I have to implement your simpleSendMail() function? I can try of course but previous post indicated that it would work without.... and that the problem is with the sms provider. But the test with the normal non sms email shows, that the normal email is wrong too and the sms is getting sent with the sms provider, only the content is wrong in the same way as the normal email shows, i.e. only the first character of message is sent.
Original comment by: hanspeterh
No, do I have to implement your simpleSendMail() function?
It probably depends on your SMS provider. But if you are using the standard sendMail() then you are passing the parameters in the wrong form. $addresses
and $text_body
should both be arrays.
* @param array $addresses an array of addresses, each being a comma
* separated list of email addresses. Indexed by
* 'from'
* 'to'
* 'cc'
* 'bcc'
* @param string $subject email subject
* @param array $text_body text part of body, an array consisting of
* 'content' the content itself
* 'cid' the content id
* @param array $html_body HTML part of body, an array consisting of
* 'content' the content itself
* 'cid' the content id
* @param array $attachment file to attach. An array consisting of
* 'content' the file or data to attach
* 'method' the iCalendar METHOD
* 'name' the name to give it
* @param string $charset character set used in body
* @return bool TRUE or PEAR error object if fails
*/
function sendMail($addresses, $subject, $text_body, $html_body, $attachment, $charset = 'us-ascii')
Original comment by: campbell-m
with the simple sendMail the sms is being sent. For the previously used code I was following the example given further above but this was from february 21, 2018 and the code obviously is no longer valid. I will try to implement it without using your simple mail Solution just to keep the modifications to mrbs as minimal as possible. Thanks for your help so far.
The problem still open is: how can I get the content of the field 'MobileNr' which is a custom field in the "users" table. From the old examples in the thread it should be something as $data['MobileNr'] but this returns an empty string as well as $data[$custom_fields['MobileNr']] or do I have to get the value by issuing an SQL?
Original comment by: hanspeterh
In the old examples the mobile number is a custom field in the entry table and entered at the time of booking. If it's a custom field in the users table, then, yes, you will have to query the users table.
Original comment by: campbell-m
To me, the old example has a wrong design, because the mobile number is an attribute of the user and not an attribute of a cerctain booking. So I will have to do a query to the users table. Thanks for your help! btw. the feature of implementing customer fields is well documented, but there is no information about using these fields, i.e. getting their value.
Original comment by: hanspeterh
I am in the process of implementing SMS confirmations to MRBS 9.2 following above suggestions. When doing so, I got a PHP error telling me "PHPMailer class not found". To get it work I had to change the line
$mail = new \PHPMailer;
to $mail = new PHPMailer();
to make the simpleSendMail function work and get the SMS sent.
Original comment by: hanspeterh
Can you point me in the right direction if I were to add an "email receipant" field when booking an appointment? Basicly an admin would manually type in the email when booking a new appointment and the data from the booking would get sent to whoever they specified int he email field.
Reported by: jeffldc
Original Ticket: mrbs/support-requests/186