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 protected]>";
            $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
https://forums.phpfreaks.com/topic/291283-sending-a-newsletter-with-mail/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.