sending get_defined_vars() to yourself via email (PHP) - 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>';

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.

String "1" gets added in php mail()

I'm sending an email using php's mail() function. My code is as following:
$to = email#email.com;
$subject = "Subject of email";
ob_start();
echo include('emailcontent.php');
$message = ob_get_contents();
ob_end_clean();
$headers =
'From: Email <email#email.com>' . "\r\n" .
'Reply-To: Email <no-reply#email.com>' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
For some reason the string "1" is added at the end of the message. This happens in the include('emailcontent.php'), because if I add another string to the message after the include the "1" is added before that addition.
In the emailcontent.php is no 1 or whatsoever.
echo include('emailcontent.php');
Remove the echo. That echo puts 1 in there
You might ask why does echo put 1 in there when its not added anywhere? Because when your file is successfully included the return value of that include is TRUE and when you send that to echo it prints 1. Try
echo 2==2; //1
Why you are echoing the php page? You have to include the page like,
include('emailcontent.php');
Remove the echo it is causing the 1.

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.

How to send email using PHP?

I want to send email using PHP when an HTML form is submitted.
I learnt from a tutorial and made this PHP script, but when I check it in my webmail, I only see email address, subject and message, but no name. What do I need to do to get the name to show up?
Form variables:
if (empty($_POST) === false) {
$errors = array();
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
// ... etc (validation)
if (empty($errors) === true) {
$headers = 'From: '.$email. "\r\n" . $name.
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('mail#example.com',$subject,$message,$headers);
print "<p class='formerrors'>Thank you for your message, I'll get back to you shortly!</p>";
}
The format of your "From" header isn't right. It should be in the following Format:
"From: Sender Name <sender#domain.com>"
So your $headers assignment should read:
$headers = 'From: '.$name.' <'.$email. ">\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You should modify your $headers variable as:
$headers = 'From: ' .$name. ' <' .$email. '>' . "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Please read more about mail function on php.net over here: http://php.net/manual/en/ref.mail.php
Moreover maybe PHPMailer, see https://github.com/PHPMailer/PHPMailer is interesting for you to enrich your mails.

if statement breaking my generated email

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.

Categories