从localhost的PHP代码发送电子邮件

时间:2017-03-26 15:02:59

标签: javascript php email

我喜欢在本地主机上设置代码自动发送电子邮件。 我有PHP代码。

但不确定,我需要在本地主机上安装或设置一些内容,以便我的网站可以自动发送电子邮件。

我的代码如下。

    public function sendEmail_post()
    {
        $jdata = json_decode(file_get_contents('php://input'), true);
        $id = $jdata['id'];
        $query = $this->Register_model->sendEmail($id);
        $email = $query[0]->email;
        $token = $query[0]->token;
        $user_type = $this->Register_model->roleNames($id);
        if ($user_type != "Consumer") {
            $user_type = "admin";
        } else {
            $user_type = "user";
        }

        $this->email->from('no-reply@xxxxxxx.com', 'XXXXxxxx');
        $this->email->to($email);
        $this->email->subject('Account Activation');
        $this->email->message('

Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.


Please click this link to activate your account:
<a href="' . $this->config->item('emailRedirect') . $user_type . '/#/login?auth=' . sha1($email) . '&token=' . sha1($token) . '">Click here<a>'
        );

        $this->email->set_newline("\r\n");
        //$this->db->query('update user set token ="active",status="active" from user where email=$email');
        if ($this->email->send()) {

            $this->db->query("update user set emailverify='No' where email='$email'");
            //$this->Crud_model->verifyEmailID($e_mail);
            $this->response(array('result' => 'mail send successfully', 'success' => true), 200);
        } else {
            $this->response(array('result' => 'Failed to send Email', 'success' => false), 404);
        }
    }

2 个答案:

答案 0 :(得分:2)

使用phpmailer,您将需要一个电子邮件服务器最简单的方法是使用像gmail或其他任何东西的网络主机,只需谷歌为他们的smtp主机。

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

 $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

答案 1 :(得分:1)

只需发送电子邮件代码

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

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();

我希望这会对你有所帮助

    public function sendEmail_post()
    {
        $jdata = json_decode(file_get_contents('php://input'), true);
        $id = $jdata['id'];
        $query = $this->Register_model->sendEmail($id);
        $email = $query[0]->email;
        $token = $query[0]->token;
        $user_type = $this->Register_model->roleNames($id);
        if ($user_type != "Consumer") {
            $user_type = "admin";
        } else {
            $user_type = "user";
        }
        $config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    'mailtype'  => 'html', 
    'charset'   => 'iso-8859-1'
);
$this->load->library('email', $config)
        $this->email->from('no-reply@xxxxxxx.com', 'XXXXxxxx');
        $this->email->to($email);
        $this->email->subject('Account Activation');
        $this->email->message('

Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.


Please click this link to activate your account:
<a href="' . $this->config->item('emailRedirect') . $user_type . '/#/login?auth=' . sha1($email) . '&token=' . sha1($token) . '">Click here<a>'
        );

        $this->email->set_newline("\r\n");
        //$this->db->query('update user set token ="active",status="active" from user where email=$email');
        if ($this->email->send()) {

            $this->db->query("update user set emailverify='No' where email='$email'");
            //$this->Crud_model->verifyEmailID($e_mail);
            $this->response(array('result' => 'mail send successfully', 'success' => true), 200);
        } else {
            $this->response(array('result' => 'Failed to send Email', 'success' => false), 404);
        }