setMailType('html'); $email->setFrom('bbone@venbait.in', 'Nhance'); $email->setTo($emaill); if (!empty($bcc)) { // Convert the comma-separated string into an array $bccList = explode(',', $bcc); // Trim whitespace from each email ID $bccList = array_map('trim', $bccList); // Set BCC recipients $email->setBCC($bccList); } $email->setSubject($subject); $email->setMessage($message); if ($email->send()) { return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...','data' => $emaill,],200); } else { $myLogger->logme('error', "mail sent failed - $emaill"); return json_encode(['status' => 'failed','code' => 404,'message'=>'Email Sent Failed...','data' => $emaill ],404); } } catch (Exception $e) { $msg = $e->getMessage(); $myLogger->logme('error', "mail sent failed - $msg"); return json_encode(['status' => 'failed','code' => 500,'data' => $emaill],500); } } public static function bulk_mail_smtp(array $mails = []) { // print_r();die; $model = new JobModel(); $start = microtime(true); $runtime= 0; try { foreach ( $mails as $index=>$mail) { $runtime = microtime(true) - $start; $send_mail = self::send_email($mail); } return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); } catch (\Throwable $th) { return json_encode(['status' => 'failed','code' => 500],500); } } //using GmailAPI public static function send_email($params) { // print_r($params);die; $myLogger = \Config\Services::mylogger(); $gmailapi = \Config\Services::gmailapi(); $emaill = $params['mail']; $subject = $params['subject']; $message = $params['message']; //check BCC mail if (isset($params['bcc'])) { $bcc = $params['bcc']; $bcc = explode(',', $bcc); }else{ $bcc = []; } //check CC mail if (isset($params['cc'])) { $cc = $params['cc']; $cc = explode(',', $cc); }else{ $cc = []; } //check REPLY TO mail $reply_to = ""; if(isset($params['reply_to']) && !empty($params['reply_to'])){ $reply_to = $params['reply_to']; } try { $res = $gmailapi->sendMessage($emaill, $subject, $message, 'info@nhanceindia.in', $reply_to, $cc, $bcc); if($res['status']) { return json_encode(['status' => 'success','code' => 200, 'message'=>('Email Sent Successfully...'.$emaill),'data' => $res,],200); } else { $myLogger->logme('error', "mail sent failed - $emaill"); return json_encode(['status' => 'failed','code' => 404,'message'=>( 'Email Sent Failed...' . $emaill ),'data' => $res ],404); } } catch (Exception $e) { $msg = $e->getMessage(); $myLogger->logme('error', "mail sent failed - $msg"); return json_encode(['status' => 'failed','code' => 500,'data' => $msg],500); } } public static function bulk_mail(array $mails = []) { // print_r();die; $model = new JobModel(); $start = microtime(true); $runtime= 0; try { foreach ( $mails as $index=>$mail) { $runtime = microtime(true) - $start; $send_mail = self::send_email($mail); } return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...'],200); } catch (\Throwable $th) { return json_encode(['status' => 'failed','code' => 500],500); } } } ?>