Skip to content

Commit

Permalink
Update mail setup
Browse files Browse the repository at this point in the history
  • Loading branch information
shihjay2 committed Jul 1, 2018
1 parent 80cbc97 commit 9060d26
Show file tree
Hide file tree
Showing 16 changed files with 597 additions and 91 deletions.
51 changes: 31 additions & 20 deletions app/Http/Controllers/Controller.php
Expand Up @@ -16049,26 +16049,30 @@ protected function send_fax($job_id, $faxnumber, $faxrecipient)
protected function send_mail($template, $data_message, $subject, $to, $practice_id)
{
$practice = DB::table('practiceinfo')->where('practice_id', '=', $practice_id)->first();
$file = File::get(base_path() . "/.google");
if ($file !== '') {
$file_arr = json_decode($file, true);
if (env('MAIL_HOST') == 'smtp.gmail.com') {
// $file = File::get(base_path() . "/.google");
// if ($file !== '') {
// $file_arr = json_decode($file, true);
$google = new Google_Client();
$google->setClientID($file_arr['web']['client_id']);
$google->setClientSecret($file_arr['web']['client_secret']);
$google->setClientID(env('GOOGLE_KEY'));
$google->setClientSecret(env('GOOGLE_SECRET'));
// $google->setClientID($file_arr['web']['client_id']);
// $google->setClientSecret($file_arr['web']['client_secret']);
$google->refreshToken($practice->google_refresh_token);
$credentials = $google->getAccessToken();
$data1['smtp_pass'] = $credentials['access_token'];
DB::table('practiceinfo')->where('practice_id', '=', $practice_id)->update($data1);
$config = [
'mail.driver' => 'smtp',
'mail.host' => 'smtp.gmail.com',
'mail.port' => 465,
'mail.from' => ['address' => null, 'name' => null],
'mail.encryption' => 'ssl',
'mail.username' => $practice->smtp_user,
'mail.password' => $credentials['access_token'],
'mail.sendmail' => '/usr/sbin/sendmail -bs'
];
$config['mail.password'] = $credentials['access_token'];
// $config = [
// 'mail.driver' => 'smtp',
// 'mail.host' => 'smtp.gmail.com',
// 'mail.port' => 465,
// 'mail.from' => ['address' => null, 'name' => null],
// 'mail.encryption' => 'ssl',
// 'mail.username' => $practice->smtp_user,
// 'mail.password' => $credentials['access_token'],
// 'mail.sendmail' => '/usr/sbin/sendmail -bs'
// ];
config($config);
extract(Config::get('mail'));
$transport = Swift_SmtpTransport::newInstance($host, $port, 'ssl');
Expand All @@ -16081,12 +16085,19 @@ protected function send_mail($template, $data_message, $subject, $to, $practice_
$transport->setPassword($password);
}
Mail::setSwiftMailer(new Swift_Mailer($transport));
Mail::send($template, $data_message, function ($message) use ($to, $subject, $practice) {
$message->to($to)
->from($practice->email, $practice->practice_name)
->subject($subject);
});
// Mail::send($template, $data_message, function ($message) use ($to, $subject, $practice) {
// $message->to($to)
// ->from($practice->email, $practice->practice_name)
// ->subject($subject);
// });
// }
}
Mail::send($template, $data_message, function ($message) use ($to, $subject, $practice) {
$message->to($to)
->from($practice->email, $practice->practice_name)
->subject($subject);
});
return "E-mail sent.";
return true;
}

Expand Down
128 changes: 128 additions & 0 deletions app/Http/Controllers/CoreController.php
Expand Up @@ -5994,6 +5994,134 @@ public function setup(Request $request)
return view('core', $data);
}

public function setup_mail(Request $request)
{
if (Session::get('group_id') == '1') {
if ($request->isMethod('post')) {
$this->validate($request, [
'mail_type' => 'required'
]);
$mail_arr = [
'gmail' => [
'MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => 'smtp.gmail.com',
'MAIL_PORT' => 465,
'MAIL_ENCRYPTION' => 'ssl',
'MAIL_USERNAME' => $request->input('mail_username'),
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => $request->input('google_client_id'),
'GOOGLE_SECRET' => $request->input('google_client_secret'),
'GOOGLE_REDIRECT_URI' => route('googleoauth')
],
'mailgun' => [
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => $request->input('mailgun_domain'),
'MAILGUN_SECRET' => $request->input('mailgun_secret'),
'MAIL_HOST' => '',
'MAIL_PORT' => '',
'MAIL_ENCRYPTION' => '',
'MAIL_USERNAME' => '',
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => '',
'GOOGLE_SECRET' => '',
'GOOGLE_REDIRECT_URI' => ''
],
'sparkpost' => [
'MAIL_DRIVER' => 'sparkpost',
'SPARKPOST_SECRET' => $request->input('sparkpost_secret'),
'MAIL_HOST' => '',
'MAIL_PORT' => '',
'MAIL_ENCRYPTION' => '',
'MAIL_USERNAME' => '',
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => '',
'GOOGLE_SECRET' => '',
'GOOGLE_REDIRECT_URI' => ''
],
'ses' => [
'MAIL_DRIVER' => 'ses',
'SES_KEY' => $request->input('ses_key'),
'SES_SECRET' => $request->input('ses_secret'),
'MAIL_HOST' => '',
'MAIL_PORT' => '',
'MAIL_ENCRYPTION' => '',
'MAIL_USERNAME' => '',
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => '',
'GOOGLE_SECRET' => '',
'GOOGLE_REDIRECT_URI' => ''
],
'unique' => [
'MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => $request->input('mail_host'),
'MAIL_PORT' => $request->input('mail_port'),
'MAIL_ENCRYPTION' => $request->input('mail_encryption'),
'MAIL_USERNAME' => $request->input('mail_username'),
'MAIL_PASSWORD' => $request->input('mail_password'),
'GOOGLE_KEY' => '',
'GOOGLE_SECRET' => '',
'GOOGLE_REDIRECT_URI' => ''
]
];
$this->changeEnv($mail_arr[$request->input('mail_type')]);
if ($request->input('mail_type') == 'gmail') {
return redirect()->route('googleoauth');
} else {
return redirect()->route('setup_mail_test');
}
} else {
$data2['noheader'] = true;
$data2['assets_js'] = $this->assets_js();
$data2['assets_css'] = $this->assets_css();
$data2['mail_type'] = '';
$data2['mail_host'] = env('MAIL_HOST');
$data2['mail_port'] = env('MAIL_PORT');
$data2['mail_encryption'] = env('MAIL_ENCRYPTION');
$data2['mail_username'] = env('MAIL_USERNAME');
$data2['mail_password'] = env('MAIL_PASSWORD');
$data2['google_client_id'] = env('GOOGLE_KEY');
$data2['google_client_secret'] = env('GOOGLE_SECRET');
$data2['mail_username'] = env('MAIL_USERNAME');
$data2['mailgun_domain'] = env('MAILGUN_DOMAIN');
$data2['mailgun_secret'] = env('MAILGUN_SECRET');
$data2['mail_type'] == 'sparkpost';
$data2['sparkpost_secret'] = env('SPARKPOST_SECRET');
$data2['ses_key'] = env('SES_KEY');
$data2['ses_secret'] = env('SES_SECRET');
if (env('MAIL_DRIVER') == 'smtp') {
if (env('MAIL_HOST') == 'smtp.gmail.com') {
$data2['mail_type'] = 'gmail';
} else {
$data2['mail_type'] = 'unique';
}
} else {
$data2['mail_type'] = env('MAIL_DRIVER');
}
$data2['message_action'] = Session::get('message_action');
Session::forget('message_action');
return view('setup_mail', $data2);
}
} else {
return redirect()->route('dashboard');
}
}

public function setup_mail_test(Request $request)
{
$data_message['item'] = 'This is a test';
$practice = DB::table('practiceinfo')->where('practice_id', '=', Session::get('practice_id'))->first();
$message_action = 'Check to see in your registered e-mail account if you have recieved it. If not, please come back to the E-mail Service page and try again.';
try {
$this->send_mail('emails.blank', $data_message, 'Test E-mail', $practice->email, Session::get('practice_id'));
} catch(\Exception $e){
$message_action = 'Error - There is an error in your configuration. Please try again.';
Session::put('message_action', $message_action);
return redirect()->route('setup_mail');
}
Session::put('message_action', $message_action);
return redirect()->route('dashboard');
}

public function superquery(Request $request, $type)
{
$user = DB::table('users')->where('id', '=', Session::get('user_id'))->first();
Expand Down
153 changes: 112 additions & 41 deletions app/Http/Controllers/InstallController.php
Expand Up @@ -58,48 +58,70 @@ public function backup()

public function google_start(Request $request)
{
$query = DB::table('practiceinfo')->first();
if ($request->isMethod('post')) {
$file = $request->file('file_input');
$json = file_get_contents($file->getRealPath());
if (json_decode($json) == NULL) {
Session::put('message_action', 'Error - This is not a json file. Try again');
return redirect()->route('google_start');
}
$config_file = base_path() . '/.google';
if (file_exists($config_file)) {
unlink($config_file);
}
$directory = base_path();
$new_name = ".google";
$file->move($directory, $new_name);
Session::put('message_action', 'Google JSON file uploaded successfully');
if (! $query) {
if (file_exists(base_path() . '/.patientcentric')) {
return redirect()->route('install', ['patient']);
} else {
return redirect()->route('install', ['practice']);
}
} else {
return redirect()->route('dashboard');
}
} else {
$data['panel_header'] = 'Upload Google JSON File for GMail Integration';
$data['document_upload'] = route('google_start');
$type_arr = ['json'];
$data['document_type'] = json_encode($type_arr);
$text = "<p>You're' here because you have not installed a Google OAuth2 Client ID file. You'll need to set this up first before configuring NOSH Charting System.'</p>";
if ($query) {
$dropdown_array['default_button_text'] = '<i class="fa fa-chevron-left fa-fw fa-btn"></i>Back';
$dropdown_array['default_button_text_url'] = route('dashboard');
$data['panel_dropdown'] = $this->dropdown_build($dropdown_array);
$text = "<p>A Google OAuth2 Client ID file is already installed. Uploading a new file will overwrite the existing file!</p>";
}
$data['content'] = '<div class="alert alert-success">' . $text . '<ul><li>Instructions are on ' . HTML::link('https://github.com/shihjay2/nosh-in-a-box/wiki/How-to-get-Gmail-to-work-with-NOSH', 'this Wiki page.', ['target'=>'_blank']) . ' Please refer to it carefully.</li><li>Once you have you JSON file, upload it here.</li></ul></div>';
$data['assets_js'] = $this->assets_js('document_upload');
$data['assets_css'] = $this->assets_css('document_upload');
return view('document_upload', $data);
$google_file = base_path() . '/.google';
$file = File::get($google_file);
if ($file !== '') {
$file_arr = json_decode($file, true);
$practice = DB::table('practiceinfo')->first();
$mail_arr = [
'MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => 'smtp.gmail.com',
'MAIL_PORT' => 465,
'MAIL_ENCRYPTION' => 'ssl',
'MAIL_USERNAME' => $practice->smtp_user,
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => $file_arr['web']['client_id'],
'GOOGLE_SECRET' => $file_arr['web']['client_secret'],
'GOOGLE_REDIRECT_URI' => route('googleoauth'),
'MAILGUN_DOMAIN' => '',
'MAILGUN_SECRET' => ''
];
$this->changeEnv($mail_arr);
}
unlink($google_file);
return redirect()->route('dashboard');
// $query = DB::table('practiceinfo')->first();
// if ($request->isMethod('post')) {
// $file = $request->file('file_input');
// $json = file_get_contents($file->getRealPath());
// if (json_decode($json) == NULL) {
// Session::put('message_action', 'Error - This is not a json file. Try again');
// return redirect()->route('google_start');
// }
// $config_file = base_path() . '/.google';
// if (file_exists($config_file)) {
// unlink($config_file);
// }
// $directory = base_path();
// $new_name = ".google";
// $file->move($directory, $new_name);
// Session::put('message_action', 'Google JSON file uploaded successfully');
// if (! $query) {
// if (file_exists(base_path() . '/.patientcentric')) {
// return redirect()->route('install', ['patient']);
// } else {
// return redirect()->route('install', ['practice']);
// }
// } else {
// return redirect()->route('dashboard');
// }
// } else {
// $data['panel_header'] = 'Upload Google JSON File for GMail Integration';
// $data['document_upload'] = route('google_start');
// $type_arr = ['json'];
// $data['document_type'] = json_encode($type_arr);
// $text = "<p>You're' here because you have not installed a Google OAuth2 Client ID file. You'll need to set this up first before configuring NOSH Charting System.'</p>";
// if ($query) {
// $dropdown_array['default_button_text'] = '<i class="fa fa-chevron-left fa-fw fa-btn"></i>Back';
// $dropdown_array['default_button_text_url'] = route('dashboard');
// $data['panel_dropdown'] = $this->dropdown_build($dropdown_array);
// $text = "<p>A Google OAuth2 Client ID file is already installed. Uploading a new file will overwrite the existing file!</p>";
// }
// $data['content'] = '<div class="alert alert-success">' . $text . '<ul><li>Instructions are on ' . HTML::link('https://github.com/shihjay2/nosh-in-a-box/wiki/How-to-get-Gmail-to-work-with-NOSH', 'this Wiki page.', ['target'=>'_blank']) . ' Please refer to it carefully.</li><li>Once you have you JSON file, upload it here.</li></ul></div>';
// $data['assets_js'] = $this->assets_js('document_upload');
// $data['assets_css'] = $this->assets_css('document_upload');
// return view('document_upload', $data);
// }
}

public function install(Request $request, $type)
Expand Down Expand Up @@ -232,6 +254,55 @@ public function install(Request $request, $type)
$this->audit('Add');
$directory = $documents_dir . $pid;
mkdir($directory, 0775);
$pnosh_url = $request->root();
$pnosh_url = str_replace(array('http://','https://'), '', $pnosh_url);
$root_url = explode('/', $pnosh_url);
$root_url1 = explode('.', $root_url[2]);
$final_root_url = $root_url1[1] . '.' . $root_url1[2];
if ($final_root_url == 'hieofone.org') {
$mailgun_url = 'https://dir.' . $final_root_url . '/mailgun';
$params = ['uri' => $pnosh_url];
$post_body = json_encode($params);
$content_type = 'application/json';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $mailgun_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: {$content_type}",
'Content-Length: ' . strlen($post_body)
]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_FAILONERROR,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,0);
$mailgun_secret = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
if ($httpCode !== 404 && $httpCode !== 0) {
if ($mailgun_secret !== 'Not authorized.' && $mailgun_secret !== 'Try again.') {
$mail_arr = [
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => 'mg.hieofone.org',
'MAILGUN_SECRET' => $mailgun_secret,
'MAIL_HOST' => '',
'MAIL_PORT' => '',
'MAIL_ENCRYPTION' => '',
'MAIL_USERNAME' => '',
'MAIL_PASSWORD' => '',
'GOOGLE_KEY' => '',
'GOOGLE_SECRET' => '',
'GOOGLE_REDIRECT_URI' => ''
];
$this->changeEnv($mail_arr);
} else {

}
}
}
} else {
$displayname = 'Administrator';
}
Expand Down

0 comments on commit 9060d26

Please sign in to comment.