Jump to content

PHPMailer


rocky48

Recommended Posts

I have been altering one of the phpmailer examples to include getting send to addresses from a database, which works OK.

 

I now want to add Subject, message body and attachment via an html form.

 

I added print_r($_POST) to check that the POST variables were being sent OK.

 

I get the following display:

 

 

Array ( [subject] => Monday Test [message] => $mail->MsgHTML($body); [attachment1] => attachment.zip ) connected to database?Mailer Error: Message body empty

 

Instead of the contents of $_ POST['message'] being displayed it is displaying the $mail variable that is being passed to the phpmailer code.

 

I guess that I have something wrong with the way that I have coded the $mail->MsgHTML($body), but I am not experienced enough to know what is wrong.

 

Can anyone experienced with phpmailer help?

 

Here is the php script:

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

<?php
print_r($_POST);
echo $_POST['Message'];
echo $_POST['Attachment1'];
echo $_POST['Subject'];
//error_reporting(E_ALL);
//error_reporting(E_STRICT);

date_default_timezone_set('Europe/London');

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'];
$attachment			 = $_POST['Attachment1'];

$mail->MsgHTML($body);


$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->AddAddress($row['email']);
				$mail->SetFrom('administrator@1066cards4u.co.uk', 'Tony Hudson');
                $mail->AddAttachment($attachment);      // 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>

Thank you!

Link to comment
Share on other sites

Hi Jazzman

 

I am getting no error with the backslash in front of the phpmailer class name.

 

Here is the form:

<!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>Attachment:</strong><br/>
	<input type="text" name="attachment1" size="50"><br/>
	<p><input type="submit" value="Send It"></p>
	</form>
	</body>
	</html>
  </body>
</html>

I have commented out the echo's for the POST's and it now errors as the following:

 

Array ( [subject] => Tuesday Test [message] => This is a test using a database and attachment [attachment1] => attachment.zip ) connected to database?Mailer Error: Message body empty

So I conclude that the POST is working OK.

 

So it must be a problem with the $mail variables!

Also I have moved the line : $mail->MsgHTML($body); to within the loop (where it was originally).

 

So why is it not picking up the values to insert into the phpmailer script?

 

Have I got the syntax wrong?

Link to comment
Share on other sites

Hi Jazzman

 

I have just thought of another tactic!

 

What if I put the variables into the MySQL database, as the other values were passed OK before I tried to introduce the POSTed values.

 

I could then write an html script to add the information to the database.

Link to comment
Share on other sites

Hi Jazzman

 

While you were away I pursued my latest idea viz sending info to DB.

 

Had more success, but found that no subject was sent.

Here is the modified code:

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

<?php
print_r($_POST);
//echo $_POST['Message'];
//echo $_POST['Attachment1'];
//echo $_POST['Subject'];
//error_reporting(E_ALL);
//error_reporting(E_STRICT);

date_default_timezone_set('Europe/London');

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'];
//$attachment	= $_POST['Attachment1'];

//echo $body;
//echo $attachment;

$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? \n";
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->AddAddress($row['email']);
		$mail->SetFrom('administrator@1066cards4u.co.uk', 'Tony Hudson');
                
		}
$sqlmail = "SELECT subtxt, message, attachment FROM mail_info WHERE EID= '".$_POST['Eno']."'";
		$mailresult = mysqli_query($mysqli, $sqlmail) or die(mysqli_error($mysqli));
		while ($rowm = mysqli_fetch_array($mailresult)) {
		$mail->Subject($rowm['subtxt']);
		$mail->MsgHTML($rowm['message']);
		$mail->AddAttachment($rowm['attachment']);      // 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>

I commented out all the lines I am not using now!

 

When I ran the code after this, I got a fatal error:

 

Fatal error: Call to undefined method PHPMailer::Subject() in C:\websites\Test\My_db2_Mailer.php on line 60

What does this mean?

 

I am using phpmailer ver 5.2.4.

Link to comment
Share on other sites

It should work just fine, but I get to work to work right now. I'll check it a bit late.

 

PS. Do some research on the web for examples, how to use phpmailer with looping constructs. I'm on SwiftMailer and not entirely sure if this sown above is correct.

Edited by jazzman1
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.