【问题标题】:Send email from code in PHP at localhost在 localhost 从 PHP 代码发送电子邮件
【发布时间】:2017-03-26 15:02:59
【问题描述】:

我喜欢在本地主机上通过代码设置自动发送电子邮件。 我有 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);
        }
    }

【问题讨论】:

  • 您确定自己是 JavaScript 开发人员吗?这感觉就像 PHP :-?
  • :) 抱歉,我不是网络开发人员。我已经更新了。谢谢
  • 如果您想发送电子邮件,您需要一个电子邮件服务器。但是安装自己的服务器很疯狂。我建议您只使用您获得的服务器与您的xxxxxxx.com 托管服务。
  • 自动是什么意思?能详细点吗?

标签: javascript php email


【解决方案1】:

使用 phpmailer,您将需要一个电子邮件服务器,最简单的方法是使用 gmail 之类的网络主机或其他任何东西,只需 google 为他们的 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';
}

【讨论】:

    【解决方案2】:

    只需电子邮件代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多