sending an email from a php page - php

Im trying to get an email to send from my php page to an email address that is set.
The email is being sent and received at the expected email address. But the contents within the email are not as expected. I got the code from watching a tutorial on youtube.
However when looking at the email is displays exactly as the code below, doesn't give me the values behind the variables e.g $name shows up in the email as $name
Any ideas?
The code im using is;
<?php
require_once 'header.php';
$to = 'emailaddress ';
$subject = 'CSG';
$name = $_POST['name'];
$email = $_POST['emailaddress'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi $name, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
From $name
EMAIL;
$header = '$email';
if ($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Email Sent';
}
?>

Firstly, variables do not get parsed inside single quotes, that's why you're seeing $email rather than the email itself inside the email body.
So change $header = '$email'; to either $header = "$email"; or remove the quotes entirely.
I.e.: $header = $email;
Then the header is failing you. It expects to be a From: (email address) - The "From" in your mail will come back as your server's name rather than the (intended) email address from the person sending it.
Consult the manual:
http://php.net/manual/en/function.mail.php
Sidenote about your heredoc.
Even though mail is going out, you still have a trailing space in your opening identifier:
$body = <<<EMAIL
and that may throw a Parse error: syntax error, unexpected '<<' (T_SL) error.
Error reporting http://php.net/manual/en/function.error-reporting.php
Testing this came back as:
email#example.com
Hi Fred, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
From Fred
and the "From:" as default#cpanel.example.com rather than whoever#example_mailer.xxx.
As noted in comments by dieend, you can try bracing the variables {$var}, however with or without them, produced the same results for me.
If that still doesn't work, then it may be caused by the trailing space in your opening identifier; you need to remove it.
Copy/paste exactly as shown:
$body = <<< EMAIL
Hi {$name}, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
From {$name} - {$email}
EMAIL;
$header = "From:" . $email; // Now you have a valid From
if ($_POST){
mail($to, $subject, $body, $header);
echo $feedback = 'Email Sent';
}
Final notes:
If that still doesn't solve the question, then I for one am unable to reproduce.

Related

PHP Email $Subject Line Will Not Pull Variable

I've searched around with no luck here. I created a PHP email form that works great. Now I need to edit the email subject line to pull information from the contact form.
Here's what I'm looking to have as the email subject line:
"You have a $variable inquiry from $variable.
Here's my current code that is giving me an error on the $Subject line:
$EmailFrom = "Contact#xxx.com";
$EmailTo = "ryan#xxx.com";
$Subject = "You have a .$Subj inquiry from .$Name";
$Name = Trim(stripslashes($_POST['sender_name']));
$Email = Trim(stripslashes($_POST['sender_email']));
$Message = Trim(stripslashes($_POST['message']));
$System = Trim(stripslashes($_POST['system']));
$Item = Trim(stripslashes($_POST['item']));
$Subj = Trim(stripslashes($_POST['subj']));
So I have tried adding a <> around the variables in the $Subject. I have also tried with the periods as above, as well as with just the $xxx.
When I send the email, it will send as:
"You have a <> inquiry from <>"
or
"You have a . inquiry from ."
Depending on which characters I have around the $xxx variable.
I know this is probably some stupid error, but can someone hint me at what I need to do to have those variable pulled into the $Subject?
Thanks in advance,
Ryan
You have your variable declarations out of order.
You are using $Name in the first line before you have assigned a value to it. You need to put the lines that give values to $Name and $Subj before the line that uses them.

Email array doesn't show corresponding text

Please someone tell me, if I have an array like this:
$subject = array();
$subject[1] = "legatoria";
$subject[2] = "restauro";
$subject[3] = "grafica";
$subject[4] = "stampa";
$subject[5] = "amministrazione";
$subject[6] = "altro";
$subjectindex = $_REQUEST['subject'];
if ($subjectindex == 0 || !isset($_REQUEST['subject'])) die ("error message");
else $subject = $subject[$subjectindex];
and the mail sends like this:
$mail->Body .= "message has subject: ".$_POST['subject']."\n";
why the subject in the email sent by the user is showed as a number (for example 4) and not as the corresponding text (i.e. stampa) ?
$_POST['subject'] refers to the exact data returned by an HTML form with name="subject". If your form is sending a number corresponding to the index of $subject and you want to print the array value, you need to use:
$subject[$_POST['subject']]
so:
$mail->Body .= "message has subject: ".$subject[$_POST['subject']]."\n";
I'm closing this post. The right solution was only the one suggested by u_mulder
"Also according to provided code $subject is already aasigned a value according to $_REQUEST['subject']. So $subject itself can work too"
Thank you

how to email database and form values

I have a website created using PHP to store form values in a database, and have a page that displays the values for certain users based on the selections. I am trying to send these values from the database to an email address, but I can not separate the values on each line of the subject. I am using the PHP built in function to email and storing the values in the subject variable.
I have tried add each variable separately by using
$subject . "content";
for each variable but it still is all in one line.
I am now thinking of making a newsletter type, but can't figure out how to make one or make it work how I want. So if anyone could help out on how I can send these values either in this way or in a different language.
Here is the code I have so far.
$query= sql query
$resultt= sql result
$roww=mysql_fetch_assoc($resultt);
extract($roww);
$emmessage = "User Form Information";
$emmessage = $emmessage . " " . "values extracted from the database from $roww
$send = #$_POST['send'];
$subject = strip_tags(#$_POST['subject']);
$reciever= strip_tags(#$_POST['email']);
$message = $emmessage;
// Start email processing
if ($send)
{
// Send the message
mail($reciever, $subject, $message, "From: $email");
$emessage="Your message has been sent";
include("forme.php");
You can used br2nl() function. Or if you are used \n directly as suggested before you should use nl2br() function so that in HTML page you will get a new line instead of a \n.
You can seperate lines in e-mails using \n.
$content = "Value 1 \n Value2";

php email sending script not sending email

following is my script for send email inquiry.. the recipient email address was stored in a db called users.. this script will not work properly.. i think the problem is recipient email section.. because when i used a email address instead of $user it will work..
thanx help me
<?php
$refno = $HTTP_POST_VARS['refno'];
$proid = $HTTP_POST_VARS['proid'];
$name = $HTTP_POST_VARS['name'];
$email = $HTTP_POST_VARS['email'];
$msg = $HTTP_POST_VARS['msg'];
//connect db and find email address related to id
include 'db_connector.php';
$id=$HTTP_POST_VARS['id'];
$query=mysql_query("SELECT user_email FROM users WHERE id='".$id."'");
$cont=mysql_fetch_array($query);
$user=$cont['user_email'];
// recipient name
$recipientname = "'".$name."'";
// recipient email
$recipientemail = $user ;
// subject of the email sent to you
$subject = "Inquiry for your advertisement No. (".$refno.")";
// send an autoresponse to the user?
$autoresponse = "no";
// subject of autoresponse
$autosubject = "Thank you for your inquiry!";
// autoresponse message
$automessage = "Thank you for your inquiry.! We'll get back to you shortly.
";
// END OF NECESSARY MODIFICATIONS
$message = "reference number : $refno
$msg
From $name $email
---";
// send mail and print success message
mail($recipientemail,"$subject","$message","From: $recipientname <$email>");
if($autoresponse == "yes") {
$autosubject = stripslashes($autosubject);
$automessage = stripslashes($automessage);
mail($email,"$autosubject","$automessage","From: $recipientname <$recipientemail>");
}
header("Location:index.php");
exit;
?>
First of all, your query is SQL injectable. Never ever pass a variable coming from a POST request directly into an SQL query. Use mysql_real_escape().
As to your bug: it seems that $user does not contain a valid e-mail address. so, the Mysql query is not returning an e-mail address.
Use $_POST rather than $HTTP_POST_VARS.
Switch on error reporting by prepending these two lines to your PHP code:
PHP code:
error_reporting(E_ALL);
ini_set('display_errors','1');
Run your script again. Do you get any notices or warnings?
If not, try to display your query, by adding
die($query);
just before the line that has the mysql_query command, and then run the query manually (e.g. using PhpMyAdmin or MySQL Query Browser) to see if you are actually getting a result that looks like an e-mail address.
Debug your PHP program.
Check out :
If the variables contain the supposed values.
Query is okay and returns result.
Proper header is set with mail function.
etc.
PHP manual have a good example to send mail.
Do not use $HTTP_POST_VARS['name']; it is deprecated use $_POST instead.
hey guys thanx for the help.. i found the error done in my inquiry form.. the id filed hidden outside of the form tag.. therefore the id value will not passed to the sendinq.php. i change it thank now the sendmail option work properly

Sending Email to Multiple Recipients from MySql Recordset

I want to send an email to multiple recipients using PHP mail() function. The email message is simply a reminder that a membership is due to expire so email addresses will come from MySql database query. There would anywhere from 2-10 at any given time. I found the following code but it generate errors. The problem is not with my query as it generates an accurate recordset. This is the code I have: Hopefully someone can help. By the way, I am very much a novice so need easy straight forward explanation. Thanks in advance:
<?php
$recipients = ("SELECT email FROM tblMembers WHERE search criteria=criteria");
$email_list = $db->query($recipients);
foreach($email_list as $row) {
$to = $row['email'];
$subject = "Membership Renewal";
$headers = "From: Membership Coordinator <membership#myaddress.net>\r\n";
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY""\n""etc, etc, etc";
if ( mail($to,$subject,$headers,$message) ) {
echo "Email was sent successfully";
} else {
echo "Email delivery has failed!";
}
}
?>
As far as I know, then $headers comes after $message, so you should just change the order in mail() and be more aware in future.
Change
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY""\n""etc, etc, etc";
to
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY\netc, etc, etc";
There is the syntax error, because " will end the string. You would need a . to concatenate the next string.
But you could also leave the two " out at this point, becase in a double quoted string, PHP will replace \n by a newline character.

Categories