简体   繁体   中英

how to send mail from localhost in php codeigniter?

i try to send mail. its give no error but it not delivered mail

        $config['smtp_host']  = "ssl://smtp.googlemail.com";
        $config['smtp_port']  = "456";
        $config['smtp_user']  = "mymailid@gmail.com";
        $config['smtp_pass']  = "mypassword";
        $this->email->initialize($config);

        $message = "my message";
        $this->email->from('mymailid@gmail.com'); 
        $this->email->to('recivermailid@gmail.com');
        $this->email->subject('Testing');
        $this->email->message($message);
        if($this->email->send())
        {
                echo 'Email sent.';
        }
        else
        {
                show_error($this->email->print_debugger());
        }

its return "Email send" But mail not delivered

Try this tested code.

 function sendMail()
    {
       $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'xxx@gmail.com', 
      'smtp_pass' => 'xxx', 
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );

          $message = '';
          $this->load->library('email', $config);
          $this->email->set_newline("\r\n");
          $this->email->from('xxx@gmail.com');
          $this->email->to('xxx@gmail.com');
          $this->email->subject('Your Subject');
          $this->email->message($message);
          if($this->email->send())
         {
          echo 'Email sent.';
         }
         else
        {
         show_error($this->email->print_debugger());
        }

    }

you can do it with phpmailer. you can download it from here PHPMailer

Now do the follwoing code in your function:

             public function sendmail()
                {
    require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php');

              $mail = new PHPMailer;
              $mail->isSMTP();                            // Set mailer to use SMTP
              $mail->Host = 'smtp.gmail.com';             // Specify main and backup SMTP servers
              $mail->SMTPAuth = true;                     // Enable SMTP authentication
              $mail->Username = 'Email Address';          // SMTP username
              $mail->Password = 'Email Account Password'; // SMTP password
              $mail->SMTPSecure = 'tls';                  // Enable TLS encryption, `ssl` also accepted
              $mail->Port = 587;                          // TCP port to connect to

              $mail->setFrom('info@codexworld.com', 'CodexWorld');
              $mail->addReplyTo('info@codexworld.com', 'CodexWorld');
              $mail->addAddress('john@gmail.com');   // Add a recipient
              $mail->addCC('cc@example.com');
              $mail->addBCC('bcc@example.com');

              $mail->isHTML(true);  // Set email format to HTML

              $bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
              $bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';

              $mail->Subject = 'Email from Localhost by CodexWorld';
              $mail->Body    = $bodyContent;

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

If incase syntax error came visit here

This is working perfectly for me.

您可以使用http://www.toolheap.com/test-mail-server-tool/它提供了在此页面上完成的所有必需配置。

    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';                            //ssl
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'xxxx@gmail.com'; //gmail id
    $config['smtp_pass']    = 'xxxx';           //gmail password
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'html'; 
    $config['validation'] = TRUE;

how to send mail (sendmail) with php codeigniter in localhost(wampserver).

stage one: downloading sendmail from this website glob.com .
stage two: unziping the sendmail zip file and put it in the wampserver directory.
stage three: opening sendmail folder and choosing sendmail.ini file in notepad and changing some lines there:
1.smtp_server=smtp.gmail.com
2.smtp_port=465
3.smtp_ssl=ssl
4.default_domain=localhost
5.error_logfile=error.log
6.debug_logfile=debug.log
7.auth_username=your mail(example@gmail.com)
8.auth_password=your mail real password
9.force_sender=your mail(example@gmail.com)
10.hostname=localhost

stage four: opening the php.ini file in this address (E:\\wamp64\\bin\\apache\\apache2.4.37\\bin)
stage five: changing some lines there and putting the sendmail.exe address in sendmail_path like below:
ctrl+f -> you must search (mail function) and find it in sendmail.ini

[mail function];
For Win32 only.
SMTP =
smtp_port =
; For Win32 only.
sendmail_from =
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path ="E:\wamp64\sendmail\sendmail.exe -t -i"

stage six: opening the php.ini in the (E:\\wamp64\\bin\\php\\php7.2.14) address according to the place that you have installed your wampserver and the version of php you are using
stage seven: changing some lines here:
ctrl+f -> you must search (mail function) like below.

[mail function]
; For Win32 only.
SMTP =
smtp_port = 25
; For Win32 only.
sendmail_from ="admin@wampserver.invalid"
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path ="E:\wamp64\sendmail\sendmail.exe  -t -i"

stage eight: restarting the wampserver with its icon on the right bottom side of desktop.
stage nine: enabling ssl_module with right clicking on the wampserver icon choosing apache/apache_module/ssl_module
stage ten: enabling open_ssl and sockets with right clicking on the wampserver icon choosing php/php_extensions/open_ssl and php/php_extensions/sockets
again restarting the wampserver

stage eleven: getting data from view in codeignter inside the controller and have these codes in the controller:

if (isset($_POST['btn_send_contact_form'])) {
    
    $data = $_POST['frm'];

    $firstname = $data['firstname'];
    $lastname = $data['lastname'];
    $email = $data['email'];
    $msg_title = $data['msg_title'];
    $msg_text = $data['msg_text'];

    $this->load->library('email'); // email library must be included.

    $this->email->from($email, $firstname);
    $this->email->to('yourmail@gmail.com'); // the receiver mail
    // $this->email->cc('another@another-example.com');
    // $this->email->bcc('them@their-example.com');
    $this->email->subject($msg_title);
    $this->email->message($msg_text);
    
    $res = $this->email->send();

    if ($res) {
        header("location: contact_us?contactUs_mail_sending=successful");
    } else {
        header("location: contact_us?contactUs_mail_sending=error");
    }
    
} 

$this->load->view('pages/contact_us');

stage twelve: (important) enabling (the Less secure app access) in your gmail account in settings/security.

stage thirteen: sending mail successfully.

Note: if every thing is ok, just try (enabling the less secure app access in your gmail account, although it is not recommended, it able you to send a simple email (in gmail account).

hope to be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM