PHP mailer Checkbox to select Recipients and add subject / message / attachment

I have these two php pages to select a recipient with a checkbox, add a subject,message and attach a file.
I cannot pass the subject ,message(called info) and attachment to the page with the phpmailer in it.
Without trying to pass these variables, it sends out to recipients as selected.

To summarize: I cannot pass the subject,info and attach values to the "send_mail2.php page, in order to send out with the selected recipients. Please be so kind to assist

// indexmes2.php
$query = "SELECT * FROM customer ORDER BY customer_id";
$statement = $con->prepare($query);
$statement->execute();
$result = $statement->fetchAll();

?>
<!DOCTYPE html>
<html>
	<head>
		<title>Send Emails </title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
	<body>
	
		<div class="container">
			<div class="row">
				<div class="col-md-8" style="margin:0 auto; float:none;">
						<h3 align="center"><u>Send Emails to Entrants</h3></u>
					
				
					<form method="post" enctype="multipart/form-data">
						<div class="form-group">
					
						<div class="form-group">
							<label>Enter Subject</label>
							<input type="text" name="subject" id="subject" class="form-control" placeholder="Enter Subject"  />
						</div>
						<div class="form-group">
							<label>Enter Message</label>
							<textarea name="info" id="info" class="form-control" placeholder="Enter info"></textarea>
						</div>
						    <div class="form-group">
					<label>Select a file</label>
					<input type="file" name="attach" name="attach" accept=".doc,.docx, .pdf"  />
					</div>
						
						
					</form>
				</div>
			</div>
		</div>
	
	
	<button id="button"> but </button>
<div id="result"></div>
	
	
	
		<br />
		<div class="container">
			
			<br />
			<div class="table-responsive">
				<table class="table table-bordered table-striped">
					<tr>
						<th>Customer Name</th>
						<th>Email</th>
						<th>Select</th>
					</tr>
				<?php
				$count = 0;
				foreach($result as $row)
				{
					$count = $count + 1;
					echo '
					<tr>
						<td>'.$row["customer_name"].'</td>
						<td>'.$row["customer_email"].'</td>
					
						<td>
						<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'"   data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Send Single Mail</button>
						</td>
					</tr>
					';
				}
				?>
					<tr>
						<td colspan="3"></td>
					</tr>
				</table>
			</div>
		</div>
	</body>
</html>

<script>
	$(document).ready(function(){
		 $(".email_button").click(function() {	
			var subject = $("#subject").val();
			var info = $("#info").val();
			var attach = $("#attach").val();
		$.ajax({
			method: "post",
			url: "send_mail2.php",
			data: {subject:subject,info:info,attach:attach}

			})	
			 });
			 })
</script>

<script>
$(document).ready(function(){
	$('.email_button').click(function(){
		
		$(this).attr('disabled', 'disabled');
		var id  = $(this).attr("id");
		var action = $(this).data("action");
		var email_data = [];
		
		if(action == 'single')
		{
			email_data.push({
				email: $(this).data("email"),
				name: $(this).data("name")
			
			});
		}
		else
		{
			$('.single_select').each(function(){
				if($(this).prop("checked") == true)
				{
					email_data.push({
						email: $(this).data("email"),
						name: $(this).data("name")
					
					});
				} 
			});
		}

		$.ajax({
			url:"send_mail2.php",
			method:"POST",
			data:{email_data:email_data},
			beforeSend:function(){
				$('#'+id).html('Sending...');
				$('#'+id).addClass('btn-danger');
			},
			success:function(data){
				if(data == 'ok')
				{
					$('#'+id).text('Success');
					$('#'+id).removeClass('btn-danger');
					$('#'+id).removeClass('btn-info');
					$('#'+id).addClass('btn-success');
				}
				else
				{
					$('#'+id).text(data);
				}
				$('#'+id).attr('disabled', false);
			}
		})

	});
});
</script>

[php]
//send_mail2.php
$con = new PDO(“mysql:host=localhost;xxxxxxxxxxx”);

if(isset($_POST[‘email_data’]))
{
$subject = $_POST[‘subject’];
$info = $_POST[‘info’];
$path = $_POST[‘attach’];

$output = '';

$path = 'upload/' . $_FILES["attach"]["name"];
move_uploaded_file($_FILES["attach"]["tmp_name"], $path);

foreach($_POST['email_data'] as $row)
{
	require 'class/class.phpmailer.php';
	$mail = new PHPMailer;
	$mail->IsSMTP();								//Sets Mailer to send message using SMTP
	$mail->Host = 'xxxxxx';		//Sets the SMTP hosts of your Email hosting, this for Godaddy
	$mail->Port = '25';								//Sets the default SMTP server port
	$mail->SMTPAuth = true;							//Sets SMTP authentication. Utilizes the Username and Password variables
	$mail->Username = 'xxxxxxxx';					//Sets SMTP username
	$mail->Password = 'xxxxxx!';					//Sets SMTP password
	$mail->SMTPSecure = '';							//Sets connection prefix. Options are "", "ssl" or "tls"
	$mail->FromName = 'Admin';	
	$mail->From = 'xxxxx';			//Sets the From email address for the message
	$mail->AddAddress('xxxxxx', 'MSEM');
	$mail->AddAddress($row["email"], $row["name"]);	//Adds a "To" address
	$mail->WordWrap = 50;							//Sets word wrapping on the body of the message to a given number of characters
	$mail->IsHTML(true);
	$mail->Subject($subject); //Sets the Subject of the message
    $mail->AddAttachment($path); 
	$mail->Body($info);
	$mail->AltBody = '';

	$result = $mail->Send();   //Send an Email. Return true on success or false on error

	{
		  $message = '<div class="alert alert-success">Info Successfully Submitted</div>';
		  unlink($path);
		 }
		 else
		 {
		  $message = '<div class="alert alert-danger">There is an Error</div>';
		 }}}

[/php]

Table

CREATE TABLE `customer` ( `customer_id` int(11) NOT NULL, `customer_name` varchar(300) NOT NULL, `customer_email` varchar(300) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Your current code isn’t actually uploading anything and it’s making multiple http requests for the data that it is submitting, so the subject, message, and filename are in one http request and the email data is in a different http request…

I recommend that you start by logging the actual data that is being submitted so that you can tell what the code is doing. Add the following line to the start of the php code in send_mail2.php -

[php]file_put_contents(‘log.txt’,“Post:”.print_r($_POST,true).“Files:”.print_r($_FILES,true)."--------\n",FILE_APPEND);[/php]

Next, in order to upload files using ajax, you need a handful of code (there are many examples posted on the web). I recommend that you start by forgetting ajax and just get a html form and the form processing code to work.

Thx for reply. I was away for a few days, and will tomorrow try out your suggestions

Sponsor our Newsletter | Privacy Policy | Terms of Service