首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP电子邮件表单发件人名称而不是电子邮件?

PHP电子邮件表单发件人名称而不是电子邮件?
EN

Stack Overflow用户
提问于 2011-07-18 11:25:02
回答 8查看 63.5K关注 0票数 22

现在,我已经设置了一个PHP电子邮件表单,一切都很正常。但是,在测试它时,我只能获得发件人的电子邮件地址作为名称。我想要的是发送者的名字,比如:

无名氏

主体主体

而不是:

j.doe@website.com

主体主体

下面是代码..。

有人能帮帮我吗?谢谢。

PHP:

代码语言:javascript
复制
<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "ricky@rickytsang.ca";
    $email_subject = $_REQUEST['subject'];


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['full_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $full_name = $_POST['full_name']; // required
    $email_from = $_POST['email']; // required
    $subject = $_POST['subject'];
    $message= $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The e-mail you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$full_name)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }
  if(strlen($message) < 2) {
    $error_message .= 'The message you entered doee not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\n";
    $email_message .= "E-mail: ".clean_string($email_from)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-07-18 11:42:38

在表示$email_message .= ...的4行后面添加一行:

代码语言:javascript
复制
$email_from = $full_name.'<'.$email_from.'>';
票数 35
EN

Stack Overflow用户

发布于 2011-07-18 11:31:44

THen您需要以下形式的它:

代码语言:javascript
复制
"John Doe <j.doe@website.com>"
票数 14
EN

Stack Overflow用户

发布于 2011-07-18 11:32:06

通常,您只需重新格式化发件人地址,如下所示:

代码语言:javascript
复制
"Ricky, Dear" <dearricky@yahoo.com>

http://php.net/manual/en/function.mail.php

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6728361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档