Welcome Guest, Not a member yet? Register   Sign In
Add a comment and email the comment to notify all email addresses in the database
#1

[eluser]Neoraj3.0[/eluser]
Hi I am trying to get a much simpler version of adding a comment to a topic in blog and as the comment is added an email i sent to very email address in my database. I works fine on my local system 'localhost' but the email does not work on the live site on 000webhosts. Please help.

These are the errors:

Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Connection timed out)

Filename: libraries/Email.php

Line Number: 1689



Message: fwrite(): supplied argument is not a valid stream resource

Filename: libraries/Email.php

Line Number: 1846




this is the Model:

Code:
function get_comment_for_email(){

//  comments.*  this means every field on the comments table.
$this->db->select('comments.*, users.username, users.email');
$this->db->from('comments');
$this->db->join('users', 'users.userID=comments.userID', 'left');
//$this->db->where('users.userID', 'comments.userID');
$this->db->order_by('comments.date_added', 'desc');
$this->db->limit(1);
$query = $this->db->get();
return $query->result_array();
}

This is the Controller:

Code:
<?php

class Comments extends CI_Controller {



function add_comment($entryID){
  date_default_timezone_set('America/Barbados');   //becareful when using this on alive server it may be incorrect




  //if nothing got posted redirect to entryID
  if(!$_POST){
   redirect(base_url().'blogs/an_entry/'.$entryID);

  }

  //check to see that they are logged in
   $user_Type = $this->session->userdata('user_type');
  if(!$user_Type){
   //if not logged in redirect to login page
   redirect(base_url().'users/log_in');
  }


  //load comment model
  $this->load->model('comment');
  $this->load->model('user');
  $this->load->model('blog');

  //create a data array
  $data = array(
    'entry_id' => $entryID,
    'userID' => $this->session->userdata('userID'),
    'comment' =>$_POST['comment'],
    'date_added' => date('Y-m-d H:i:s')
             );
  //execute
  $this->comment->add_a_comment($data);


  //AT THIS POINT EMAILS ARE SENT TO ALL EMAIL ADDRESSES STORED IN THE DATABASE of the last comment added
  //get all the email addresses from User Model
  $emails = $this->user->get_emails();
  $username_and_comments = $this->comment->get_comment_for_email();
  $subjects = $this->blog->get_subjects();

  //Setup gmail email configs
  $email_config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.gmail.com',
            'smtp_port' => '465',
            'smtp_crypto' => 'ssl',
            'smtp_timeout' => '7',
            'smtp_user' => '[email protected]',
            'smtp_pass' => 'password',
            'charset'   =>  'iso-8859-1',   //or 'charset' => 'utf-8'
            'mailtype'  => 'html',
            'wordwrap' => TRUE,
            'starttls'  => true,
            'newline'   => "\r\n"
        );

      

        foreach ($emails as $row) {

         if($row['email']){
         $this->load->library('email', $email_config);
         $this->email->from('my gmail address', 'G.O.T -Season 4 Blog Message');
         $this->email->to($row['email']);
    foreach ($username_and_comments as $row2) {
          $this->email->subject('G.O.T -Season 4 Blog Message');
          foreach ($subjects as $row3) {
         $this->email->message('For the post named:'.$row3['title'] .' '. $row2['username'].'said:' .$row2['comment']);
         }
         }
         $this->email->send();
        // uese print_debugger (below) for tesing email in debug mode
        // echo $this->email->print_debugger();
        
         $this->email->clear();

     }

  }


  //redirect
  redirect(base_url().'blogs/an_entry/'.$entryID);

  

}


}
#2

[eluser]Unknown[/eluser]
Is port 465 open for use between your host and the target?
#3

[eluser]CroNiX[/eluser]
Break it down. First get rid of sending in a loop until you get a single email working. Use the debugging method available to the email class. It might provide more info ($this->email->print_debugger()). Make sure port 465 is open. Try other ports like port 587, etc.
#4

[eluser]Neoraj3.0[/eluser]
Well i used telnet to check port 25, 465 and 587 they are all open.

I also called my ISP they said they are dont have any blocks on. I guess i can contact the host also. What messages should I be looking for in the debugger?
#5

[eluser]InsiteFX[/eluser]
Why are you using gmail? Use your hostings SMTP server.
#6

[eluser]Neoraj3.0[/eluser]
Well I also did that today.

My host sent me this info which I used:

Username: [[email protected]]
Password: [the password you set up when you configured your mail box]
Incoming Mail Server: [mail.domain.ext]
Outgoing Mail Server: [smtp.domain.ext]
Outgoing Server requires Authentication (typically found in advanced options or settings)
Use Same Username and Password as Incoming
Incoming Server Port: 110 (POP3)
Incoming Server Port: 143 (IMAP)
Outgoing Server Port: 2525 (POP3/IMAP)
Outgoing Server Port: 587 (Verizon Carrier)

**Authentication: Password
**Please Turn Off the SSL Connection for both Incoming and Outgoing Server Settings.


I first used this (below) and got the same error "fsockopen(): unable to connect to smtp" etc

Code:
$email_config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.thedomain..com',
            'smtp_port' => '2525',
            'validation' => TRUE,
            'smtp_timeout' => '7',
            'smtp_user' => '[email protected]',
            'smtp_pass' => 'password',
            'charset'   =>  'iso-8859-1',   //or 'charset' => 'iso-8859-1'
            'mailtype'  => 'html',
            'wordwrap' => TRUE,
            'newline'   => "\r\n"
        );

Then I tried the Verizon port 587 just for fun but same results:

Code:
$email_config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.mydomain.com',
            'smtp_port' => '587',
            'validation' => TRUE,
          //  'smtp_crypto' => 'ssl',
            'smtp_timeout' => '7',
            'smtp_user' => [email protected]',
            'smtp_pass' => 'password',
            'charset'   =>  'iso-8859-1',   //or 'charset' => 'iso-8859-1'
            'mailtype'  => 'html',
            'wordwrap' => TRUE,
          //  'starttls'  => true,
            'newline'   => "\r\n"
        );
#7

[eluser]InsiteFX[/eluser]
Code:
$this->load->library('email');   // This should not be in your foreach loop.

     $config = Array(
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.mydomain.com',
            'smtp_port' => '587',
            'validation' => TRUE,
          //  'smtp_crypto' => 'ssl',
            'smtp_timeout' => '7',
            'smtp_user' => [email protected]',
            'smtp_pass' => 'password',
            'charset'   =>  'iso-8859-1',   //or 'charset' => 'iso-8859-1'
            'mailtype'  => 'html',
            'wordwrap' => TRUE,
          //  'starttls'  => true,
            'newline'   => "\r\n"
     );

     $this->email->initialize($config);
#8

[eluser]Neoraj3.0[/eluser]
Thanks InsiteFX I placed $this->load->library('email'); above the for each loop but got the same results.

On a positive note, I got the email with comment to work when I change 'protocol' => 'smtp' to 'protocol' => 'mail' but the email is much slower and renders the css in the email too. But if anyone has any other ideas for making it work with smtp that would be great. I wondering if I should just change web hosts. Are there any web hosts that are know to be good with providing smtp?




Theme © iAndrew 2016 - Forum software by © MyBB