如何使用CodeIgniter从地址添加?

时间:2018-03-22 14:20:52

标签: php function codeigniter email

我正在开发小型CodeIgniter应用程序,我必须在其中构建自己的联系人表单,一切正常,接收电子邮件但是我只需要在邮件功能中添加From Address吗?

CodeIgniter邮件功能

public function form() {

 $this->load->library('form_validation');
 $this->form_validation->set_rules('name', 'Name', 'trim|required|alpha');
 $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
 $this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
 $this->form_validation->set_rules('message', 'Message', 'trim|required');
 $data['success'] = false;

if ($this->form_validation->run() == TRUE) {

 @mail(config('webmaster_email'), 'Contact Us from ABC',""
    . "Full Name: $_POST[name]\n"
    . "Email: $_POST[email]\n"
    . "Phone: $_POST[phone]\n"
    . "Message: $_POST[message]\n"
    . "");
    $data['success'] = true;


 }

    $this->load->view($this->module, $data);

 }

需要在表单邮件功能中添加此行

'From: webmaster@example.com' 

1 个答案:

答案 0 :(得分:0)

您需要使用邮件标题从您想要的电子邮件地址发送电子邮件。 如果你需要简单的电子邮件没有附件和标记使用mail函数我更喜欢使用PHP邮件程序,请参阅How to send mail using phpmailer

function sendmail($to, $subject, $message, $from)
{
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    $headers .= 'Reply-To: ' . $from . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    @mail($to, $subject, $message, $headers);

    if ($result) return 'sent';
    else return 'error';
}

$to = config('webmaster_email');
$from = "webmaster@example.com";
$subject = "Contact Us from ABC";
$message =
    "Full Name: $_POST[name]\n"
    . "Email: $_POST[email]\n"
    . "Phone: $_POST[phone]\n"
    . "Message: $_POST[message]\n";

$result = sendmail($to, $subject, $message, $from);
var_dump($result);

PHP mail()用于标题的第4个参数。

完整的PHP电子邮件标题:

$headers  = "From: Your Website < mail@yourwebsite.com >\n";
$headers .= "Cc: Your Website < mail@yourwebsite.com >\n"; 
$headers .= "X-Sender: Your Website < mail@yourwebsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail@Your Website.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";