Can you insert an if statement in php email - php

// 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

Related

Adding an extra field to mail() [duplicate]

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);

send an email using data the user has given in a form?

I have a contact form for my website and am hoping to modify it so that a confirmation email is sent to the user when they click submit. Can anybody advise me on the best way to do this?
My php is pretty simple:
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" &&
$msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$EmailFrom = "someone#somewhere.co.uk";
$EmailTo = "someone#somewhere.co.uk";
$Subject = "Online contact form";
$full_name = Trim(stripslashes($_POST['full_name']));
$Phone_Num = Trim(stripslashes($_POST['Phone_Num']));
$email_addr = Trim(stripslashes($_POST['email_addr']));
}
// prepare email body text
$Body = "";
$Body .= "full_name: ";
$Body .= $full_name;
$Body .= "\n";
$Body .= "Phone_Num: ";
$Body .= $Phone_Num;
$Body .= "\n";
$Body .= "email_addr: ";
$Body .= $email_addr;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
?>
First we get the mailto function to work with localhost and email client:
Check this link on stackoverflow:
URL link: send email to owner by collecting information provided by user through form?
Then I recommend using Swiftmailer. http://swiftmailer.org/docs/sending.html They got the best manual.
As others have already suggested the "if" statement is useless since it is achieving nothing. I think your idea was to verify if the fields that you have in your contact form are filled or not. If the fields are unavailable you should throw some error which you are not doing.
Also, if you are to send a confirmation mail to the user who clicks the submit button then the $EmailTo variable should take the email from $msg_email or $msg2_email which according to the code is not done here.
Check this simple snippet this might help you:
if ($_POST['submit']) {
if ($_POST['name1] == '')
echo "Please provide the first name";
if ($_POST['name2] == '')
echo "Please provide the last name";
if ($_POST['email1] == '')
echo "Please provide the email";
if ($_POST['email2] == '')
echo "Please provide the alternate email";
if ($_POST['message] == '')
echo "Please provide the Message";
//Send email to your inquiry mail with above details
// Confirmation mail
$message =<<<EOM
Dear $_POST['name1'],
Thank you for your inquiry.
Our executives will get back to you ASAP.
Thanks,
Sales
EOM;
$to = $_POST['email1'];
$subject = "Acknowledgement of Inquiry";
$headers = "Content-Type: text-plain";
$headers .= "From: sales#yourcompany.co.uk";
mail($to, $subject, $message, $headers);
}

E-mail parsing addresses with junk characters

I'm sending e-mails through a form I coded and for some reason, for some cases the e-mail address becomes junk, and for other times it works fine.
//on form page
$message = str_replace("#e",$emtemail,$message);
$message is stored in SQL (defined on another page), same for $emtemail. $message is just the body of the e-mail being sent, and I'm replacing all instances of #e with the e-mail people send payments to. It sends one e-mail to the customer, and one e-mail to me.
//customer e-mail
//the display address might appear as payment52.62gmail.com instead of payment#gmail.com
//my e-mail
//all e-mail addresses formatted properly without error, # appears as #
Why do e-mail addresses parse strangely? Something to do with encoding?
This is all of the code relevant to sending e-mails I have. I can't pinpoint the problem.
//any variables used in the below but not declared are previously initialized
$em = $userc["email"];
$subject = $emailone["subject"];
$subject = str_replace("#o",$ordernum,$subject);
$subject = str_replace("#u",htmlspecialchars($rn),$subject);
$subject = str_replace("#g",$gt,$subject);
$subject = str_replace("#sl","www.SZVapor.com",$subject);
$subject = str_replace("#ss","SZVapor.com",$subject);
$subject = str_replace("#st","SZVapor",$subject);
$message = nl2br($emailone["message"]);
$message = str_replace("#o",$ordernum,$message);
$message = str_replace("#u",htmlspecialchars($rn),$message);
$message = str_replace("#t",$table,$message);
$message = str_replace("#e",$emtemail,$message);
$message = str_replace("#g",$gt,$message);
$message = str_replace("#a",$addrsubmit,$message);
$message = str_replace("#sl","www.SZVapor.com",$message);
$message = str_replace("#ss","SZVapor.com",$message);
$message = str_replace("#st","SZVapor",$message);
$message = str_replace("#c",$em,$message);
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
$headers .= "From: no-reply#SZVapor.com";
mail($em, $subject, $message, $headers);
Some examples of e-mail addresses given:
payment62.44gmail.com
payment54.45gmail.com
payment22.59gmail.com
payment25.49gmail.com
http://php.net/manual/en/function.mail.php
The first parameter entry for mail is the email address.
The code above does not do anything with $em
And by the way, I think that the last header line should look like this:
$headers .= "From: no-reply#SZVapor.com" . "\r\n";
I solved the issue, and it was an error on my part. I was replacing all instances of certain character combinations with variables.
#g = replaced with grand total
#e = replaced with payment e-mail
#o = replaced with order number
etc
The order I did them in was such that I replaced #e with the e-mail payment#gmail.com, and then replaced all #g with the grand total, so payment*#g*mail.com became "payment".$grandtotal."mail.com".

HTML Email using PHP script

I want an email to be sent in HTML and inside this email i need to pass data from php form. The email is sent successfully as an html email but it doesn't display data passed from PHP VARIABLE.
$name = "PHP TEXT";
$headers = "From: " ."mail#example.com" . "\r\n";
$headers .= "Reply-To: ". "mail#example.com" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
$email_to = 'example#mail.com'; //the address to which the email will be sent
$subject = "test";
$email = "example#mail.com";
if (mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // success
} else {
echo 'failed'; // failure
}
This line:
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
it should be like:
$message = "<html><body><h1>HTML TEXT $name </h1></body></html>";
or
$message = "<html><body><h1>HTML TEXT " . $name . " </h1></body></html>";
<?php and ?> is used to specify that text inside it is a PHP code, not HTML. Here are some details about that.
But you are already in PHP code block and instead of specifying another PHP block, you should use string concatenation.
you aren't using any of the form values in your script. do you know how $_POST or $_GET work?
you are setting $name to a hardcoded value, so it will always be that value when you use it, and also, since you are creating the string with PHP, you don't need to echo php from within this line. i.e. (since you are using double quotes)
$message = "<html><body><h1>HTML TEXT $name;</h1></body></html>";
so, if you can post your HTML form, your $name assignment will probably be something like
$name = $_POST['name']
but you are strongly encouraged to sanitize and validate all user supplied info. It should be a fairly simple Google search.
An alternative solution can be closing the " before like
$message = "<html><body><h1>HTML TEXT ".$name." how are you?</h1></body></html>";
No need for the starting and closing of PHP as you are already in PHP workspace.
Try this..
$message = "<html><body><h1>HTML TEXT".$name."</h1></body></html>";
You are assigning the html to a php variable. So you don't have to echo the variable there.. Just use proper concatination
The $message variable itself has been used wrongly.
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
It should be either
$message = "<html><body><h1>HTML TEXT $name </h1></body></html>";
or this:
$message = "<html><body><h1>HTML TEXT ".$name." </h1></body></html>";

Correctly encode characters in a PHP mail form ("I'm" turns to be "I\'m")

I'm testing a PHP mail form, a very barebones one, found here:
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h3>Thanks for your message</h3>';
//Deal with the email
$to = 'mymail#mail.com';
$subject = 'you have a mail';
$contactname = strip_tags($_POST['contactname']);
$adress = strip_tags($_POST['adress']);
$contactemail = strip_tags($_POST['contactemail']);
$textmessage = strip_tags($_POST['textmessage']);
$boundary =md5(date('r', time()));
$headers = "From: My Site\r\nReply-To: webmaster#mysite.com";
$message = "Name: ".$contactname."\n";
$message .= "Adress: ".$adress."\n";
$message .= "E-mail: ".$contactemail."\n";
$message .= "Message: ".$textmessage."\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is I'm receiving an unwanted slash "\" everytime I write a single or a double quote in my message, so "I'm" appear as "I\'m" in my mailbox.
I know it have to do with the way PHP distinguishes code quotes from only lecture quotes, but I wouldn't know what to add in my form to get it properly running.
Any help is appreciated,
The easiest thing to do is to turn magic quotes off in php.ini,
magic_quotes_gpc=false
If you can't do that, you need to remove slashes like this,
if (get_magic_quotes_gpc()) {
foreach($_POST as $k => $v) {
$_POST[$k] = stripslashes($v);
}
}
you can try stripslashing your message , something like :
$message = stripslashes($message);

Categories