if statement breaking my generated email - php

I have the following email generated automatically. When it is triggered and I am sent an email, I only get "MESSAGE PART 1". The if statement, and everything after the if statement (including REST OF MESSAGE) never gets sent. How can this be done?
/*----SEND EMAIL TO STUDENT----*/
$to = $studentEmail;
$subject = 'Title';
$message = 'Dear '.$studentFirstName.',
MESSAGE PART 1';
if ($bubbleWrap == true) {
'Bubble Wrap ($5)';
}
'REST OF MESSAGE';
$headers = 'From: CustomerService#GuysAndDollies.com' . "\r\n" .
'Reply-To: customerservice#GuysAndDollies.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

You need to concatenate the remaining strings with the original:
$message = 'Dear '.$studentFirstName.', MESSAGE PART 1';
if ($bubbleWrap == true) {
$message .= 'Bubble Wrap ($5)';
}
$message .= 'REST OF MESSAGE';
Currently you just have two string literals which will do absolutely nothing on their own.

Related

Using die() after mail() causes email not to send

I have the code below and the redirect works. However when I have the die() command at the bottom the email doesn't get sent. The email goes fine without the die() command.
Is there a way I can stop the php script continuing without stopping the email from working?
$to = 'hello#nospam.com';
$subject = "Test Subject";
$message = "Test Message";
$headers = 'From: robocop#nospam.com' . "\r\n" .
'Reply-To: robocop#nosopam.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location:http://nospam.com/home/bouncer.php');
die();
Try this for testing. I don't think that there are really such problem.
$to = 'hello#nospam.com';
$subject = "Test Subject";
$message = "Test Message";
$headers = 'From: robocop#nospam.com' . "\r\n" .
'Reply-To: robocop#nosopam.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
header('Location:http://nospam.com/home/bouncer.php');
} else {
echo "Fail";
}
die(); // and use for also exit(); for testing
That's very interesting if it is real problem.

HTML Form/PHP and MySQL email notification

complete beginner at PHP and was wanting a little direction for a website I am creating. I want the admin of the website to receive an email with all of the form information aswell as it being stored in the database. The database is storing the information fine, just need an email notification. How is this achieved. My PHP code is:
<?php
session_start();
include('connection.php');
$product = $_POST['product'];
$productcomments = $_POST['productcomments'];
$name = $_POST['name'];
$address = $_POST['address'];
$age = $_POST['age'];
$delivery = $_POST['delivery'];
mysql_query("INSERT INTO orderform(product, productcomments, name, address, age, delivery)VALUES('$product', '$productcomments', '$name','$address', '$age', '$delivery')");
header("location: google.com");
$to = 'j_bussey#live.co.uk';
$subject = 'Order';
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysql_close($con);
?>
this is the very basic example of mail function. read more about mail() manual here.
$email = $_POST['email'];
$subject = "Email Subject";
$message = "Email Message Body";
mail($email, $subject, $message, "from: admin#yourdomain.com");
PHP has an awesome mail() function. I'm taking this from the documentation page here: http://us2.php.net/manual/en/function.mail.php
<?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 since you're already grabbing the variables, you would just edit the $message variable in the code above to look something like this:
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
etc, etc. If you don't want to assume that they have html emails enabled, you would use \n instead of <br />
Edit:
You also need to change your header('Location: google.com'); to header('Location: http://www.google.com'); or wherever you want to redirect after the email has been sent off.

keeping line break while sending email using php

I am using below php code to send email after collecting user entered email ID (email-box) and content from a textarea(output_textarea) as message.
$to = $_REQUEST['email-box'] ;
$message = $_REQUEST['output_textarea'];
$subject = 'form meta cli script';
$headers = 'From: info#mydomain.com' . "\r\n" .
'Reply-To: me#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
But outlook is removing the line break and on top giving message "Extra Line breaks in this message were removed" How can I keep the line break in outlook?

sending get_defined_vars() to yourself via email (PHP)

During development when other people are experimenting with my site and have issues, I want to be able to find out where my code was having issues, get_defined_vars() is probably the most useful thing for me in finding out what happened at this point.
I am up to the point of writing this function, however it is returning:
Parse error: syntax error, unexpected '='
Does anybody know a way to send yourself get_defined_vars() from php?
if(isset($_GET['sendmeanemail'])){
$emailarr = get_defined_vars();
$to = 'myemail#gmail.com';
$subject = 'Debug Report for'. $currentApiUser['first-name']. ' '. $currentApiUser['last-name'];
$message = '<pre>\n';
$message. = print_r(addslashes($emailarr));
$message. = '</pre>';
$headers = 'From: webmaster#domain.com' . "\r\n" .
'Reply-To: webmaster#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Edit: This was fixed by changing to: (pre tags arent actually necessary, am figuring out how to better format for gmail)
if(isset($_GET['sendmeanemail'])){
$emailarr = get_defined_vars();
$to = 'myemail#gmail.com';
$subject = 'Debug Report for'. $currentApiUser['first-name']. ' '. $currentApiUser['last-name'];
$message = '<pre>';
$message .= print_r($emailarr, true);
$message .= '</pre>';
$headers = 'From: webmaster#domain.com' . "\r\n" .
'Reply-To: webmaster#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
You need to call print_r with the second parameter set to true. Also, addslashes should not be called on an array. It should look like:
print_r($emailarr,true);
Last, you need to move the . (period) next to the equal sign. Your code should look like:
$message = '<pre>\n';
$message .= print_r($emailarr,true);
$message .= '</pre>';

PHP from field when sending mail()

I'm using PHP's mail() function and noticing that my mail is being shown from being sent by 'My Website' in my inbox, but when I click on the actual email it shows it being sent from mywebsite#sitename.localdomain.
Ideally I'd like to have it say being sent from 'My Website', but the reply email being 'no-reply#mywebsite.com', and not to have it say anything about #sitename.localdomain.
$to = trim(strtolower($_POST['to']));
$from = trim($_POST['from']);
$message = trim($_POST['message']);
$subject = $from . ' has shared a link with you';
$headers = 'From: My Website' . "\r\n" .
'Reply-To:' . $to . "\r\n" .
'X-Mailer: PHP/';
mail($to, $subject, $message, $headers);
Is this an issue that I need to fix in Apache, or can I modify the headers within PHP?
Try this:
$to = trim(strtolower($_POST['to']));
$from = trim($_POST['from']);
$message = trim($_POST['message']);
$subject = $from . ' has shared a link with you';
$headers = 'From: My Website <no-reply#mywebsite.com>' . "\r\n" . // <- change your email here
'Reply-To:' . $to . "\r\n" .
'X-Mailer: PHP/';
mail($to, $subject, $message, $headers);
The Question and Answer #1 contains a serious security vulnerability -
$to = trim(strtolower($_POST['to']));
Will allow an attacker to use your website to email arbitrary spam and your site will be blocked from most search engines.
See
https://www.owasp.org/index.php/Top_10_2010-A1
My recommendation is to
Sanitize the to and from fields
Never ever ever copy the message in the post to the output unless carefully sanitized.

Categories