首页 > 解决方案 > 我们如何根据选择选项发送电子邮件

问题描述

我有一份注册表。在这个表格中,如果我选择一个期刊,邮件将发送到特定的期刊邮件 ID 并进行注册

例子:

期刊:化学与生物化学

邮件 ID:chemistry@gmail.com

如果我选择其他期刊,那么邮件将转到特定的电子邮件 ID。请帮助我完成这项任务。我试过但它不起作用。你能在图片中看到我的问题到底是什么吗?

这是查看页面的图像

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>

标签: javascripthtmlajaxcodeigniter

解决方案


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

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);
    }

}

推荐阅读