You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
3.8 KiB

  1. // EMAIL
  2. // Autosuggest defaults
  3. _e.email_as_field = 'email_id';
  4. _e.email_as_dt = 'Contact';
  5. _e.email_as_in = 'email_id,contact_name';
  6. sendmail = function(emailto, emailfrom, cc, subject, message, fmt, with_attachments) {
  7. var fn = function(html) {
  8. $c('webnotes.utils.email_lib.send_form', {
  9. 'sendto':emailto,
  10. 'sendfrom': emailfrom?emailfrom:'',
  11. 'cc':cc?cc:'',
  12. 'subject':subject,
  13. 'message':replace_newlines(message),
  14. 'body':html,
  15. 'full_domain': wn.urllib.get_base_url(),
  16. 'with_attachments':with_attachments ? 1 : 0,
  17. 'dt':cur_frm.doctype,
  18. 'dn':cur_frm.docname
  19. },
  20. function(r, rtxt) {
  21. //
  22. }
  23. );
  24. }
  25. // build print format
  26. _p.build(fmt, fn);
  27. }
  28. _e.make = function() {
  29. var d = new Dialog(440, 440, "Send Email");
  30. var email_go = function() {
  31. var emailfrom = d.widgets['From'].value;
  32. var emailto = d.widgets['To'].value;
  33. if(!emailfrom)
  34. emailfrom = user_email;
  35. // validate email ids
  36. var email_list = emailto.split(/[,|;]/);
  37. var valid = 1;
  38. for(var i=0;i<email_list.length;i++){
  39. if(!validate_email(email_list[i])) {
  40. msgprint('error:'+email_list[i] + ' is not a valid email id');
  41. valid = 0;
  42. }
  43. }
  44. // validate from
  45. if(emailfrom && !validate_email(emailfrom)) {
  46. msgprint('error:'+ emailfrom + ' is not a valid email id. To change the default please click on Profile on the top right of the screen and change it.');
  47. return;
  48. }
  49. if(!valid)return;
  50. var cc= emailfrom;
  51. if(!emailfrom) {
  52. emailfrom = locals['Control Panel']['Control Panel'].auto_email_id;
  53. cc = '';
  54. }
  55. sendmail(emailto, emailfrom, emailfrom, d.widgets['Subject'].value, d.widgets['Message'].value, sel_val(cur_frm.print_sel), d.widgets['Send With Attachments'].checked);
  56. _e.dialog.hide();
  57. }
  58. d.onhide = function() {
  59. hide_autosuggest();
  60. }
  61. d.make_body([
  62. ['Data','To','Example: abc@hotmail.com, xyz@yahoo.com']
  63. ,['Select','Format']
  64. ,['Data','Subject']
  65. ,['Data','From','Optional']
  66. ,['Check','Send With Attachments','Will send all attached documents (if any)']
  67. ,['Text','Message']
  68. ,['Button','Send',email_go]]
  69. );
  70. d.widgets['From'].value = (user_email ? user_email:'');
  71. $td(d.rows['Format'].tab,0,1).cur_sel = d.widgets['Format'];
  72. // ---- add auto suggest ----
  73. var opts = { script: '', json: true, maxresults: 10 };
  74. wn.require('lib/js/legacy/widgets/autosuggest.js');
  75. var as = new AutoSuggest(d.widgets['To'], opts);
  76. as.custom_select = function(txt, sel) {
  77. // ---- add to the last comma ----
  78. var r = '';
  79. var tl = txt.split(',');
  80. for(var i=0;i<tl.length-1;i++) r=r+tl[i]+',';
  81. r = r+(r?' ':'')+sel;
  82. if(r[r.length-1]==NEWLINE) r=substr(0,r.length-1);
  83. return r;
  84. }
  85. var emailto = d.widgets['To']
  86. as.set_input_value = function(new_txt) {
  87. if(emailto.value && emailto.value.indexOf(',')!=-1) {
  88. var txt = emailto.value.split(',');
  89. txt.splice(txt.length - 1, 1, new_txt);
  90. for(var i=0;i<txt.length-1;i++) txt[i] = strip(txt[i]);
  91. emailto.value = txt.join(', ');
  92. } else {
  93. emailto.value = new_txt;
  94. }
  95. }
  96. // ---- override server call ----
  97. as.doAjaxRequest = function(txt) {
  98. var pointer = as; var q = '';
  99. // ---- get last few letters typed ----
  100. var last_txt = txt.split(',');
  101. last_txt = last_txt[last_txt.length-1];
  102. // ---- show options ----
  103. var call_back = function(r,rt) {
  104. as.aSug = [];
  105. if(!r.cl) return;
  106. for (var i=0;i<r.cl.length;i++) {
  107. as.aSug.push({'id':r.cl[i], 'value':r.cl[i], 'info':''});
  108. }
  109. as.createList(as.aSug);
  110. }
  111. $c('webnotes.utils.email_lib.get_contact_list',{'select':_e.email_as_field, 'from':_e.email_as_dt, 'where':_e.email_as_in, 'txt':(last_txt ? strip(last_txt) : '%')},call_back);
  112. return;
  113. }
  114. var sel;
  115. _e.dialog = d;
  116. }