0

I am trying to make a mailing system where by the user can select a specific group of people to send emails. What I am currently facing now is that I couldn't send out emails.

I had tried $row['email'], but it will show me error message like

Fatal error: Cannot use object of type stdClass as array

I has also tried $row->$email, no errors found but no email was to send out.

NOTE: I would prefer using Gmail SMTP because I did not have hosting server set up, and I do not have the time to do that anymore. Please kindly advise.

From doMailingSystem.php,

<?php
include 'dbFunctions.php';
include "mailingSystem.php";

require_once('class.phpmailer.php');
require_once('PHPMailerAutoload.php');

extract($_POST);
$session_id = date('Y-m-d', strtotime($session_id));
$sql = "SELECT student_profile.email FROM student_profile, booking_history WHERE student_profile.student_id = booking_history.student_id and booking_history.booking_date_session LIKE '$session_id%' ";
//print $sql;
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_object($result)) {

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = "UTF-8";
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
    $mail->SMTPAuth = true;

    $mail->From = '[email protected]';
    $mail->FromName = 'X Team';
    $mail->AddAddress($row['email']);

    $mail->IsHTML(true);
    $mail->Subject = $_POST['subject'];
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
    $mail->Body = $_POST['message'];
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <p>
<?php
// echo $statusMessage;
?>
        </p>
    </body>
</html>
3
  • You should be using ->property for types of StdClass. ['index'] is for arrays. Commented Jan 19, 2015 at 18:37
  • 2
    Oh man: extract($_POST);. Words can't describe how bad an idea that is. Commented Jan 19, 2015 at 18:39
  • You really should try starting with sane code rather than sticking random bits together and hoping it works. Commented Jan 19, 2015 at 19:00

2 Answers 2

1

You forgot to send you message:

...
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $_POST['message'];
$mail->send();
^^^^^^^^^^^^^ Here

And you might want to check that the e-mail address is what you expect it to be.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @jeroen, thank you for your help! I must have left that out when I copied and pasted from another file.
1

Use mysqli_fetch_assoc() instead of mysqli_fetch_object() in your while statement. Then you can use it as array like $row['email'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.