使用codeigniter发送电子邮件(新手)

时间:2013-02-21 10:24:23

标签: php codeigniter email smtp gmail

我是Codeigniter的菜鸟,我想制作一个联系表格,我希望将其发送到我的Gmail。我提出了一个观点:

<div id="content">
<?php if (isset($mail_sent)): ?>
<b>Mail Sent</b>
<?php endif; ?>

<form action="<?php echo base_url() ?>" method="POST">

<b>Your Name :</b>
<br />
<input type="text" name="name" />
<br />
<b>Your Email Address:</b>
<br />
<input type="text" name="from" />
<br />
<b>Subject :</b>
<br />
<input type="text" name="subject" />
<br />
<b>Message :</b>
<br />
<textarea name="message" rows="10" cols="15"></textarea>
<br />
<input type="submit" name="q" value="Contact" />

</form>
</div>

我的邮件设置在我的联系页面的同一控制器代码块中。它看起来像这样:

public function contact()
{
    $this->load->view('header');
    $this->load->view('owrb_contact');
    $this->load->view('footer');

    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'iso-8859-1';

    // gmail specific settings here
    $config['smtp_host'] = 'smtp.gmail.com';
    $config['smtp_user'] = 'keessonnema@gmail.com';
    $config['smtp_pass'] = '123keesje123';
    $config['smtp_port'] = '465';

    $config['wordwrap'] = TRUE;

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

    $this->email->from('keessonnema@gmail.com', 'Kees Sonnema');

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

    $this->email->send();
}

现在我必须配置一个smtp gmail控制器?我真的不知道该怎么做。

由于

1 个答案:

答案 0 :(得分:2)

您可以使用Codeigniter Framework发送邮件,此处将对其进行描述。

http://ellislab.com/codeigniter/user-guide/libraries/email.html

协议,服务器和您希望配置的每个项目都有设置。如果你愿意的话,你可以用gmail挂钩。

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';

// gmail specific settings here
$config['smtp_host'] = '';
$config['smtp_user'] = '';
$config['smtp_pass'] = '';
$config['smtp_port'] = '';

$config['wordwrap'] = TRUE;

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

$this->email->from('your@example.com', 'Your Name');

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

$this->email->send();