Jump to content

Sending a newsletter with Mail


travisco87

Recommended Posts

Hello all! I am trying to send mail using the "Mail.php" function but I cant figure out whats going wrong. The insert function works in the database but not the sending mail function. Please let point me in the right direction, thanks for your help in advance! 

<?php
    if(isset($_POST)){
//If the post is not empty, continue
        if(!empty($_POST)) {
            include_once 'includes.php';
            require_once 'Mail.php';
            $from = "Name <email@foobar.com>";
            $subject = $_POST['subject'];
            $text = $_POST['newsletterBody'];
            $host = "host";
            $port = "port";
            $username = "EMAILUSERNAME";
            $password = "EMAILPASSWORD";
            echo "Subject: " . $subject;
            echo "<br />Body: " . $text;
            echo "<br />Host: " . $host;
            echo "<br />Port: " . $port . "<br />";
            $sql = 'INSERT INTO newsletter_log (date,title,body) VALUES (:time,:subject,:body)';
            $params = array(
                        ':subject' => $subject,
                        ':body' => $text,
                        ':time' => time());
            $poststmt = $DBH->prepare($sql);
            $poststmt->execute($params);
            if($poststmt)
            {
                echo "Saved to the database.";
                
            }
            else {
                echo "Not saved.";
            }
            $sqlSubUsers = 'SELECT * FROM test_newsletter WHERE newsletter_id = 1';
            $newsletterStmt = $DBH->prepare($sqlSubUsers);
            $newsletterStmt->execute();
            $userIds = $newsletterStmt->fetch(PDO::FETCH_ASSOC);
            echo $userIds;
            foreach ($userIds as $row){
                $getUserEmailSql = 'SELECT email_addr FROM newsletter_emails WHERE id =' . $row['people_id'];
                $getUserEmailStmt = $DBH->prepare($getUserEmailSql);
                $getUserEmailStmt->execute();
                $userEmail = "<" . $getUserEmailStmt->fetch(PDO::FETCH_COLUMN,0) . ">";
                $userEmail->closeCursor();
                $html = "<html><body>" . $text . "</html></body>";
                $headers = array ('From' => $from,
                              'To' => $userEmail,
                              'Subject' => $subject);
                $smtp = Mail::factory('smtp',
                                  array ('host' => $host,
                                         'port' => $port,
                                         'username' => $username,
                                         'password' => $password));        
            $mail = $smtp->send($to, $headers, $html);
            echo $userEmail;
            if (PEAR::isError($mail)) {
                echo("<p>" . $mail->getMessage() . "</p>");
            } else {
                echo("<p>Message successfully sent!</p>");
            }
        }
     echo 'Newsletter Sent.';
} else {
        echo '<h3>No newsletter was sent.</h3>';
    }
}
?>

I have removed the emails, host, port, username and password along with in other information I did not think I should send over. 

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.