Zoeken…


Opmerkingen

In CodeIgniter 3 moet u de parameter opnemen:

$config['newline'] = "\r\n";

Het zal gewoon niet werken zonder.

Als u niet om nieuwe regels geeft en u CodeIgniter 2 gebruikt, is deze configuratieparameter optioneel.

Laad de e-mailbibliotheek

Eerst moet u de e-mailbibliotheek laden.

Doe dit in het controllerbestand dat de e-mail verzendt:

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

Of laad het globaal in het autoload.php-bestand in de configuratiemap:

$autoload['libraries'] = array('email');

Terwijl je daar bent, wil je misschien de e-mailhelper laden als je een aantal van CodeIgniter's ingebouwde snelkoppelingen wilt gebruiken:

$autoload['helper'] = array('email');

De e-mailhelper kan op dezelfde manier in het controllerbestand worden geladen als de e-mailbibliotheek:

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

Stel uw e-mailconfiguratieparameters in

Maak een nieuw bestand in de applicatie / configuratie map met de naam email.php

Stel de parameters in voor het verzenden van e-mail. Deze worden geladen wanneer u uw e-mail verzendt.

$config['newline'] = "\r\n"; //You must use double quotes on this one
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //Change for your specific needs
$config['smtp_port'] = 465; //Change for your specific needs
$config['smtp_user'] = '[email protected]'; //Change for your specific needs
$config['smtp_pass'] = 'yourpassword'; //Change for your specific needs
$config['charset'] = 'iso-8859-1';
$config['mailtype'] = 'text'; //This can be set as 'html' too

Maak je e-mail

$this->email->from('[email protected]', 'Tom Webmaster');
$this->email->to('[email protected]', 'Freddie Fakeperson');
$this->email->subject('Your Account Is Active');
$this->email->message('Welcome to our new site!');

In de 'van'-methode is de eerste parameter het e-mailadres van waaruit u verzendt, de tweede parameter is de naam die u de ontvanger wilt laten zien.

In de methode 'aan' definieert u naar wie de e-mail wordt verzonden.

De 'onderwerp'-methode definieert het onderwerp van de e-mail.

De 'bericht'-methode definieert wat er in uw e-mail staat.

Dit kunnen gegevens zijn die door een gebruiker naar uw site zijn verzonden. Dus je hebt hier misschien een variabele die geposte gegevens bevat. Dus ze kunnen er meer zo uitzien:

$this->email->to($email, $username);

Stuur je e-mail

$sent = $this->email->send();


//This is optional - but good when you're in a testing environment.
if(isset($sent)){
    echo "It sent!";
}else{
    echo "It did not send.";
}

Stuur een HTML-e-mail

Maar u wilt niet alleen een e-mail met platte tekst. U wilt een mooie HTML-e-mail.

Stel uw configuratiebestand in als html:

$config['mailtype'] = 'html';

Als u gegevens (zoals een gebruikersnaam bijvoorbeeld) wilt doorgeven aan de HTML-e-mail, plaatst u deze in een array:

$data = array('name' => $name, 
              'email' => $email,
              'phone' => $phone,
              'date' => $date);

Wijs vervolgens bij het verzenden uw 'bericht' op een weergave. Geef uw gegevensmatrix er vervolgens aan door:

$this->email->message($this->load->view('new_user',$data, true));

Maak in uw applicatie / view-map uw view.

In dit geval heet het 'new_user.php'.

Je kunt dit op elke gewenste manier stylen. Hier is een snel voorbeeld:

<html>
<head>
    <style type='text/css'>
        body {background-color: #CCD9F9;
             font-family: Verdana, Geneva, sans-serif}

        h3 {color:#4C628D}

        p {font-weight:bold}
    </style>
</head>
<body>

    <h3>Hi <?php echo $name;?>,</h3>
    <h3>Thanks for contacting us.</h3> 

    <p>You've taken your first step into a larger world.</p>   
    <p>We really appreciate your interest.</p>

</body>
</html>

Contact Formulier

Controller (Pages.php)

public function contact()
{
    
    $this->load->library('email');
    $this->load->library('form_validation');

    //Set form validation
    $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[4]|max_length[16]');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[60]');
    $this->form_validation->set_rules('message', 'Message', 'trim|required|min_length[12]|max_length[200]');

    //Run form validation
    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('contact');
    } else {

        //Get the form data
        $name = $this->input->post('name');
        $from_email = $this->input->post('email');
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        //Web master email
        $to_email = '[email protected]'; //Webmaster email, who receive mails

        //Mail settings
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = '[email protected]'; // Your email address
        $config['smtp_pass'] = 'mailpassword'; // Your email account password
        $config['mailtype'] = 'html'; // or 'text'
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE; //No quotes required
        $config['newline'] = "\r\n"; //Double quotes required

        $this->email->initialize($config);                        

        //Send mail with data
        $this->email->from($from_email, $name);
        $this->email->to($to_email);
        $this->email->subject($subject);
        $this->email->message($message);
        
        if ($this->email->send())
        {
            $this->session->set_flashdata('msg','<div class="alert alert-success">Mail sent!</div>');

            redirect('contact');
        } else {
            $this->session->set_flashdata('msg','<div class="alert alert-danger">Problem in sending</div>');
            $this->load->view('contact');
        }

    }

Weergaven (contact.php)

<div class="container">
<h2>Contact</h2>
<div class="row">
    <div class="col-lg-6">
        <?php echo $this->session->flashdata('msg'); ?>
        <form action="<?php echo base_url('contact'); ?>" method="post">
        <div class="form-group">
            <input name="name" placeholder="Your name" type="text" value="<?php echo set_value('name'); ?>" class="form-control" />
            <?php echo form_error('name', '<span class="text-danger">','</span>'); ?>
        </div>
        <div class="form-group">
            <input name="email" placeholder="Your e-mail" type="text" value="<?php echo set_value('email'); ?>" class="form-control" />
            <?php echo form_error('email', '<span class="text-danger">','</span>'); ?>
        </div>
        <div class="form-group">
            <input name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" class="form-control" />
        </div>
        <div class="form-group">
            <textarea name="message" rows="4" class="form-control" placeholder="Your message"><?php echo set_value('message'); ?></textarea>
            <?php echo form_error('message', '<span class="text-danger">','</span>'); ?>
        </div>
        <button name="submit" type="submit" class="btn btn-primary" />Send</button>
        </form>
    </div>
</div>


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow