Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Error AddAddress?

2014-04-20
2014-04-21
  • Tony Hudson
    Tony Hudson
    2014-04-20

    I get the following error when parsing the code listed below:
    Fatal error: Call to a member function AddAddress() on a non-object in /homepages/43/d344817611/htdocs/subscription.php on line 86
    CODE
    require_once('class.phpmailer.php');
    $mail->AddAddress('administrator@1066cards4u.co.uk', 'Tony Hudson');
    $mail->Subject= 'New subscribe/unsubscribe';
    $mbody='A new member has subscribed/unsubscribed.';
    $mail->Body($mbody);
    $mail->Send();

    I am not too familar with PHPmailer, but looking at the code I can't see where I am going wrong.

    Ultimately I would like to add some data that is generated by a form POST and is stored in a SQL array, but I am not sure how to do that either.

    Can anybody help me?

     
    • asharris
      asharris
      2014-04-20

      You need to instantiate the object:

      require_once('class.phpmailer.php');
      $mail = new PHPMailer();
      $mail->AddAddress('administrator@1066cards4u.co.uk', 'Tony Hudson');
      $mail->Subject= 'New subscribe/unsubscribe';
      $mbody='A new member has subscribed/unsubscribed.';
      $mail->Body($mbody);
      $mail->Send();

      Antony

      On 20/04/2014 18:14, Tony Hudson wrote:

      I get the following error when parsing the code listed below:
      Fatal error: Call to a member function AddAddress() on a non-object in
      /homepages/43/d344817611/htdocs/subscription.php on line 86
      CODE
      require_once('class.phpmailer.php');
      $mail->AddAddress('administrator@1066cards4u.co.uk', 'Tony Hudson');
      $mail->Subject= 'New subscribe/unsubscribe';
      $mbody='A new member has subscribed/unsubscribed.';
      $mail->Body($mbody);
      $mail->Send();

      I am not too familar with PHPmailer, but looking at the code I can't
      see where I am going wrong.

      Ultimately I would like to add some data that is generated by a form
      POST and is stored in a SQL array, but I am not sure how to do that
      either.

      Can anybody help me?


      Error AddAddress?
      https://sourceforge.net/p/phpmailer/discussion/130418/thread/55204a55/?limit=25#b050


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/phpmailer/discussion/130418/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
      Attachments
  • Marcus Bointon
    Marcus Bointon
    2014-04-20

    Antony, thanks for answering.

    Tony, please don't use this sourceforge project any more - the code here is old, buggy and no longer maintained. Go to the GitHub project instead: https://Github.com/PHPMailer/PHPMailer

     
  • Tony Hudson
    Tony Hudson
    2014-04-21

    Hi Marcus/Antony

    Thank you for your help.

    I have downloaded the new version as you suggested and I see you now have to also have PHPMailerAutoload to work. However it now says:
    Fatal error: Call to undefined method PHPMailer::Body() in /homepages/43/d344817611/htdocs/subscription.php on line 90.

    Why is it saying that as I can see that $Body is a method in the class file?

     
    Last edit: Tony Hudson 2014-04-21
  • Tony Hudson
    Tony Hudson
    2014-04-21

    Hi Margus/Antony

    Just spotted the mistake!
    Missed the equals sign after Body.

    Any ideas on how I can add POSTed or SQL variables into the email body?

    Here is the whole code:
    <?php
    include("connect_mailer.php");

    //print_r($_POST);
    require_once ('recaptchalib.php');
    $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $resp = recaptcha_check_answer ($privatekey,
    $_SERVER["REMOTE_ADDR"],
    $_POST["recaptcha_challenge_field"],
    $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
    "(reCAPTCHA said: " . $resp->error . ")");
    } else {
    if (($_POST) && ($_POST["action"] == "sub")) {
    //trying to subscribe; validate email address
    if ($_POST["email"] == "") {
    header("Location: subscription.php");
    exit;
    } else {
    //connect to database
    doDB5();

    //check that email is in list
    emailChecker($_POST["email"]);

    //get number of results and do action
    if (mysqli_num_rows($check_res) < 1) {
    //free result
    mysqli_free_result($check_res);

    //add record
    $add_sql = "INSERT INTO subscribers (email, Forename, Surname, Country, Gender) VALUES('".$_POST["email"]."', '".$_POST["Forename"]."', '".$_POST["Surname"]."', '".$_POST["Country"]."', '".$_POST["Gender"]."')";
    $add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));
    $display_block = "

    Thanks for signing up!

    ";

    //close connection to MySQL
    mysqli_close($mysqli);
    } else {
    //print failure message
    $display_block = "

    You're already subscribed!










    ";
    }
    }
    } else if (($_POST) && ($_POST["action"] == "unsub")) {
    //trying to unsubscribe; validate email address
    if ($_POST["email"] == "") {
    header("Location: subscription.php");
    exit;
    } else {
    //connect to database
    doDB5();

    //check that email is in list
    emailChecker($_POST["email"]);

    //get number of results and do action
    if (mysqli_num_rows($check_res) < 1) {
    //free result
    mysqli_free_result($check_res);

    //print failure message
    $display_block = "

    Couldn't find your address!









    No action was taken.

    ";
    } else {
    //get value of ID from result
    while ($row = mysqli_fetch_array($check_res)) {
    $id = $row["id"];
    }

    //unsubscribe the address
    $del_sql = "DELETE FROM subscribers WHERE id = '".$id."'";
    $del_res = mysqli_query($mysqli, $del_sql) or die(mysqli_error($mysqli));
    $display_block = "

    <P>You're unsubscribed!








    ";
    }
    }
    }
    }
    require_once('class.phpmailer.php');
    //$subsemail= $row->email;
    //$subsFname = $row->Forename;
    //$subsSname = $row->Surname;
    //$where = $row->Country;
    //$gender = $row->Gender;
    $mail = new PHPMailer();
    $mail->AddAddress('administrator@1066cards4u.co.uk', 'Tony Hudson');
    $mail->Subject= 'New subscribe/unsubscribe';
    $mbody='A new member has subscribed/unsubscribed.';
    $mail->Body=($mbody);
    $mail->Send();

    mysqli_close($mysqli);
    ?>

    As you can see I have commented out the new variables that I created to extract the details from the the MySQL database.

     
    Last edit: Marcus Bointon 2014-04-21
  • Marcus Bointon
    Marcus Bointon
    2014-04-21

    You can put anything you like in the message body. Just build it as a string and then assign it to the Body property of PHPMailer.

    PHPMailer contains a good email address validator - I suggest you use it.

    From some of your comments I think you could do with reading the docs on basic PHP OO syntax.

    You should also be aware that because you are not filtering your incoming data before inserting into SQL queries, your code is vulnerable to SQL injection.