rocky48 Posted February 17, 2014 Share Posted February 17, 2014 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> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 17, 2014 Share Posted February 17, 2014 (edited) You are using wrapping quotes around the $_POST vars. Remove '". and ."' from around it . You should use it like $varname = $_POST['fieldname']; // or passing to a function function($_POST['fieldname']) Edited February 17, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
rocky48 Posted February 17, 2014 Author Share Posted February 17, 2014 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? Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted February 17, 2014 Solution Share Posted February 17, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.