如何发送SMTP邮件?

时间:2018-11-20 06:57:17

标签: php codeigniter smtp

 $config = array();
       $config['protocol'] = 'smtp';
       $config['smtp_host'] = 'smtp.gmail.com';
       $config['smtp_user'] = '***';
       $config['smtp_pass'] = '***';
       $config['smtp_port'] = 465;
       $this->email->initialize($config);

       $useremail = $result['email'];
        $this->load->library('email',array("mailtype" => "html"));
        $from = '******';
        $this->email->from($from);
        $this->email->to($useremail);
        $base_url = base_url();
        $subject = "App : Please validate your email";
        $this->email->subject($subject);

        $message = "Dear ${FirstName} ${LastName}! Welcome to app.\r\n\r\nRegards,\r\nTeam ";
        $this->email->message($message);
        if($this->email->send()){
          echo 1;
        }else{
            echo 0;
        }

我无法使用smtp发送电子邮件。请帮助我。我没弄错我要去哪里。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用以下脚本:

$this->load->library("email");
$config = array("protocol"=>"smtp","smtp_host"=>"ssl://smtp.gmail.com","smtp_user"=>"$smtp_user","smtp_pass"=>"$smtp_password","smtp_port"=>"465","mailtype"=>"html","charset"=>"utf-8","newline"=>"\r\n");
$this->email->initialize($config);
$this->email->from("$email", "$name"); 
$this->email->to($to_email);
$this->email->subject("$subject"); 
$this->email->message("$message"); 
//Send mail 
if($this->email->send()){
    echo "SUCCESS";
}
else{
    echo "FAILED";
}

答案 1 :(得分:0)

<?php
    $data=array();
    if($_POST['submit']){

            $otp  = $_POST['name'];
            $email =$_POST['email'];




           require 'mailer/class.phpmailer.php';

            //Create a new PHPMailer instance
            $mail = new PHPMailer();
            //Tell PHPMailer to use SMTP
            $mail->IsSMTP();
            //Enable SMTP debugging
            // 0 = off (for production use)
            // 1 = client messages
            // 2 = client and server messages
            $mail->SMTPDebug  = 0;
            //Ask for HTML-friendly debug output
            $mail->Debugoutput = 'html';
            //Set the hostname of the mail server
            $mail->Host       = "sg2plcpnl0014.prod.sin2.secureserver.net";
            //Set the SMTP port number - likely to be 25, 465 or 587
            $mail->Port       = 465;
            //Whether to use SMTP authentication
            $mail->SMTPAuth   = true;
            //Username to use for SMTP authentication
            $mail->SMTPSecure = 'ssl';
            //Set the encryption system to use - ssl (deprecated) or tls
            $mail->Username   = "yourmail@gmail.com"; //email that u want auth
            //Password to use for SMTP authentication
            $mail->Password   = "Techno@123";
            //Set who the message is to be sent from
            $mail->SetFrom('yourmail@gmail.com', 'your name'); //email that u want sent from
            //Set an alternative reply-to address
            $mail->AddReplyTo($email,$name); //email or name that u want sent to 
            //Set who the message is to be sent to

            $mail->Subject = 'Yo got a msg From user';
            //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
            //$mail->MsgHTML(file_get_contents('contents.html'), dirname(__FILE__));
            $content="<b>Email Recovery OTP  :  </b>".$otp."<br>";




            $mail->MsgHTML($content);
            //Replace the plain text body with one created manually
            //$mail->AltBody = '$name'.'$email'.'$telephone'.'$comments';
            //Attach an image file
            //$mail->AddAttachment($target);

            //Send the message, check for errors

            if(!$mail->Send()) {
                 //echo "Mailer Error: " . $mail->ErrorInfo;
              $data[0]=array('result'=>'Mail not send');
            } else {
               $data[0]=array('result'=>'Mail Send');
            }

    }else{
        $data[0]=array('result'=>'Error');
    }
    echo json_encode($data); 


?>