#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Mailer Error () Message body empty


    Hi I am new to programming and was trying to figure out how to send newsletters. I learned the best way forward is using PHPmailer.

    Below is the code which I am trying and getting "Mailer Error () Message body empty". I also tried using $body = "this is text subject".. but didn't worked. I am using php version 5.4.3 and phpmailer version 5.2.4

    PHP Code:
    //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                file_get_contents("C:/WAMP/www/PHPMailer_5.2.4/examples/contents.html");
    $body                preg_replace('/[\]/','',$body);

    $mail->IsSMTP();        // telling the class to use SMTP
    $mail->Host          "ssl://smtp.gmail.com";
    $mail->SMTPAuth      true;                  // enable SMTP authentication
    $mail->SMTPKeepAlive true;                  // SMTP connection will not close after each email sent
    $mail->Host          "ssl://smtp.gmail.com"// sets the SMTP server
    $mail->Port          465;                    // set the SMTP port for the GMAIL server
    $mail->Username      "myemail@gmail.com"// SMTP account username
    $mail->Password      "password";        // SMTP account password
    //$mail->SetFrom    ('myemail@gmail.com');
    $mail-> From         "myemail@gmail.com";
    $mail->FromName      "myemail@gmail.com";
    $mail->AddReplyTo   ('myemail@gmail.com');

    $mail->Subject       "Test subject - newletter";

    @
    MYSQL_CONNECT("localhost","root","password");
    @
    mysql_select_db("newsletter"); //Databasename
    $query  "SELECT SrNo, emailid FROM test"//table and columns
    $result = @MYSQL_QUERY($query);

    while (
    $row mysql_fetch_array ($result)) {
    $mail->AltBody    "To view the message, please use an HTML compatible email viewer!"// optional, comment out and test
    $mail->IsHTML(true);
    $mail->MsgHTML($body);
    $mail->AddAddress($row["emailid"], $row["emailid"]);
    //$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

    if(!$mail->Send()) {
    echo 
    "Mailer Error (" str_replace("@""@"$row["emailid"]) . ') ' $mail->ErrorInfo '<br />';
    } else {
    echo 
    "Message sent to :" $row["emailid"] . ' (' str_replace("@""@"$row["emailid"]) . ')<br />';
    }
    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    //$mail->ClearAttachments();

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    First and foremost, if you are new to PHP stop what you are doing. Copying someone else's bad code from online sources will (did) get you into trouble. The MySQL extensions are deprecated. Don't use them. Learn PDO and switch to it and use prepared statements.

    Second, to your problem, if you echo '$body' right after you set it up, do you get what you expected? You need to read ManiacDan's New User Guide. It addresses various debugging techniques for problems like yours.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    Copying someone else's bad code from online sources will (did) get you into trouble. The MySQL extensions are deprecated.
    I haven't copied the code from any site.. I am using the code from examples given in PHPmailer version 5.2.4. As this is latest version of PHPmailer (released on 19th Feb 2013), I don't think it contains any deprecated functions.
  6. #4
  7. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,556
    Rep Power
    2338
    ptr2void's universal truth of PHP:

    Example and tutorial writers do the forum denizens of the world no favors by omitting error checking and handling, as clueless n00bs copy and paste their example code with no concept of this very important facet of programming, leaving the hapless volunteers in said forums to clean up the mess.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by ptr2void
    ptr2void's universal truth of PHP:

    Example and tutorial writers do the forum denizens of the world no favors by omitting error checking and handling, as clueless n00bs copy and paste their example code with no concept of this very important facet of programming, leaving the hapless volunteers in said forums to clean up the mess.
    I don't think you know about PHPmailer and that's why instead of solving the problem, you are blaming writers and talking like spoilt kid.

    In the meantime I have figured out the solution on my own...

    Comments on this post

    • ptr2void disagrees : My years of experience dating back to writing extensions for PHP 3 beg to differ with you.
    • gw1500se disagrees : MySQL extension are indeed deprecated whether the tutorial uses them or not. Reprogram it using PDO and then come back.

IMN logo majestic logo threadwatch logo seochat tools logo