Jump to content

Not showing whole name with mail () script


tech4all

Recommended Posts

I sent up a simple mail form with the PHP mail() script. One problem is when it sends an email with the form's contents, the From header only reads the first name and shows @domain.com in the From column.

 

Example:

 

A user inputs "John Smith" into the form as their name.

 

When the email arrives in my inbox it's say "John@yahoo.com", the domain name depends on which email I'm using, could say "John@hotmail.com" if I had a hotmail email address. It basically ignores the "space" and "Smith" in the name input part of the form and replaces it with "@domain.com".

 

How can I get it to put the whole "John Smith" instead? FYI: I'm very new to this, so not sure if this is considered an "easy fix" or not.

 

Thanks!

Link to comment
Share on other sites

No problem

 

FORM CODE:

 

<form action="sendemail.php" method="post" name="contactForm">
    	<table width="400px" height="auto" cellpadding="0" cellspacing="0" border="0">
		<tr>
    			<td width="100px" height="30px" align="right" style="padding-right: 10px;"><span class="emailformtext">Name:</span></td>
       			<td><input name="name" type="text" value="" size="32" maxlength="50"></td>
    		</tr>
    		<tr>
    			<td width="100px" height="30px" align="right" style="padding-right: 10px;"><span class="emailformtext">Email:</span></td>
       			<td><input name="email" type="text" value="" size="32" maxlength="50"></td>
    		</tr>
            <tr>
    			<td width="100px" height="30px" align="right" style="padding-right: 10px;"><span class="emailformtext">Subject:</span></td>
       			<td><input name="subject" type="text" value="" size="32" maxlength="50"></td>
    		</tr>
        	<tr>
	  		<td width="100px" align="right" valign="top" style="padding-right: 10px; padding-top: 8px;"><span class="emailformtext">Message</span></td>
            	<td width="400px" height="100px" valign="top" style="padding-top: 5px;"><textarea name="message" cols="30" rows="10" wrap="soft"></textarea></td>
    		</tr>
       		<tr>
        		<td width="400px" height="25px" colspan="2" style="padding-left:85px; padding-top:10px"><input name="submit" type="submit" value="Send"></td>
      		</tr>
	</table>
    </form>

 

mail () code

 

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  $name = $_REQUEST['name'] ;
  mail( "tech_for_all@yahoo.com", "$subject",
  $message, "From: $name\r\nReply-to: $email" );
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url="../index.htm".'">';
  }
?>

 

Thanks!

 

Link to comment
Share on other sites

I think you need to specify the From: header as a valid formatted email address (with optional display name).  So it should really look like this: "John Doe" <JohnDoe@Mymail.net>.  Otherwise the mail servers processing it are going to take liberties with an invalid email address.

 

Be aware, however, that you might have problems trying to send an email using a "from" address that is not your domain.  Some will consider it a spam, some servers will not send it.  I think it best to use your website's email address in the from (although you can still use the User's display name) "John Doe" <Mywebsite@domain.com> and then put the user's email in the ReplyTo header (as you are doing now).

 

Link to comment
Share on other sites

Thanks for replying.

 

I think you need to specify the From: header as a valid formatted email address (with optional display name).  So it should really look like this: "John Doe" <JohnDoe@Mymail.net>.  Otherwise the mail servers processing it are going to take liberties with an invalid email address.

 

Which part of the code would I alter? Right now the "$From name", in the PHP code, isn't connected to the email address but rather their name. And the "tech_for_all@yahoo.com" is the email address that it will send the form to (my email address). So I guess I'm not understanding how the way you formatted it ("John Doe" <JohnDoe@Mymail.net> goes in the code in terms of eliminating the "@domian.com" after the First name when I receive the email. The user's email address will be in the Reply-To: header, which does work.

 

Maybe you could get the PHP code I posted, and do the fix so I could compare it to mine?

 

I realize you probably know much more than me, so I apologize if didn't understand your instructions completely.

 

Be aware, however, that you might have problems trying to send an email using a "from" address that is not your domain.  Some will consider it a spam, some servers will not send it.  I think it best to use your website's email address in the from (although you can still use the User's display name) "John Doe" <Mywebsite@domain.com> and then put the user's email in the ReplyTo header (as you are doing now).

 

The yahoo email address I have right now is just a test email. All email from this form will go to my own email address for my own site. Is that what you were referring to?

Link to comment
Share on other sites

The 'From' header needs to be set to a valid email address on the server that is sending the email (the domain this script resides on) not the name or address of user who fills in the form.

 

Is it then not possible to have a user's name be in the 'From' header without the @domain.com? The 'From' header has to be an email address?

 

If that is the case, then when I fill out other forms on websites that ask for my name, where does my name go to in their email?

 

Thanks!

Link to comment
Share on other sites

If that is the case, then when I fill out other forms on websites that ask for my name, where does my name go to in their email?

 

Likely it goes within the message body.

 

The email is being sent From your website, not the person filling in the form.

 

Otherwise, it would be simple for me to send an email and make it look like it came from anyone I like. While this is indeed possible (and what you are actually attempting), most mail servers will reject such emails as spam.

Link to comment
Share on other sites

It is true that the From header of an email message needs to have a valid email address.  Your system's mail handler sees the name you put there (for instance, "John") and is trying to turn it into a valid address.  So it is adding the domain your server is configured to send mail from, so you end up with John@yahoo.com (or whatever).  You need to specify an email address associated with your server.  However, you can add a display name to that.  Some (most?) email clients will display this part rather than the actual email address if it is specified correctly.  You can try it like this:

$siteEmail = 'mySite@yahoo.com'; // or whatever your website's designated email address is
mail( "tech_for_all@yahoo.com", "$subject",
  $message, "From: \"$name\" <$siteEMail>\r\nReply-to: $email" );

Do not put the user's email address in the From:, since, as thorpe said, most mail servers will treat it as spam, and some will just refuse to send it.  But you can cause the client to display just about anything in the from column.

Link to comment
Share on other sites

$message = "Name: $namesend ;  Home Phone: $homesend ;  Cell Phone: $cellsend ;  Email: $emailsend ;  Time & Date: $visitTime ; IP: $ip";
$emailsend = "$_GET['firstname']" . "$_GET['lastname']" . "@" . "yahoo" . ".com"
$headers = "From: $emailsend";

mail( "richard@studio378d.com", [color=red]"Cobra Request Form"[/color],  $message , $headers );

the red indicates the subject portion. you have limited resources in the mail( so you need to construct the information before hand into a $var then put it in the mail( . i put in some stuf to give example but im thinking puting the name together as a way of obtaining email address is not so reliable and ive never heard of an email with a space. hope this helps you anyway.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.