Adding an extra field to mail() [duplicate] - php

This question already has an answer here:
Extra field in this contact form [closed]
(1 answer)
Closed 9 years ago.
I tried asking this before but I think I made things too complicated and nobody answered. This is take two. I need to add a phone number field to this line of PHP. I have NO IDEA how to add it.
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
I've tried this, but the email never goes through.
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['phone'], $_POST['text'], "From:" . $_POST['email'] );
Here's the full PHP if you need it.
<?php
if ( isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
}
?>
Thanks :)

Your code should be like this
Your are doing wrong way.Add proper subject and message.Please study this link for details.
$subject = 'the subject';
$message = 'Your subject and add phone no here';
mail('contact#jeremyblaze.com', $subject, $message);

Here is the php mail function page:
http://php.net/manual/en/function.mail.php
You cannot add the phone number inside the function like that.
Here is the basic function:
mail(email,subject,body);
You need to add the phone number to the body of the text:
$email = "contact#jeremyblaze.com";
$subject = "Contact Form: ".$_POST['name'];
$body = "From: ".$_POST['email']."\n\r\n\rPhone: ".$_POST['phone']."\n\r\n\r".$_POST['text'];
mail($email,$subject,$body);
You should really look at the documentation on the php site:
http://php.net/manual/en/function.mail.php
It will show you how to set additional headers for reply-to and from, etc. But what I gave you should make what you are trying to do.

Just to complement #Mahmood Rehman info, if you want send a HTML e-mail you can do this:
// From
$email_from = $vEmail;
// To
$to = 'contact#jeremyblaze.com';
// Subject
$subject = $vSubject;
// HTML Message
$message = "<html>
<head>
<title>Title</title>
</head>
<p><b>$vName</b> send the follow message:</p>
<p><b>Subject:</b> $vSubject</p>
<p><b>Message:</b> $vMessage </p>
<p><b>E-mail:</b> $vEmail </p>
<p><b>Phone:</b> $vPhone </p>
</body>
</html>";
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: $email_from\r\n";
// Mail it
mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);

Related

Contact form email sends, but the message is in the subject [duplicate]

This question already has an answer here:
php form sending email, but not in correct way [closed]
(1 answer)
Closed 5 years ago.
My contact form on my web page finally send emails, but it doesn't send it in the proper format. This is the email of what I am getting. I've hidden the email and organization for privacy reasons.
From-- norgun
Subject-- Test Email: ***#gmail.com Message: Sbsbdb
Message--
MIME-Version: 1.0
Content-type: text/plain; charset=iso-8859-1
From: Test <***#gmail.com>
Reply-To: <***#gmail.com>
X-Mailer: PHP/7.0.21
Anyway, as you can see from the above, the name, email, and message content that the person would've written in the contact form are in the subject line instead of the actual email box. Is there a way that I could format my code so that the message and the name that they wrote down are in the message box instead of the subject box, and the email gets sent from the person who put their email in the form, not "norgun" which is what I came up with for the website?
Here is my code so far:
<?php
$to = 'index#indexmarkets.biz';
$name = !empty($_POST['name']) ? filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING) : '';
$from = !empty($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : $to;
$message = !empty($_POST['message']) ? filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING) : '';
$body = "Name: {$name}\r\nEmail: {$from}\r\nMessage: {$message}";
$body = wordwrap($body, 70, "\r\n");
$headers = [
'MIME-Version: 1.0',
'Content-type: text/plain; charset=iso-8859-1',
"From: $name <$from>",
"Reply-To: <$from>",
'X-Mailer: PHP/' .phpversion()
];
$success = mail($to, $body, implode("\r\n", $headers));
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
die(json_encode(['success' => $success]));
}
echo $success ? 'Sent Successfully.' : 'An error occurred';
ini_set('display_errors', 1); error_reporting(E_ALL);
That's because you're passing your variable $body as the subject parameter to the mail() function. This line:
$success = mail($to, $body, implode("\r\n", $headers));
Should instead be something like this:
$success = mail($to, $subject, $body, implode("\r\n", $headers));
Make sure you set $subject to something.

