FORM: Name as sender instead of e-mail. - php

I have a very basic form that uses PhpMail to send it's content but is there some way to change the "sender name" of the form from an e-mail adress to a name.
Example:
Instead of "from: form.ello#whatever.org" it says "From: Ello Recruitment" or whatever.
Here's the code i'm currently using:
HTML:
<form name="10_percent" action="http://www.whatever.org/recruit.php" method="post" style="text-align:center;">
<label for="description"><p>Description</p></label>
<textarea name="description" id="description" rows="4" cols="50"></textarea>
<input type="submit" id="submit" name="submit" value="Send" style="width:88px; height:24px;"/>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "xxx#whatever.org";
$subject = "Recruitment";
$epost_field = $_POST['description'];
$headers = "Content-type: text/html; charset=utf-8\r\n";
$headers = 'From: Ello Recruitment' . "\r\n" .
'Reply-To: xxx#whatever.org' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "$description_field\n";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.whatever.org\">";
mail($to, $subject, $body, $headers);
} else {
echo "not working";
}
?>

Marcel was nearly there - you need to change the From header:
$headers = 'From: Ello Recruitment <form.ello#whatever.org>' . "\r\n" .
You may have trouble setting the From address on some servers, for example gmail doesn't let you do this arbitrarily.
I would recommend you use a library to send email from PHP as it will do all this kind for formatting for you correctly.

Related

How to create contact form and mail to the owner of website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos
This is my HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name: <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>
</form>
</body>
</html>
This is my PHP Code
<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk#gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
?>
mail function as described here http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
So the header "From" must be an email address while you are putting a custom name.
How to get mail function delivery mail to 'to' address please read here
It is mostly correct.
You can't pass Mobile as the additional parameter, that will do nothing.
I have changed the script to append the mobile to the message body.
To set a from address, you need to have a from email, just a name by itself won't work.
Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.
<?php
if ($_POST) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$mes = $_POST['mes'];
// append mobile to message
$mes .= "\r\nMobile: $mobile\r\n";
$from_email = "imjeevajk#gmail.com';
mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
// mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
exit;
}
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>
<?php
if (isset($_POST['sendMail'])) {
$to = 'admin#dummyemail.com';
$subject = 'Contact From Site';
$from = "From: ".$_POST["name"]."\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message = "Name: ".$_POST["name"]."";
$message .= "Mobile: ".$_POST["mobile"]."";
$message .= "Message: ".nl2br($_POST["mes"])."";
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo 'Thanks for Contacting Us.';
} else{
echo 'Unable to send email. Please try again.';
}
}
?>
Better use PHP Mailer
some servers providers block mail function and that can be reason why it doesn't work.

PHP message sends "undefined"

I have mail.php which sends out an email. This is the code I'm using.
<?php
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = 'Your Results!';
$message = $_POST['message'];
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail($to, $subject, $message, $headers);
//header("Location: thankyou.php");
echo $message;
}
?>
The contents of the email is supposed to the .innerText of of a div I have on my index.php page. If I echo out the message, then the output appears the page (mail.php) but when I comment/uncomment the necessary parts to email myself the message I receive is just "undefined".
Is $message not defined?
Here is the form and javascript I'm using
<form action="mail.php" method="post" onsubmit="this.message.value = document.getElementById('box').innerText;">
<input type="email" name="email" required>
<input id="content" type="hidden" name="message" value="">
<input type="submit" name="submit">
</form>
Change .innerText to .textContent. .innerText is a nonstandard property that doesn't exist in Firefox.
I don't know why you didn't have the same problem when used echo $message in the PHP script, unless you tested that version of the script with a different browser (always try to minimize the number of differences when you're testing like this).

PHP Email Form More Fields

So I have an email form using PHP:
<form method="post" action="contactus.php" autocomplete="off">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" maxlength="60" required/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" maxlength="120" required/>
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message" required></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
And the PHP to send to my email:
<?php
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail.com', 'Quick Pass', $message, $email);
}
?>
But the problem is only 'myemail.com' (which is my email address), 'Quick Pass' (the email subject) and $message (the email message). $email isn't being sent.
I have tried:
<?php
$to = "myemail.com";
$subject = "Quick Pass";
$message=$_POST["message"];
$email=$_POST["email"];
mail($to,$subject,$message,$email);
?>
With this I am receiving four emails (two with $message and two with $email).
The problem is I need all of this in one email. So the $message is sent along with $email somewhere in the email.
Any ideas? (hopefully using my form)
There is an example, direct from the php manual, which looks as follows:-
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
The fourth parameter is NOT simply an email address - it is a header:value type arrangement with multiple headers separated using \r\n
In your code perhaps you could try:-
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
$headers='From: '.$email."\r\n".
'Reply-To: '.$email;
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail#example.com', 'Quick Pass', $message, $headers );
}
Make sure you getting your form input correctly. then try this.
// recipients
$to = 'to#example.com'; //your email address
$user_email = $_POST["email"]; //input from form
// subject
$subject = 'Subject';
// message
$message = $_POST["message"];
// set conent type
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: User Email <'.$user_email.'>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
As previous posters have said, the fourth parameter id for for headers so it should be "From: {$email}" or similar.
However, mail servers are sometimes configured to ignore From headers and set a default value. Also you could find that the message is being ignored somewhere along the email route (by your host's own relay server or by your email provider's incoming mail server) if the sending address doesn't match any DNS MX records for the domain in question. You will need to talk to your hosting provider(s) to investigate this.

