dongrong9053 2014-08-28 09:14
浏览 80
已采纳

1and1上的php电子邮件表单

I am trying to combine the name field with the email field so that the email will be delivered like:

From: Name of Person Filling Out Form (email_address.com)

Goal: (Email From Line would contain the person's name and email).

I am using 1and1's mail.php function and there are no instructions on how to do this.

Is this even possible?

Here is my code:

<?php

echo '<link href="new/css/style.css" rel="stylesheet">';

// THE BELOW LINE STATES THAT IF THE SUBMIT BUTTON
// WAS PUSHED, EXECUTE THE PHP CODE BELOW TO SEND THE 
// MAIL. IF THE BUTTON WAS NOT PRESSED, SKIP TO THE CODE
// BELOW THE "else" STATEMENT (WHICH SHOWS THE FORM INSTEAD).
if ( isset ( $_POST [ 'buttonPressed' ] )){

// REPLACE THE LINE BELOW WITH YOUR E-MAIL ADDRESS.
$to = 'myemail.com' ;
$subject = 'From PHP contact page' ;

// NOT SUGGESTED TO CHANGE THESE VALUES
$message = $_POST [ "message" ] ;
$headers = 'From: ' . $_POST[ "from" ] . PHP_EOL ;
$name = $_POST['name'];
mail ( $to, $subject, $message, $headers ) ;

// THE TEXT IN QUOTES BELOW IS WHAT WILL BE 
// DISPLAYED TO USERS AFTER SUBMITTING THE FORM.

echo '<div id="msg_sent"> Your e-mail has been sent. You should receive a reply within a week. </div>';}

?>

Thank you if you know anything!

展开全部

  • 写回答

1条回答 默认 最新

  • doujinai2183 2014-08-28 09:21
    关注

    "I am using 1and1's mail.php function and there are no instructions on how to do this.
    Is this even possible?"

    Yes, it is possible.

    Add an email form element <input type="text" name="email"> if you don't already have one, then do:

    $email   = $_POST['email'];
    $name    = $_POST['name'];
    $headers = "From: ". $name . " <" . $email . ">
    ";
    

    Mail stands at going to Spam if the From is only a name, instead of containg an Email address.

    You should also use FILTER_VALIDATE_EMAIL for your email variable, in order to validate it.

    I.e.:

    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
      {
      echo "E-mail is not valid.";
      }
    

    Sidenote:
    Rocket's comments make sense in regards to PHP_EOL which is why my example contains .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部