Jump to content

Fatal error: Call to undefined method PHPMailer::Subject() in .......


Recommended Posts

I am getting the error shown in the title but can't see why it is erroring!

Here is the 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>

>

Anyone familiar with PHPmailer?

Please help?

So what is the method?

 

I am a relantive newbie, so I don't understand!

 

According mailer's docs the subject is a property as @mac said above.

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->Subject   // property 

$mail->Subject () // method

How many rows of data do you expect to retrieve, based on $_POST['Eno'] by using the following->

$sqlmail = "SELECT subtxt, message, attachment FROM mail_info WHERE EID= '".$_POST['Eno']."'";

while ($rowm = mysqli_fetch_array($mailresult)) {

} 

Hi Jazzman

 

I have 2 tables in my database: one where i will store the mail list subscribers and the other wich will store the message details (Subject, Message body and attachment).  I felt that this was the best way of getting the script to send the latest message.

 

Looking at my script and going on what you have said, surely $mail->Subject (rowm[subtxt]); is a method?

So why is it failing?

$mail->Subject (rowm[subtxt]) is a method!

 

Try,

 

$mail->Subject = rowm[subtxt];

 

 

So why is it failing?

 

Because you're trying to call a method named "Subject", but actually it doesn't exist in your library and the php parser returns fatal error message.

Hi Jazzman

 

Tried your suggestion:

No error, but email still does not have subject line!

Looked at another example and saw that it should have had an equals after "Subject", there it had a string inside single quotes.

The problem appears to be with the way I am writing the instruction to extract the field from the array.

 

I am not sure of the correct syntax so are you able to help?

Unfortunately, I'm not using phpmailer to provide an efficacious help. However, it leads me to believe that you haven't returned any rows from database. It's a bad practice also to send two different sql select statements using looping application's constructs to return data from 2 or more tables and columns. Use "JOIN" instead! Do "DESCRIBE" to provide information about those 2 tables and their columns.

Edited by jazzman1

Hi Jazzman

 

Firstly it is returning the other fields from the database table, but not the Subject field.

I have found the error!  A silly one the 'subtxt' should have been 'Subtxt'.

 

Regarding the database question, if I write it as one MySQL statement, because of the fact I am using a where statement in the mail_info table, what effect will it have on the subscribers table?  I other words will it loop through all the subscribers?

 

Thanks for your help!

 

Regarding the database question, if I write it as one MySQL statement, because of the fact I am using a where statement in the mail_info table, what effect will it have on the subscribers table?  I other words will it loop through all the subscribers?

 

Yes, everything will be the same except the performance. However, to provide a "JOIN" help, we need to know the structure of "subscribers" and "mail_info" tables.

Hi Jazzman

 

Here is the structure of the 2 tables:

 

subscribers

Field Type Null Default id  int(11) Yes  NULL email  varchar(150) Yes  NULL Forename  varchar(50) Yes  NULL Surname  varchar(50) Yes  NULL Country  varchar(50) Yes  NULL Gender  text Yes  NULL
 

 Indexes:

 

Keyname Type Cardinality Field PRIMARY PRIMARY 4 

id

 

mail_info

Field Type Null Default EID  int(11) Yes  NULL subtxt  text Yes  NULL message  longtext Yes  NULL attachment  text Yes  NULL
 

 Indexes:

Keyname Type Cardinality Field PRIMARY PRIMARY 0  EID

 

Your help is appreciated!

Edited by rocky48

Hi Jazzman

 

As it checks the email to see if it exists I guess that the answer is no, but I can't test this as I am in the middle of trying to add reCaptcha working.

The answer to qu. 2 is yes.

 

At the moment I am getting tied up in knots to make reCaptcha work.  As the form and the code to add the values to the are in the same file I am finding it difficult to get my form to be displayed.

 

The reCaptcha works OK, its dovetailing it into my code is the difficulty.

 

However that is another question. I have posted a different thread on this subject > (Since adding reCaptcha code not working)

Edited by rocky48

Then, no need to use a "JOIN" as I thought before, because there is no relationship between them. All you need to do is select the results of those two tables combaining them in one query.

// instead 

//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


// to be 

//otherwise, get emails from subscribers list
$eno = intval($_POST['Eno']);

$sql = "SELECT 
    s.email, m.subtxt, m.message, m.attachment
FROM
    subscribers s,
    mail_info m
WHERE
    EID = $eno";

$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->Subject = $row['subtxt'];
$mail->MsgHTML($rowm['message']);
$mail->AddAttachment($rowm['attachment']); // attachment
  • 2 weeks later...
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.