Jump to content

Problem with POST


rocky48
Go to solution Solved by Ch0cu3r,

Recommended Posts

I am fairly new to programming and have been trying to modify an example program supplied by phpmailer.

 

I have succesfully added the connect to a database, but when I modified the code to allow the user to modify the content fro an HTML form it does not work.

 

When run it displays the echo connected to database but nothing else happens.  It should display the emails sent echo.

 

I am unsure if I have got the correct syntax for the POST entries.

 

Any help would be useful!

 

Here is the HTML code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="dcterms.created" content="Wed, 05 Feb 2014 16:30:04 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <title></title>
    <head>
	<title>Send a Newsletter</title>
	</head>
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
	<body>
	<h1>Send a Newsletter</h1>
	<form method="post" action="My_db_Mailer.php">
	<p><strong>Subject:</strong><br/>
	<input type="text" name="subject" size="30"></p>
	<p><strong>Mail Body:</strong><br/>
	<textarea name="message" cols="50" rows="10"></textarea>
	<p><strong>Attachments:</strong><br/>
	<input type="text" name="attachment1" size="50"><br/>
	<p><input type="submit" value="Send It"></p>
	</form>
	</body>
	</html>
  </body>
</html>

And here is the PHP code:

<html>
<head>
<title>PHPMailer - MySQL Database - SMTP basic test with authentication</title>
</head>
<body>

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('\class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail                = new PHPMailer();

$body                = '".$_POST["Message"]."';

$mail->SetFrom('webmaster@1066cards4u.co.uk', 'List manager');
$mail->AddReplyTo('webmaster@1066cards4u.co.uk', 'List manager');

$mail->Subject = '".$_POST["Subject"]."';

include("connect_mailer.php");
doDB5();

echo "connected to database?";
if (mysqli_connect_errno()) {
		//if connection fails, stop script execution
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	} else {
		//otherwise, get emails from subscribers list
		$sql = "SELECT email FROM subscribers";
		$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

		//create a From: mailheader
		$mailheaders = "From: webmaster@1066cards4u.co.uk>";

		//loop through results and send mail
		while ($row = mysqli_fetch_array($result)) {
			//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
                $mail->MsgHTML($body);
                $mail->AddAddress($row["email"]);
				$mail->SetFrom('administrator@1066cards4u.co.uk', 'Tony Hudson');
                $mail->AddAttachment('".$_POST["Attachment1"]."');      // attachment
		}

if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Messages sent!';
}
// Clear all addresses and attachments for next loop
  
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>

</body>
</html>
Link to comment
Share on other sites

Hi

Changed as suggested and also created a variable for attachment ($attachment), but still not working!

The lines I changed:

$attachment			 = $_POST['Attachment1'];

and

 $mail->AddAttachment($attachment);

Still does not display message sent echo!

 

Any other ideas?

Link to comment
Share on other sites

  • Solution

To add a file attachmment you'll need to use the file input field

<input type="file" name="attachment" />

Make sure you add   enctype="multipart/form-data" attribute to your <form> tag

<form method="post" action="My_db_Mailer.php" enctype="multipart/form-data">

Now use   $_FILES['attachment']   instead of    $_POST['attachment']

Example

$mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);      // attachment
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.