PHP E-mail Form Sender Name Instead Of E-mail? - php

Right now, I've set up a PHP e-mail form and everything works fine. However, while testing it, I'm only getting the sender e-mail address as the name. What I want is the name of the sender, like:
John Doe
Subject Subject Subject
Instead of:
j.doe#website.com
Subject Subject Subject
Below is the code...
Can someone help me please? Thanks.
PHP:
<?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
}
?>

Right after your 4 lines that say $email_message .= ... add a line:
$email_from = $full_name.'<'.$email_from.'>';

THen you need it in this form:
"John Doe <j.doe#website.com>"

Usualy you simply need to reformat the from address like this:
"Ricky, Dear" <dearricky#yahoo.com>
http://php.net/manual/en/function.mail.php

In the headers you must set the From header like this:
<?php
...
$headers = 'From: ' . clean_string($full_name) . ' <' . clean_string($email_from) . ">\r\n";
..
?>

Thank you stimms!
So, your whole code portion would look like this:
// prepare email body text
$message = "";
$message .= "Thanks for registering .....";
$message .= "\n";
$message .= "More stuff here";
$headers = "From: John Smith <info#yourcoolsite.com>";
mail($recipientemail, "Hey thanks for joining!", $message, $headers);
Then the email recipient will see an email in their inbox that looks like this:
FROM: John Smith
SUBJECT: Hey thanks for joining!
MESSAGE: Thanks for registering ..... More stuff here

You set the e-mail address like this
$email_to = "Ricky <ricky#rickytsang.ca>";
http://php.net/manual/en/function.mail.php

change these 4 lines
$full_name = $_POST['full_name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= $_POST['message'];
to:
$full_name = $_POST['full_name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= '"' . $full_name . '" <' . $_POST['message'] . '>';
please make sure u copy paste this as it is.

Drupal e-mail Form Sender Name Instead Of E-mail with Domain Basis?
Example:
function hook_mail_alter(&$message) {
if($message['id'] == "put_id" && $domain_id == 368){
$default_from = variable_get('site_mail', ini_get('sendmail_from'));
if($message['from'] == $default_from){
$message['from'] = '"'. variable_get('site_name', 'Drupal') .'" <'.
$default_from .'>';
$message['headers']['From'] = $message['from'];
}
}
}

Related

PHP Contact Form - Sending email to multiple recipients

How would I go about making this form able to send to multiple recipients? At the moment it's only allowing me to send it to 1 email only, and when I try typing multiple in (e.g "user1#example.com, user2#example.com") it returns the message for when it's invalid. What do I need to do to fix this?
EDIT: The user has to input the email address it wants to sent to, but it only works with 1, which is why I'm asking for help on how I can edit the code to work with multiple emails/recipients and to be separated with a comma and space.
Here is the code
<?php
if(isset($_POST['email'])) {
$email_to = array($_GET["celebrant_emails"]);
$email_subject = "Email from website Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the email you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go <a href='http://celebrantsaustralia.asn.au/celebrants-trial.htm'>back</a> and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments']) ||
!isset($_POST['celebrant_emails'])) {
died('We are sorry, but there appears to be a problem with the email you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$celebrant_emails = $_POST['celebrant_emails']; // 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 Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message you entered does not appear to be valid.<br />';
}
if(!preg_match($email_exp,$celebrant_emails)) {
$error_message .= 'The Celebrant Email(s) you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Email details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\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 -->
<html>
<head>
<title>Email sent!</title>
</head>
<body>
<b>Thank you for contacting us. We will be in touch with you shortly.</b>
<p>(Page will auto-direct in a moment, if it doesn't click here)</p>
</body>
</html>
<?php
}
?>
There are numerous ways of doing this.
$email_to = "jhewitt#amleo.com,some#other.com,yet#another.net";
$email_to = 'Mary <mary#example.com>, Kelly <kelly#example.com>';
If you need to add emails as CC or BCC, add the following part in the variable you use as for your header :
$headers .= "CC: sombodyelse#noplace.com".PHP_EOL;
$headers .= "BCC: hidden#special.com".PHP_EOL;
Source: PHP form send email to multiple recipients
Also check here: Example #1 Sending mail.
$email=$_POST['email']; // write multiple emails with comma's
$emails=explode(',', $email); // explode email with comma's
foreach($emails as $one_email)
{
$touser=$one_email;
$subjectAdmin= "subject";
$headersAdmin = "From: noreply#talentswype.com\r\n";
$headersAdmin .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$messageuser ='msg here';
$emailSenduser = mail($touser,$subjectAdmin,$messageuser,$headersAdmin);
}
Write one mail function per email.
mail('example1#example.com', 'My Subject', $message);
mail('example2#example.com', 'My Subject', $message);
mail('example3#example.com', 'My Subject', $message);
mail('example4#example.com', 'My Subject', $message);
mail('example5#example.com', 'My Subject', $message);
or
foreach($mail as $mails ){
mail($mail, 'My Subject', $message);
}

Sending email two multiple recipients but different body using PHP [duplicate]

This question already has answers here:
How to do email form with multiple recipients and different body?
(3 answers)
Closed 9 years ago.
I have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin#gmail.com";
$email_subject = "Lifemailer Sales Enquiry";
$email_to_user= "Name: ".clean_string($name)."\n";
function died($error) {
// your error code can go here
$URL = "error.html";
header("Location: $URL");
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['contact']) ||
!isset($_POST['email']) ||
!isset($_POST['email_sub']) ||
!isset($_POST['remarks'])) {
died('We are sorry, but there appears to be a problem with the form your
submitted.');
}
$name = $_POST['name']; // not required
$contact = $_POST['contact']; // required
$email = $_POST['email']; // required
$email_sub = $_POST['email_sub']; // required
$remarks = $_POST['remarks']; // required
$error_message = "";
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
$string_exp = "^[0-9 .-]+$";
if(!eregi($string_exp,$contact)) {
$error_message .= 'The Contact Number you entered does not appear to be valid.
<br />';
}
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email)) {
$error_message .= 'The Email Address you entered does 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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Contact Number: ".clean_string($contact)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Email Subject : ".clean_string($email_sub)."\n";
$email_message .= "Remarks/Enquiry : ".clean_string($remarks)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
$URL = "thank-you.html";
header("Location: $URL");
?>
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
In the same way you can send a second mail with different subject, email_to, email_message after sending it to admin.
just concatenate
$email_to = ' admin#gmail.com ' . ',' ;
$email_to . = ' admin#gmail.com ' ;

email with php not sending email

I am trying to send an email with php everything seems to work but i dont get the email. No errors come up i just dont receive the email. any suggestions? here is my code:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>mail</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail#gmail.com";
$email_subject = "MythMedia Account Reqe";
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['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email'])
) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Password: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.Mythmedia."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you, I will process your account soon™<br>
Login
<?php
}
?>
Any help is greatly appreciated!
please check with this line
$headers = 'From: '.Mythmedia."\r\n".
I think you have to do something like
$Mythmedia='Mythmedia';
$headers = 'From: '.$Mythmedia."\r\n".
Or
$headers = 'From: Mythmedia \r\n'.

