0

Here's my code and it works well, but i don't want it to be sending as normal. I want it to be sending using SMTP authentication. I really do not know how to do that.

    <?php
if(!isset($_POST['to_emails'],$_POST['from_email'],$_POST['replyTo'],$_POST['comp_name'],$_POST['subject'])) {

    $failure=true;
    echo "Oops, insufficent mailing values posted";
}else{
    foreach(explode(",",$_POST['to_emails']) as $index=>$email){
        $email=trim($email);

        $compname = $_POST['comp_name'];
        $fromail = $_POST['from_email'];
        $headers = 'From: ' . $compname . ' <' . $fromail .'>';
        $headers .= "\r\nContent-Type: text/html; charset=ISO-8859-1";
        $headers .= "\r\nReply-To: " . $_POST['replyTo'];
        $headers .= "\r\nX-Mailer: PHP/" . phpversion();
        $message = $_POST['message'];
        $message = str_replace("&email&", $email, $message);

        if(mail($email, $_POST['subject'], $message, $headers)){
            echo "<font color='#FFFFB9'>{$index} Emailed: {$email}</font><br />";
        }else{
            $failure=true;
            echo "Oops, something went wrong on email index $index to $email<br>";
        }
    }
}

    ?>
Eric
  • 43
  • 6
  • 1
    Possible duplicate of [Setting SMTP details for php mail () function](https://stackoverflow.com/questions/6093976/setting-smtp-details-for-php-mail-function) – Metalik Jun 13 '18 at 09:00
  • Why don't you use a library like SwiftMailer that handles all that difficult stuff for you? – Nico Haase Jun 13 '18 at 09:42

1 Answers1

0

function do_email($msg = NULL, $sub = NULL, $to = NULL, $from = NULL, $bcc = NULL) {

        $config = array();
        $config['useragent'] = "CodeIgniter";
        $config['mailpath'] = "/usr/bin/sendmail";
        $config['protocol'] = "smtp";
        $config['smtp_host'] = "ssl://smtp.gmail.com";
        $config['smtp_port'] = "465";
        $config['smtp_user'] = "yourgmail"; 
        $config['smtp_pass'] = "password";
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";


        $this->load->library('email');
        $this->email->initialize($config);      

        $this->email->from($from);
        $this->email->to($to);
        $this->email->subject($sub);            
        $this->email->message($msg); 
        $this->email->send();            

    }

Send your parameters to the above function.

vejitha
  • 13
  • 5
  • Thank you. But i tried to do this but it's still not sending mails: – Eric Jun 13 '18 at 11:52
  • can you please post the error by debugging with the line below echo $this->email->print_debugger(); exit(); – vejitha Jun 13 '18 at 11:59
  • Fatal error: Using $this when not in object context in C:\wamp2\www\Email_sender_SMTP\test2.php on line 26 – Eric Jun 13 '18 at 14:02