Jump to content

how to send mail with name and email?


mark103

Recommended Posts

Hi guys,

 

I have a problem with the $header variable. I am trying to extract the value from the $name method and included with the $email method.

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'myusername');
    define('DB_PASSWORD', 'mypass');
    define('DB_DATABASE', 'mydbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $name = clean($_GET['name']);
    $email = clean($_GET['email']);
    $type = clean($_GET['type']);
    $comments = clean($_GET['comments']);


if($name == ''){
   $errmsg_arr[] = 'name are missing.';
   $errflag = true;
} elseif($email == ''){
   $errmsg_arr[] = 'email are missing.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {
$insert = array();
if(isset($_GET['name'])) {
    $insert[] = 'name = \'' . clean($_GET['name']) .'\'';
}
if(isset($_GET['email'])) {
    $insert[] = 'email = \'' . clean($_GET['email']) . '\'';
}
if(isset($_GET['type'])) {
    $insert[] = 'type = \'' . clean($_GET['type']) . '\'';
}
if(isset($_GET['comments'])) {
    $insert[] = 'comments = \'' . clean($_GET['comments']) . '\'';
}

if (count($insert)>0) {
   $names = implode(',',$insert);


if(isset($email)){
   $name = $_GET['name'];
   $email = $_GET['email'];
   $header = 'From: $name <-f $email>' . "\r\n";  
   $to = "myname@myemail.com";
   $subject = $type;
   $message = "$comments";
   $header .= "MIME-Version: 1.0\l\n";
   mail($to, $subject, $message, $header);
echo "Thank you for sent us your email";
  }
}
}
?>

 

 

Something got to do with this line:

 

$header = 'From: $name <-f $email>' . "\r\n";

 

My problem is I can see in my email that the sender name has been included with (unknown sender) while the email address are display as empty address. I am really not sure why I have got the blank sender name and with the blank sender address.

 

 

Do you know what the main problem is and what I need to change it with?

 

Any advice would be much appreciate.

 

Thanks,

Mark

Link to comment
Share on other sites

yes, sorry it was my mistake. However, I have got an email that come with a $name as sender name and <$email@.myserver.comname> as my email.

 

I have input the values into $name as sender name and $email as from sender email address.

 

Do you know what the problem is?

 

Here's the update code:

 

if(isset($email)){
   $name = $_GET['name'];
   $email = $_GET['email'];
   $header = 'From: $name <$email>' . "\r\n";  
   $to = "myname@myemail.com";
   $subject = $type;
   $message = "$comments & $rate";
   $header .= "MIME-Version: 1.0\l\n";
   mail($to, $subject, $message, $header);
echo "Thank you for sent us your email";
  }

Link to comment
Share on other sites

try using double quotes

 

$header = "From: $name etc";

 

it should work, cuz a variable only works in double quote, not in a single quote

 

good catch!

 

ay caramba! such a simple thing, and I missed it, even using single-quotes in my suggestion...

 

oh yeah :D

 

but lets wait for the mark's answer

Link to comment
Share on other sites

Thanks for the help guys. I can see the problem is fixed. However, how i can hide the mailed-by server name, something is like myservername.com. How I can get rid it??

 

here's the update code:

 

if(isset($email)){
   $name = $_GET['name'];
   $email = $_GET['email'];
   $header = "From: $name <$email>". "\r\n";  
   $to = "myname@myemail.com";
   $subject = $type;
   $message = "$comments & $rate";
   $header .= "MIME-Version: 1.0\l\n";
   $add = "$name <$email>";
   mail($to, $subject, $message, $header, $add);
echo "Thank you for sent us your email";
  }

Link to comment
Share on other sites

not sure it can be done.

 

warning: you might find that if you use a return address that has a different domain than the sending server your email will be bounced back, put into the spam box, and/or cause your server to be blacklisted as a spam server. i suggest that you use the actual server domain for outbound emails.

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.