Unknown Sender PHP (Code inside)

I've created a PHP file to work with my form on my jQuery mobile site. The form works perfectly and sends the email and errors work and all. But I keep getting the email from an (unknown sender). Subject line and email information is there. (Email coming from the form to host the host email address). Thanks for any help that can be provided.
<?php
if(isset($_POST['email'])){
// Here is the email to information
$email_to = "hostemail#email.com";
$email_subject = "Customer Service Form";
$email_from = "Company";
//error code
function died($error){
echo "We are sorry, but there were errors found with the form you submitted.";
echo "These errors appear bellow.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
//$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z] {2,4}$/';
//(!preg_match($email_exp, $email)) {
//$error_message .='The Email Address you entered does not appear to be valid.<br/>';
// }
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not seem to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The comments you entered do 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 .= "Name:" . clean_string ($name) . "\n";
$email_message .= "E-Mail:" . clean_string ($email) . "\n";
$email_message .= "Message:" . clean_string ($message) . "\n";
//create email headers
$headers = 'From:' .$email_From . "\r\n" . 'Reply-To:' . $email. "\r\n" .
'X-MAILER: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success Message Goes Here -->
Thank you for contacting us. We will be in touch with you shortly. <br/>
Please Click here to go back to the contact page.
<?php
}
?>
The format for the From header is:
Display Name <email address>
For example:
Company <foo#company.com>
Right now, you're just using "Company", which is neither a valid e-mail address on its own, nor has an e-mail address at the end.

PHP: Form submission asnwer as a part of the email subject

So i am fairly new to PHP (worked with it a little, not much though). I have a form submission that sends to my email. The thing is, i want to have one of the answers in my subject line. example: they choose bug, i want my subject to be "Site: Bug", or "Site: Other" depending on what they pick for the subject in the form.
<?php
if(isset($_POST['email'])) {
$email_to = "email#email.com";
$email_subject = "Site: Submission";
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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // drop down menu
$comments = $_POST['comments']; // 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 Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do 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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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);
?>
Thank you for you submission.
<?php
}
die();
?>
Any help is appreciated. Sorry if this is a easy question, but i couldnt find an answer using google.
Assuming you want the contents of $subject in the email subject, put something like this just before email headers:
[...]
if ( isset( $subject ) ) {
$email_subject = 'Site: '.$subject;
}
// create email headers
[...]
if i understood you correctly - just add it to the subject:
$subject = $_POST['subject']; // drop down menu
$email_subject = "Site: ".$subject;
first get $subject then add it to $email_subject
Add something like this to your form.
<select name="subject">
<option value="Site: Bug">Bug</option>
<option value="Site: Other">Other</option>
</select>

Categories