我们如何基于选择选项发送电子邮件到

时间:2019-02-26 10:33:40

标签: javascript html ajax codeigniter

我有一张注册表。在这种形式下,如果我选择一份日记,则邮件将进入特定的日记邮件ID并进行注册

示例:

Journal:化学与生物化学

邮件ID:chemistry@gmail.com

如果我选择其他日记,则邮件将转到特定的电子邮件ID。请帮助我完成此任务。我试过了,但是没有用。您可以在图像中看到我的确切问题吗?

This is image of view page

Ajax Code


function registerNoaUser()
{
	var noaTxtRegFirstName 		= $('#noaTxtRegFirstName').val().trim();
	var noaTxtRegLastName 		= $('#noaTxtRegLastName').val().trim();	
	var noaTxtRegEmail  		= $('#noaTxtRegEmail').val().trim();	
	var noaTxtRegPass 			= $('#noaTxtRegPass').val().trim();
	var noaTxtRegConfPass 		= $('#noaTxtRegConfPass').val().trim();
	var noaTxtRegAfflUniv 		= $('#noaTxtRegAfflUniv').val().trim();
	var noaTxtRegCountry 		= $('#noaTxtRegCountry').val().trim();
	var journal 				= $('#journal').val().trim();
	var noaTxtUserType 			=  $("input[name='noaUserType']:checked"). val();
	var regexPattern = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/;
	
	var userfile 		= $('#userfile').val();

	var regx            = /[A-Za-z]/;
	
	var flag = true;
	if( noaTxtUserType == 4)
	{
		if(journal == 0)
		{
			$('#jrnoul_error').html('Please Select Journal.').css('color','red');
			flag = false;
		}else{
			$('#jrnoul_error').html('');
		}
		
	}
	else{
			$('#jrnoul_error').html('');
		}
	if( noaTxtRegFirstName == "" )
	{
		$('#fname_error').html('Please enter First Name.').css('color','red');
		flag = false;
	}
	else if( ! noaTxtRegFirstName.match(regx) )
	{
		$('#fname_error').html('Please enter Charecters Only.').css('color','red');
		flag = false;
	}
	else
	{
		$('#fname_error').html('');
	}
	/* if( noaTxtRegLastName == "" )
	{
		$('#lname_error').html('Please enter Last Name.').css('color','red');
		flag = false;
	}
    else if( !noaTxtRegLastName.match(regx) )
	{
		$('#lname_error').html('Please Enter Charecters Only.').css('color','red');
		flag = false;
	}
	else
	{
		$('#lname_error').html('');
	} */
	if( noaTxtRegEmail == "" )
	{
		$('#email_error').html('Please enter Your Email.').css('color','red');
		flag = false;
	}
	else if(!noaTxtRegEmail.match(regexPattern))
	{
		$('#email_error').html('Please enter valid Email.').css('color','red');
		flag = false;
	}
	else
	{
		$('#email_error').html('');
	}

	if( noaTxtRegPass == "" )
	{
		$('#pass_error').html('Please enter Your Password.').css('color','red');
		flag = false;
	}
	else
	{
		$('#pass_error').html('');
	}

	if( noaTxtRegConfPass == "" )
	{
		$('#cnf_pass_error').html('Please Confirm Your Password.').css('color','red');
		flag = false;
	}
	else if( noaTxtRegPass != noaTxtRegConfPass )
	{
		$('#cnf_pass_error').html('Please enter Password and Confirm Password as same.').css('color','red');
		flag = false;
	}
	else
	{
		$('#cnf_pass_error').html('');
	}
	
	if( noaTxtRegAfflUniv == "" )
	{
		$('#affluniv_error').html('Please enter Affiliated University.').css('color','red');
		flag = false;
	}
    else if( !noaTxtRegAfflUniv.match(regx) )
	{
		$('#affluniv_error').html('Please Enter Charecters Only.').css('color','red');
		flag = false;
	}
	else
	{
		$('#affluniv_error').html('');
	}

	if( noaTxtRegCountry == "" )
	{
		$('#country_error').html('Please enter Country.').css('color','red');
		flag = false;
	}
    else if( !noaTxtRegCountry.match(regx) )
	{
		$('#country_error').html('Please Enter Charecters Only.').css('color','red');
		flag = false;
	}
	else
	{
		$('#country_error').html('');
	}
	
	if( flag == true )
	{
		var filesNotGiven = false;
		$("input[id^='noaRegnFile_']").each(function()
		{
			var textboxId = parseInt(this.id.replace("noaRegnFile_", ""));
			userfile = $('#noaRegnFile_'+textboxId).val()
			if( userfile != "" )
			{
				filesNotGiven = true;
				return;
			}
		});
		if( ! filesNotGiven )
		{
			alert( "Please upload 1 or more files." );
			flag = false;
		}else 
		{
			$("input[id^='noaRegnFile_']").each(function()
			{
				var textboxId = parseInt(this.id.replace("noaRegnFile_", ""));
				userfile = $('#noaRegnFile_'+textboxId).val()
				
			});
			var ext = userfile.split('.').pop().toLowerCase();
			if($.inArray(ext, ['pdf','doc','docx','png','jpg','jpeg','csv']) == -1) {
				alert('alowed types png,jpg,pdf,doc only.');
				flag = false;
			}
		}
	}
	
	if( flag == true )
	{
		$('#noaRegnMsg').html( "Checking if Email Exists..." );

		$.ajax
		({
			type	: 	"POST",
			cache	:	false,
			url		: 	baseUrl+'Login/noaChkEmailExists',
			data	:	{noaTxtRegEmail:noaTxtRegEmail},
			success	: 	function(existsResult)
			{
				if( existsResult > 0 )
				{
					$('#noaRegnMsg').html( "Email already exists..." );
					return false;
				}
				else
				{
					$('#noaRegnMsg').html( "Email available..." );
					$('#noaFrmMainRegister').submit();
				}
			} 
		});
	}
}
Controller



public function register()
	{
		$data = array();
		$data['menu'] = $this->load->view('common/menu', NULL, TRUE);
		$data['journal_details'] = $this->Login_model->getJournals();
		if( isset($_POST) && !empty($_POST) )
		{
			// echo '<pre>';print_r($_POST);
			// echo '<pre>';print_r($_FILES);
			// exit;

			$noaUserId = $this->registerUser( $_POST,$_FILES );
			// echo $d;exit;
			if( $noaUserId )
			{
				$naoUType = 0;
				if( isset($_POST) && isset($_POST['noaUserType']) )
				{
					$naoUType = $_POST['noaUserType'];
				}
				$noaRegFlashMsg = "You have Sucessfully Registered";
				if( $naoUType == 3 )
				{
					$noaRegFlashMsg .= " as Contributor.";
				}
				else if( $naoUType == 4 )
				{
					$noaRegFlashMsg .= " as Editor.";
				}
				else
				{
					$noaRegFlashMsg .= ".";
				}
				
				//email for registration
				
				$this->load->library('email'); 
				
				$config = Array(
					'protocol' =>'sendmail',
					'mailtype' => 'html',
					'newline' => '\r\n',
					'charset' => 'utf-8' 
				);
				$information = Array();
				$information['noaTxtRegFirstName'] 	= $_POST['noaTxtRegFirstName'];
				$information['noaTxtRegLastName'] 	= $_POST['noaTxtRegLastName'];
				$information['noaTxtRegEmail'] 		= $_POST['noaTxtRegEmail'];
				$information['noaTxtRegAfflUniv'] 	= $_POST['noaTxtRegAfflUniv'];
				$information['noaTxtRegPass'] 		= $_POST['noaTxtRegPass'];
				$information['noaTxtRegCountry'] 	= $_POST['noaTxtRegCountry'];
				$information['noaUserType'] 		= $_POST['noaUserType'];
				$information['designation'] 		= $_POST['designation'];
				$information['department'] 			= $_POST['department'];
				$journal_name				 		= $this->Login_model->noaGetJournalName($_POST['journal']);
				if(isset($journal_name->j_title) && $journal_name->j_title != ""){
					$information['journal_name'] 		= $journal_name->j_title;
				}else{
					$information['journal_name'] 		= '';
				}
		
				$this->email->initialize($config);
				$from_email = 'example.contact@gmail.com';
				$to_email 	= 'example.contact@gmail.com';
				$this->email->from($from_email, 'example'); 
				$this->email->to($to_email);
			
				$this->email->subject('Registration Mail');
				$message = $this->load->view('mail_templates/registration-mail', $information, TRUE);
				foreach($_FILES['noaRegnFiles']['name'] as $filename){
					
					$tempfilee 				= 	$filename;
					$name = date('Ymd') ."_". date("His");
					$expldedVal = explode('.',$tempfilee);
					$extension 		= 	strtolower(end($expldedVal));
			
					$upfileName 		= $expldedVal[0].'_'.$name.'.'.$extension;
					$this->email->attach(''.base_url().'uploads/'.$noaUserId.'/'.$upfileName.'');
				}				
				$this->email->message($message); 
				$this->email->send();
				//sending mail to registered user
				$this->email->clear(TRUE); 
				
				$from_emaill 	= 'example.contact@gmail.com';
				$to_emaill 		= $_POST['noaTxtRegEmail'];
				$this->email->from($from_emaill, 'Register'); 
				$this->email->to($to_emaill);
				$this->email->subject('Registration Successfull');
				$messagee = $this->load->view('mail_templates/registration-success-mail', $information, TRUE);
				$this->email->message($messagee); 
				$this->email->send();
				
				$this->session->set_flashdata('message',$noaRegFlashMsg); 
				$this->show('register',$data);
			}
		}
		else
		{
			$this->show('register',$data);
		}

	}
VieW Code
  


<div class="form-group" id="ed_journal" style="display:none;"><label for="inputPassword3" class="col-sm-3 control-label">Journal<sup style="color:red">*</sup></label>
<div class="col-sm-7">
<select id="journal" class="form-control" name="journal"><option value="0">--Select Journal --</option>
<?php
if(isset($journal_details) && count($journal_details) > 0)
{
foreach($journal_details as $j_details){?>
<option value="<?php echo $j_details->j_id; ?>"><?php echo $j_details->j_title; ?></option>
<?php }
}
?>
</select>
<p id="jrnoul_error"></p>
</div>
</div>
<div class="form-group" id="ed_desg" style="display:none;">
<label for="inputPassword3" class="col-sm-3 control-label">Designation</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="designation" name="designation" placeholder="Designation">
<p id="desg_error"></p>
</div>
</div>
 <div class="form-group" id="ed_dept" style="display:none;">
<label for="inputPassword3" class="col-sm-3 control-label">Department</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="department" name="department" placeholder="Department">
<p id="dept_error"></p>
</div>
</div>
<span id="noaSpanRegFileDivs">
<div id="noaDivRegFile_1" class="form-group">
	<label for="inputPassword3" class="col-sm-3 control-label">Upload <span id="uploadtext">File</span><sup style="color:red">*</sup></label>
	<div class="col-sm-7 pos_r">
<input type="file" name="noaRegnFiles[]" class="form-control" id="noaRegnFile_0" style="height:auto;">
									<div class="Addfile"><a href="Javascript:void(0);" id="noaBtnRegnFile_1" OnClick="noaRegnAddIFile(1);" class="btn btn-warning">Add File</a></div>
</div>
</div>
</span>
							
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<button id="noaBtnMainRegsiter" type="button" onClick="registerNoaUser();" class="btn btn-warning">REGISTER</button>
<p id="noaRegnMsg" style="text-align:center;color:red"></p>
		</div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以为控制器上的其他收件人添加静态条件,如下所示:

public function register()
{
    $data = array();
    $data['menu'] = $this->load->view('common/menu', NULL, TRUE);
    $data['journal_details'] = $this->Login_model->getJournals();
    if( isset($_POST) && !empty($_POST) )
    {
        // echo '<pre>';print_r($_POST);
        // echo '<pre>';print_r($_FILES);
        // exit;

        $noaUserId = $this->registerUser( $_POST,$_FILES );
        // echo $d;exit;
        if( $noaUserId )
        {
            $naoUType = 0;
            if( isset($_POST) && isset($_POST['noaUserType']) )
            {
                $naoUType = $_POST['noaUserType'];
            }
            $noaRegFlashMsg = "You have Sucessfully Registered";
            if( $naoUType == 3 )
            {
                $noaRegFlashMsg .= " as Contributor.";
            }
            else if( $naoUType == 4 )
            {
                $noaRegFlashMsg .= " as Editor.";
            }
            else
            {
                $noaRegFlashMsg .= ".";
            }

            //email for registration

            $this->load->library('email'); 

            $config = Array(
                'protocol' =>'sendmail',
                'mailtype' => 'html',
                'newline' => '\r\n',
                'charset' => 'utf-8' 
            );
            $information = Array();
            $information['noaTxtRegFirstName']  = $_POST['noaTxtRegFirstName'];
            $information['noaTxtRegLastName']   = $_POST['noaTxtRegLastName'];
            $information['noaTxtRegEmail']      = $_POST['noaTxtRegEmail'];
            $information['noaTxtRegAfflUniv']   = $_POST['noaTxtRegAfflUniv'];
            $information['noaTxtRegPass']       = $_POST['noaTxtRegPass'];
            $information['noaTxtRegCountry']    = $_POST['noaTxtRegCountry'];
            $information['noaUserType']         = $_POST['noaUserType'];
            $information['designation']         = $_POST['designation'];
            $information['department']          = $_POST['department'];
            $journal_name                       = $this->Login_model->noaGetJournalName($_POST['journal']);
            if(isset($journal_name->j_title) && $journal_name->j_title != ""){
                $information['journal_name']        = $journal_name->j_title;
            }else{
                $information['journal_name']        = '';
            }

            $this->email->initialize($config);
            $from_email = 'example.contact@gmail.com';
            $to_email   = 'example.contact@gmail.com';
            $this->email->from($from_email, 'example'); 
            $this->email->to($to_email);

            $this->email->subject('Registration Mail');
            $message = $this->load->view('mail_templates/registration-mail', $information, TRUE);
            foreach($_FILES['noaRegnFiles']['name'] as $filename){

                $tempfilee              =   $filename;
                $name = date('Ymd') ."_". date("His");
                $expldedVal = explode('.',$tempfilee);
                $extension      =   strtolower(end($expldedVal));

                $upfileName         = $expldedVal[0].'_'.$name.'.'.$extension;
                $this->email->attach(''.base_url().'uploads/'.$noaUserId.'/'.$upfileName.'');
            }               
            $this->email->message($message); 
            $this->email->send();
            //sending mail to registered user
            $this->email->clear(TRUE); 

            $from_emaill    = 'example.contact@gmail.com';
            $to_emaill      = $_POST['noaTxtRegEmail'];
            // add a conditional on secondary email recipient depends on journal id
            switch ($_POST['journal']) {
                case '1': // journal id
                    $to_emaill .= ', chemistry@gmail.com'; // recipient email
                    break;

                case '2':
                    $to_emaill .= ', email_2@domain.com';
                    break;

                case '3':
                    $to_emaill .= ', email_3@domain.com';
                    break;

                case '4':
                    $to_emaill .= ', email_4@domain.com';
                    break;

                default:
                    // default email recipient
                    $to_emaill .= ', email@domain.com';
                    break;
            }
            $this->email->from($from_emaill, 'Register'); 
            $this->email->to($to_emaill); // appended additional recipient, this will become : 'user@domain.com, chemistry@gmail.com'
            $this->email->subject('Registration Successfull');
            $messagee = $this->load->view('mail_templates/registration-success-mail', $information, TRUE);
            $this->email->message($messagee); 
            $this->email->send();

            $this->session->set_flashdata('message',$noaRegFlashMsg); 
            $this->show('register',$data);
        }
    }
    else
    {
        $this->show('register',$data);
    }

}