Changing CGI-Mailer in PHP mail script [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a php script that sends an email from an HTML form. The issue is that the sender shows as CGI-Mailer in my inbox.
How can I set the sender address to be that of the sender and not CGI-Mailer?
<?php session_start();
if(isset($_POST['Submit'])) {
$youremail = 'info#complexny.com';
$fromsubject = $_POST['fname'];
$subject = $_POST['fname'];
$fname = $_POST['fname'];
$url = $_POST['url'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$headers = "From: $mail \n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$message = $_POST['message'];
$to = $youremail;
$subject = ''.$fromsubject. ' is interested in a project with you.';
$body = '
Client: '.$fname.'
Phone Number: '.$phone.'
URL: '.$url.'
E-mail: '.$mail.'
Message:
'.$message.'
';
echo "<p style='text-align:center'>Thank you for your feedback. We will be in contact shortly.<br/>Continue to <a href='/'>The Company/a></p>";
mail($to, $subject, $body);
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You are not passing the additional_headers parameter to the mail function. Change the line with the call to mail to:
mail($to, $subject, $body, $headers);
I would suggest using PHPMailer rather than mail(). You can see how to do what you're after in the answer to this question: Setting replyTo field in email
Quoting from that question:
$this->phpmailer->AddReplyTo($replyEmail,$fromName); //this is email2#example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1#example.com
You can find more info on PHPMailer here: https://github.com/PHPMailer/PHPMailer

Wordpress not recieving contact form email

I have a website I'm working on with a contact form. Simple Name, Email, and Message fields. However, I'm not receiving the email from the form; though I do get the "Message Sent" success option.
I had found a way to test wp_mail to see if that was it, using https://gist.github.com/butlerblog/5c9b805529c419b81447#file-test_wp_mail-php and that works just fine - so I have no clue what the problem is.
<?php
//loading wordpress functions
require( '../../../wp-load.php' );
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
?>
As I said, it does hit
wp_redirect($return.'?sent=success')
so I have no clue what is wrong.
EDIT:
Did a variable dump
To: uriah.h.brown#gmail.com
Subject: E-Mail from HollyBrown.Net
From: TestName
Name: test#yahoo.com
Message: TestMessage
Headers: From: test#yahoo.com Reply-To: E-Mail from HollyBrown.Net
Return: http://www.hollybrown.net/contact/
Sitename: The Artwork of Holly Brown
Body: You received e-mail from TestName [test#yahoo.com] using The Artwork of Holly Brown TestMessage
Send: 1
Warning: Cannot modify header information - headers already sent by (output started at /home/panoramicpanda/hollybrown.net/wp-content/themes/widely/sendcontact.php:21) in /home/panoramicpanda/hollybrown.net/wp-includes/pluggable.php on line 1178
The header already sent error leads me to believe output has already been started before you try and send the headers. In this case, the redirect won't work. Try this instead:
/loading wordpress functions
require( '../../../wp-load.php' );
ob_start(); // Starts data buffer.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
ob_end_clean();// Purges data in buffer.
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
Notice the ob_start and ob_end_clean. This will throw your output into the "buffer" and output it clean. The reason this is a problem, is because if any markup is sent BEFORE your sendmail (ie: html generated through templates etc, then the headers have already been started. At that point, unless your server configuration specifically allows you to modify already sent headers, then you will get the error you are getting. Using output buffering allows you to send the data as you intend.

Can you insert an if statement in php email

// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.com';
$body = HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var

PHP tweaking on a mail form

I got some help with an email form, and I feel that I am almost there as the script sends an email but need a few tweaks before I put the form up. Here is my code right now:
index.html:
<div id="main">
<form method="post" action="mailer.php">
<div id="text">
Please enter your email address.
</div>
<input type="text" name="q" id="search" />
<input type="submit" name="submit" id="submit" value="Go!" />
</form>
</div>
mailer.php:
<?php
$email = addcslashes($_REQUEST['q']) ;
if ($email==FALSE){
echo "You forgot to enter your email";
}
else
mail( "example#gmail.com", "E-Mail entered",
"E-Mail entered: $email");
header( "Location: http://www.example.com/thankyou.html" );
?>
A few issues I am running into:
The email being sent does not actually include the email entered, the email comes from Apache#ipaddress.ec2.internal and the Body is Email Entered:
which does not include the email string - is there something buggy with the code?
Also, my if statement doesn't seem to work. Even if I leave the box black, it still assumes a valid email address was sent.
Finally, is there a parameter that sees if the address is in the correct format? ie: includes the # the . and the domain?
Many thanks for any help!
First off, you can use filter_var($email, FILTER_VALIDATE_EMAIL) to test the submitted address. This function returns false if it's not valid. Second, mail() requires a 4th parameter to assign a return address in your message header. Here's an example:
mail(
'to#address.com',
'Subject',
'Message Body',
'From: from#address.com'
)
Regarding your if/else statement, test $_POST['q'] == NULL first, then change $email = addcslashes($_REQUEST['q']); to $email = str_replace(array('\'', '"'), '', $_POST['q']); - no real reason to escape characters in this case. Just take em' out.
Edit: This is what your code should look like:
$email = str_replace(array('\'', '"'), '', $_POST['q']);
$isValid = filter_var($email, FILTER_VALIDATE_EMAIL);
if ( $_POST['q'] == NULL ) {
echo "You forgot to enter your email";
} elseif ( $isValid == FALSE ) {
echo "Please enter a valid email address";
} else {
mail(
'example#gmail.com', // Your address that you want the message sent to
'Subject',
'Message Body',
'From: ' . $email // The address collected
);
header( "Location: http://www.example.com/thankyou.html" );
}
Does that make a little more sense?
Do note that because of modern spam filters, this method may not make it to every recipient. Creating useful email headers can be a bit of an art-form that takes some practice.
To change the "from field", try something like this:
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
As far as your if statement, check the $_REQUEST variable first:
if (isempty($_REQUEST['q']) { echo "forgot to enter email"; }
Finally, here's a link to a method to validate email addresses: http://www.linuxjournal.com/article/9585
Try this for checking if the email is set.
if(isset($_REQUEST['q'])) ..
As far as the "Email Sender". I believe you are looking for the "From" header.. that can be done like this..
$header = "From: Your Name <example#gmail.com>";
mail($to, $subject, $message, $header);
More details are available at http://us3.php.net/manual/en/function.mail.php

Categories