首页 > 解决方案 > 有人可以帮我编辑我的代码,以便它使用 SMTP 身份验证来发送邮件吗?

问题描述

这是我的代码,它运行良好,但我不希望它正常发送。我希望它使用 SMTP 身份验证发送。我真的不知道该怎么做。

    <?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>";
        }
    }
}

    ?>

标签: phpsmtpmailer

解决方案


函数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();            

    }

将您的参数发送到上述函数。


推荐阅读