【问题标题】:Send email using phpmailer使用 phpmailer 发送电子邮件
【发布时间】:2016-12-14 12:13:01
【问题描述】:

我正在尝试使用 PHPMailer 我的控制器代码在这里发送邮件

public function register(){ $this->form_validation->set_rules('username', 'Usename Name', 'trim|required|min_length[3]|max_length[30]');
    $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[employer_registration.email]');
    $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|numeric');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('employer/emp_register');
    }
    else
    {
        $data = array(
            'username' => $this->input->post('username'),
            'email' => $this->input->post('email'),
            'mobile' => $this->input->post('mobile'),
            'password' => $this->input->post('password')
        );
        $email = $this->input->post('email');
        $username = $this->input->post('username');
    // insert form data into database
        if ($this->Employer_model->insertUser($data))
        {
            require 'PHPMailerAutoload.php';
            require 'class.phpmailer.php';
            $mail = new PHPMailer;
            $mail->isSMTP();
            $mail->SMTPAuth = true;
    $subject = 'Testing Email';
            $username = $username;
            $email = $email;
            $body = "Thank you for register your username is $username";
            $mail->AddAddress($email);
            $mail->IsMail();
            $mail->From = 'domainname.com';
            $mail->IsHTML(true);
            $mail->Subject = $subject;
            $mail->Body  = $body;
            $mail->Send();

            if(!$mail->send())
            {
                echo "<script>alert('Sorry! mail not sent')</script>";
               echo 'Mailer Error: ' . $mail->ErrorInfo;
            }
            else
                {
                echo "<script>alert('Email sent to $email mail id')</script>";
                 }
    }}}

我已经从 github 下载了 phpmailer 文件夹,将它包含在我的项目 PHPMailerAutoload 文件中,成功注册后我仍然无法将邮件发送到注册的电子邮件 ID。我收到了类似 Mailer Error: could not instantiate mail function.

之类的错误

【问题讨论】:

  • Codeigniter 有一个电子邮件库。
  • 在下面查看我的答案。
  • 使用 CI 邮件库。
  • 你在这方面犯了很多错误,不值得指出;将您的代码基于 PHPMailer 提供的示例,使用 the latest version,并阅读文档。

标签: php codeigniter phpmailer


【解决方案1】:

您正在使用SMTP 身份验证,但实际上并未定义任何主机,这是必需的:

例如:$mail-&gt;Host = "smtp.example.com";

$mail-&gt;isSMTP(); 后面加上你的域名。

此外,如果需要,您可以选择提供这样的用户名和密码:

$mail->Username = 'username'; $mail->Password = 'password';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多