How can i send an e-mail with HTML content [duplicate]

This question already has answers here:
Failed to send HTML mails using PHP mail()
(3 answers)
Closed 8 years ago.
I'm trying to send e-mails with html content.
I want to insert some images on it, but i dont know how can i do that, here my code:
<?php
if(isset($_POST['submit'])){
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: myemail#gmail.com' . "\r\n" .
'Reply-To: '.$to.' ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
<form method="POST">
<input type="text" placeholder="from" name="from" />
<input type="text" placeholder="to" name="to" />
<input type="text" placeholder="Subject" name="subject" />
<textarea type="text" placeholder="Message" name="message"></textarea>
<input name="submit" type="submit" />
</form>
Thanks guys
Just need to pass the appropriate headers to the mail function:
// Send HTML Message
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= 'From: example name <example#example.com>' . PHP_EOL;
mail('to#example.com', 'Subject', $output, $headers);
$to = "$email";
// Change this to your site admin email
$from = "admin#MYSITE.COM";
$subject = "Welcome to MYSITE.COM";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Dear ' . $firstname . ',
<br /><br />
Thank you for joining MYSITE.COM.
<br /><br />
You can now start shopping on our website and enjoy all of our services.
Please click on the link bellow to go to our website >>
http://www.MYSITE.COM
<br /><br />
Your Login Details is as follows:
<br /><br />
E-mail Address: ' . $email . ' <br />
Password: ' . $password . '
<br /><br />
Regards
MYSITE.COM
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
You can edit the code to suit your needs. but it should help you understand how to send an HTML eMAIL directly from PHP.

Php contact form Encoding issue

I have a contact form made with php on my website. The problem is that it perfectly sends english letters but doesn't support russian letters. So, I need to change the encoding, how do i do that?
Here is a code:
<div id="center">
<p class="please">Please contact us using this form.</p>
<div id="formbox">
<?
if (isset ($_POST['message'])) {
$name = # trim ($_POST['name']);
$contact = # trim ($_POST['contact']);
$message = # trim ($_POST['message']);
if (! $name or ! $contact or ! $message) echo ('<p style="color: red">You should fill in all the blanks.</p>');
else { mail ("support#myemail.com",
"Message from Giftosite (Sender: $name)",
"$message \n\n Reply to: \n $contact");
echo ('<p style="color: green">Message has been sent, thank you!</p>');
$_POST['name'] = $_POST['contact'] = $_POST['message'] = '';
}
}
?>
<form method="POST" class="form">
<p class="formcontent">Your name:</p>
<input name="name" value="<?=#$_POST['name'];?>">
<p class="formcontent">Your e-mail address:</p>
<input name="contact" value="<?=#$_POST['contact'];?>">
<p class="formcontent">Message:</p>
<textarea name="message" rows="6" cols="36"><?=#$_POST['message'];?></textarea>
<p><input type="submit" value=" Send "></p>
</form>
</div>
</div>
Set headers in your email
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Content-type: text/html; charset=UTF-8;' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
So, in your code instead of this
mail ("support#myemail.com", $message from Giftosite (Sender: $name)", "$message \n\n Reply to: \n $contact");
You will have
$headers = 'From: webmaster#example.com' . "\r\n" .
'Content-type: text/html; charset=UTF-8;' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail ("support#myemail.com", $message from Giftosite (Sender: $name)", "$message \n\n Reply to: \n $contact", $headers);
You need to set header for your email before sending Unicode characters, try this
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
mail ("support#myemail.com",
"Message from Giftosite (Sender: $name)",
"$message \n\n Reply to: \n $contact", $header_);

Categories