【问题标题】:how to send smtp mails?如何发送 smtp 邮件?
【发布时间】:2019-04-22 14:04:37
【问题描述】:
 $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 发送电子邮件。请帮帮我。我不明白我哪里出错了。

【问题讨论】:

  • 我在 Godaddy 服务器上工作。
  • 省去一些麻烦,开始使用电子邮件类来处理电子邮件。我推荐 PHPMailer。它易于使用,您可以直接从脚本中加载它。

标签: php codeigniter smtp


【解决方案1】:

你可以试试下面的脚本:

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

【讨论】:

【解决方案2】:
<?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); 


?> 

【讨论】:

  • 使用发送邮件功能发送邮件的完整代码
  • 感谢您的代码 mukesh。邮件仅从我的代码触发到收件箱。但仅对 hotmail 用户而言,邮件最终会成为垃圾邮件。你能建议这个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2010-11-22
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 2011-07-30
相关资源
最近更新 更多