sending a email by the use of a variable - php

I have search for my problem and i know there is a easy fix for it but i just can't figure it out and i keep banging my head against the wall, so here goes:
i have a variable in my config file
$businessemail = "me#myemail.com";
This email changes a lot so i want to use this variable in my mail script which is this
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = '$businessemail'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
Now my question is how do i format the $businessemail in the right way so it can read and sent the message to the email in the config file
Thanks for the help.

$emailTo = $businessemail; // don't use variable in single quotes here

Related

Using PHP's mail() - name.com hosting issue

my site is hosted on name.com, and to test this I uploaded a simple file called contact.php with this at the top.
<?php
$to = '~~~~';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email - $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message, $header);
$feedback = "Sent";
}
?>
So, when I click the submit button it sends the mail. Inside a p tag I have echo $feedback, which shows up after I click submit.
The mail does not send? Anything I'm doing wrong here, or do I need to configure my cPanel in some way?
Since header is optional and is left undefined, removing it should resolve your issue.
Be aware that using the $_POST content directly into a email is a security risk for you!
THere are good email libraries that have tools to avoid abuses. (For example: ZendMail, PHPmailer)
Having said that, on your code you are missing the header and have a small mistake on $email = (not -) $_POST['email'];, you can use this:
$header = 'From: from#name.com' . "\r\n" .
'Reply-To: from#name.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You have not set header variable which is optional, there is a typo while setting $email variable, you've use - instead of =
your updated code,
<?php
$to = 'a#a.com';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message);
$feedback = "Sent";
}
?>

A php mail function regarding the additional parameters for headers

I'm using dreamhost for my mailing.
I'm having an issue with php mail function additional headers parameters.
This code works, and the email is sent:
$to = 'myemail#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $name <**webmaster#example.com**>\r\n" .
"Reply-To: $name <**webmaster#example.com**>\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
but when I replace webmaster#example.com to the variable $email
$headers = "From: $name <**$email**>\r\n" .
"Reply-To: $name <**$email**>\r\n" .
'X-Mailer: PHP/' . phpversion();
The email doesn't get sent. I did do a print_r($_POST), and the elements are there. I also did another test where I typed the email: webmaster#example.com into the form, to see if it would send, and it did. So my question is, how do I remedy this issue, if a user types their email address into the form with another mailing extension, that mail will not get sent, but if the extension is #example.com, then the mail will get sent.
$name variable should be the full emailaddress:
"$name = " 'a name' email#whatever.com> "
$headers = "From: $from \r\n";
works on my servers. Add the leading < to the email addtress. This system won't display the string at all if I include it. If your form has different variables for the responders nam and email address you'll have to concatenate them to get $name in the right formate.

Send email to address from form input

I have a pretty standard contact form with inputs for name, email address and phone number. As well as sending the email to the specified email address as per a standard form,
$to = 'diysoakwells#hotmail.com';
I would also like to send the email to the user using the address from the email address input from the form. I was thinking using the post variable from the email input like this would work:
$to = 'diysoakwells#hotmail.com', $email;
but no luck. Can someone point me in the right directiona and are there any security risks in using this approach? I ultimately aim to provide the user with a checkbox that if checked sends a copy of the email to themselves.
Here is a link to my form
http://www.diysoakwells.com.au/cart.php
Thankyou in advance : )
<?php
include_once("wsp_captcha.php");
if(WSP_CheckImageCode() != "OK") {
header('location:/form-rejected.php');
die();
}
$to = 'diysoakwells#hotmail.com';
$subject = 'Order Inquiry';
$jcitems = " <p><b>ORDER:</b></p><p> " . $_POST['jcitems']."<p/>" . "<p><b>Total:</b> $" . $_POST['jctotal']."</p>";
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: inquiry#diysoakwells.com' . "\r\n" .
'Reply-To: noreply#diysoakwells.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
$phone = $_POST['phone'];
$emaile = $_POST['emaile'];
$textbox = $_POST['textbox'];
$text = "<html><body><p><b>Message:</b>\n$textbox</p><p>This form was submitted on Your Web Site on \n $date at\n $time</p><p><b>Customers Email Address:</b> $emaile</p><p><b>Customers Name:</b> $name </p><p><b>Customers Phone Number:</b> $phone </p></html> </body>";
$body = $text . $jcitems;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
?>
What your doing is not what you want to do. Concatenating two strings in PHP is done with the . not the , so the correct syntax is:
$to = 'diysoakwells#hotmail.com'.", ".$emaile;
or simply
$to = "diysoakwells#hotmail.com, $emaile";
That's assuming that the code in charge of sending the email uses php's mail() function, which allows multiple emails in the $to argument. If that doesn't work, I can't be of more use without seeing the actual code.
The 'to' field on emails accepts a string, with the email address comma-separated.
$to = 'diysoakwell#hotmail.com, ' . $emaile;
should do the trick.
You should check the email address that they provide is formatted as an email address, and it would be a good idea to have a CAPTCHA to prevent automated bots from using your form as a spamming tool.
If you use php mail() function you can send copy by specifying additional headers like that:
$headers = 'Cc: '.$emaile;
mail($to, $subject, $message, $headers);

The way to send duplicate email to the user?

I made simple email in wordpress but it would only send email to the administrator email. I need to find the way to send duplicate email with Thanks to the user.
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = 'I Have A Question to Ask from '.$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone: $phone \n\nComments: $comments";
$headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
Insight is appreciated. Thank you.
looks like you already have the user's email address in $email, right? you used it in the message you showed in the question.
$subject = '(duplicate) I Have A Question to Ask from '.$name;
$body = "This message was sent to the administrator:\n\nName: $name \n\nEmail: $email \n\nPhone: $phone \n\nComments: $comments";
$headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($email, $subject, $body, $headers);
Just add:
wp_mail($email, "Thank you for your message", $body, $headers);

PHP E-Mail Form w/Options

I need to include the first code (which calls the e-mail from a WordPress options panel) in the second code where it says email#mail.com. I tried to include " around the code, but it doesn't seem to work. I also tried to strip slashes, but to no avail.
How I call the e-mail
<?php echo get_option('to_email'); ?>
The relevant part of my contact form:
if(!isset($hasError)) {
$emailTo = 'email#mail.com';
$subject = 'Contact Form Submission from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailTo = get_option('to_email');
You have to make sure the right files are included (wordpress config files).

Categories