CodeIgniter Email helper
×

CodeIgniter Email helper

1895

Email helper contains functions that help us for working with Emails.


Related Topics:

Codeigniter Interview Questions
CodeIgniter File helper
CodeIgniter HTML Helper

Loading this Helper:

Use the given line in your controller or model file to load the Email helper.

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

The following functions are available in this helper:

1 valid_email(): This function is used in the format of Email addresses. It returns True if the input is a correctly formatted e-mail address.

Syntax:

valid_email($email);

Parameter Description:

  • $email (string) – E-mail address

The return type of this function is bool and it returns TRUE if a valid email is supplied, FALSE otherwise.

Example:

Step 1 Open the application/controllers directory and create a new controller file Valid_mail.php

<?php
class Valid_mail extends CI_Controller {

public function index() {
$this->load->helper('email'); //load Email helper
if (valid_email('email@somesite.com'))
{
echo 'email is valid';
}
else
{
echo 'email is not valid';
}
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/valid_mail


2 send_email(): This function is used to send email by using PHP’s native mail() function.

Syntax:

send_email($recipient, $subject, $message);

Parameter Description:

  • $recipient (string) – E-mail address
  • $subject (string) – Mail subject
  • $message (string) – Message body

The return type of this function is bool and it returns TRUE if the mail was successfully sent, FALSE in case of an error.

Example:

Step 1 Open application/controllers directory and create a new controller file Send_mail.php

<?php
class Send_mail extends CI_Controller {

public function index() {
$this->load->helper('email'); //load Email helper
$msg = "This is a test mail";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: heading <sender@somesite.com>' . "\r\n";

$send=send_email("receiver@somesite.com","Subject",$msg,$headers);
if($send){
echo "successfully sent";
}else{
echo "try again";
}
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Send_mail



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments