Email array doesn't show corresponding text - php

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

Related

Sending an email via PHP with the body in MYSQL db and parsing \n

I'm trying to send an email via PHP and the body of the email is stored in a MYSQL database. For some reason when I hardcode the body as a string, it converts characters like "\n" to a paragraph break, but when it's extracted from the MYSQL database, it leaves "\n" in. Anyone have a good idea why that is and how to fix it? I've tried converting it into a string, but to no avail.
the code:
$stmt = $conn->prepare("SELECT * FROM Tickets LIMIT 1");
$stmt->execute();
while($ticket = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ticket_queue_id = $ticket["ticket_queue_id"];
$subject = "test";
$body = $ticket["body"];
$headers = "From: Email";
mail($recipient,$subject,$body,$headers);
echo "Sent test";
}
"Tickets" is a MYSQL table, where $ticket["body"] is stored as a varchar.
In the email, the above code gives something like "Hello\n\world" in the body,
While
$subject = "test";
$body = $ticket["body"];
$headers = "From: Email";
mail($recipient,$subject,$body,$headers);
looks like:
"Hello
world"
Thanks!
Found the answer! Have to encapsulate $ticket["body"] with stripcslashes. Found from here: PHP: Convert single-quoted string into double-quoted

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.

sending an email from a php page

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.

Get all rows into a variable then mailto for each PHP

So, I found this thread (Get all data from mysql row in a variable) but I am too much of a beginner to make it apply easily to my situation. Thank you for helping me out... sorry for the total newb questions.
I have a PHP form that lets the user select one of my tables in a database where email addresses are stored to send an email to each of them. Right now, I have this code:
$recipientid = $_POST['recipientid'];
$body = $_POST['body'];
$subject = $_POST['subject'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SENDER NAME <senderemail#gmail.com>' . "\r\n";
date_default_timezone_set('America/Los_Angeles');
$time = date('m/d/Y h:i:s a', time());
$sendbody = $body . "<br><br>This is a bulk email announcement sent by
the institution.<br><br>It was sent at " . $time . ". If you have any
questions about this message or wish to unsubscribe, please contact
the institution.";
if($recipientid == 'allstudents'){
// SEE NOTE #2//
$recipientlist = //email addresses
}
$process=explode(",",$recipientlist);
reset($process);
foreach ($process as $to) {
$sent=mail($to,$subject,$sendbody,$headers);
}
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
What is the easiest way to capture all of the email addresses in the column of the specified table and then loop a mailto for each? I was originally trying to get them all into one string and then explode them as you can see, but that might not be the best solution. Am I on the right track here?
(NOTE #2 FROM IF)
HERE IS WHERE I NEED SOMETHING... I was sort of thinking about trying to use the following. I need it to grab all the emails from the column emailaddresses in the table students. I am using an if statement because there are four other things that $recipientid could equal, and each different one grabs email addresses from a different table.
array pg_fetch_all_columns ( resource $result [, int $column = 3 ] ) But then, I don't know how to get this array to work with my mail. I originally tried to use just a SELECT * from emailaddresses and then use each row somehow but I don't know how to implement that.
YES, I know I am using mysql not mysqli and I know that mailto is probably not the best solution, but it is what I have to work with right now (unless you can suggest an alternative route for the mail loop).
Thank you again! I really want to learn what I am doing, so an explanation would be appreciated:)
(and ps I am using the mail function with the explode because of this article http://tutorial.world.edu/web-development/php-script/how-to-send-out-mass-emails-php-script/)
I might be a little confused about the question. It sounds like you have a database with email address and you want to send an email for each email address. I think you can just do the query SELECT emailaddress from table and cycle through the results and use your mail function each time.
$query = *your select query*
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$sent=mail($row['emailadress'],$subject,$sendbody,$headers);
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
If you want your user to select the table the email addresses are coming from you can use a form and a variable in the query.
You can use the below piece of code to send the mail.
Consider you have an email field in your table 'students'
then
$sel = mysql_query("select email from students");
while($info = mysql_fetch_assoc($sel))
{
$to = $info['email'];
$sent=mail($to,$subject,$sendbody,$headers);
//---Remaining code goes on
}
try it.

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