travisco87 Posted September 25, 2014 Share Posted September 25, 2014 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. Quote Link to comment Share on other sites More sharing options...
davidannis Posted September 26, 2014 Share Posted September 26, 2014 (edited) Does it print an error message? Does it print "Message successfully sent!"? Is php error reporting on? (If not, put this at the top of your php file:) error_reporting(E_ALL); ini_set("display_errors", 1); Edited September 26, 2014 by davidannis Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.