i have made my contact form and it's working good except that when someone send me a message it comes without any format like the image below:
this is my .php code that i use:
$formdata = array (
'name' => $name,
'city' => $city,
'message' => $message
);
if ( !( $formerrors ) ) :
$to = "me#sipledomain.com";// input my name address to get mail to
$subject = "From $name";
$message = json_encode($formdata);
if ( mail( $to, $subject, $message ) ):
$msg = "Thanks for filling out the form, i will contact you soon";
else:
$msg = "Problem sending the message";
endif; // mail form data
endif; // check for form errors
endif; //form submitted
thanks in advance
json_encode() encodes your array into a single line of text designed to be decoded later, not for reading by humans.
Instead I would build your email message yourself by writing your own HTML or by giving it line breaks. You could do it programmatically by parsing/iterating through your array.
Eg:
$message = 'Name: '.$formdata['name'].'<br />'.$formdata['city'].'<br />'.'...';
If you really want to encode into JSON, you will need to do parse the JSON after you encode and do the same thing.
You might want to look into a flag when you call json_encode() called JSON_PRETTY_PRINT which will keep whitespace. More info: http://www.php.net/manual/en/json.constants.php
In use: $message = json_encode($formdata, JSON_PRETTY_PRINT);
For playing with JSON I like to use tools like http://jsonmate.com/ that formats JSON into a neat tree.
If you want, send the email as formatted HTML by wrapping everything in standard HTML tags. Otherwise, PHP sends messages as unformatted, so use \n to break lines etc.
Related
I am trying to send a message with one list with PHP, here is my code:
$packs = $db->QueryFetchArrayAll("SELECT * FROM configmoreg");
foreach($packs as $pack) {
echo '<a>'.$pack['setting_id'].'</a>
<a>'.$pack['config_name'].'</a>
<a>'.$pack['config_value'].'</a>
<br>';
}
mail('example#something.com',"My List",$msg);
How do I make it send only one message with list on it?
For example:
id 00000001
My_name game_over_new
NR_Job 11
type_secure MD5
5 etc...
Build up a string in the for-loop, and send that. Consider:
$msg = '';
foreach($packs as $pack)
$msg .= "<a>${pack['some_key']}</a>";
$subject = "My List";
mail('null#example.com', $subject, $msg);
Meanwhile, I gather you're still learning PHP, so hold on to this next piece of advice until you're ready: don't use PHP's builtin mail() function, use PHPMailer.
Try adding all the variables to an array; then send the array containing the variables.
As you don't know how to use arrays I suggest reading:
https://www.w3schools.com/php/php_arrays.asp
However to get what you want, you'll want something along these lines:
$infoToSend = array($pack['setting_id'], $pack['config_name'], $pack['config_value']);
mail('example#something.com',"My List", $infoToSend);
I am trying to use swiftmailer in my project so that I can send html newsletter to multiple users. I have searched thoroughly but all i got never worked for me. I want to paste more than one recipient in the form input field seperated by comma and send the html email to them.
I set the recipients to a variable($recipients_emails) and pass it to setTo() method in the sending code, same with the html_email.
Questions are:
Q1 How do i send to more than one recipient from the recipient input field.
I tried this:
if (isset($_POST['recipients_emails'])) {
$recipients_emails = array($_POST['recipients_emails'] );
$recipients_emails= implode(',',$recipients_emails);
}
Q2 How do I make the Html within heredoc tag. when i tried concatenating like this ->setBody('<<<EOT'.$html_email.'EOT;', 'text/html'); , my message would appears with the heredoc tag.
if (isset($_POST['html_email'])) {
$html_email = $_POST['html_email'];
}
How do I have input from $_POST['html_email']; to be within EOT tag;
this in part of swiftmailer sending script ;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($html_email, 'text/html');
Nota bene : Am still learning these things.
According to this document
// Using setTo() to set all recipients in one go
$message->setTo([
'person1#example.org',
'person2#otherdomain.org' => 'Person 2 Name',
'person3#example.org',
'person4#example.org',
'person5#example.org' => 'Person 5 Name'
]);
You can input array directly into setTo, setCc or setBcc function, do not need to convert it into string
You should validate the input-data by first exploding them into single E-Mail-Adresses and push the valid Data into an Array. After this you can suppy the generated Array to setTo().
<input type="text" name="recipients" value="email1#host.com;email2#host.com;...">
On Submit
$recipients = array();
$emails = preg_split('/[;,]/', $_POST['recipients']);
foreach($emails as $email){
//check and trim the Data
if($valid){
$recipients[] = trim($email);
// do something else if valid
}else{
// Error-Handling goes here
}
}
Ok so first of all you need to create a variable that form your heredoc contents. The end tag of heredoc has to have no whitespace before it and then use that variable. (to output variables inside heredoc you need to wrap them with curly braces) see example..
$body = <<<EOT
Here is the email {$html_email}
EOT;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($body, 'text/html');
I have a form with more than 20 input fields. The PHP mail function is not working since it can only accept 5 parameters. How can I send these values to my email address?
$to = 'myemail#mydomain.com';
$subject = 'form values';
$message = '';
foreach( $_POST as $key => $ value ) {
$message .= $key . ' => ' . $value . '<br>';
}
mail( $to, $subject, $message);
The parameters in the PHP mail() function have different meaning. They are not the data, sent to the user. Try using the following script and if it works - format the $message to match your requirements.
$email = 'rec#example.com';
$subject = 'Subject';
$message = print_r($_POST,true);
mail($email,$subject,$message);
More information can be found # http://php.net/manual/en/function.mail.php
Well I don't know what kind of input fields you have, but they could all be passed in the "subject" field. However, if you have a sufficiently complex email to send, and you really need to write everything yourself, you should look into PEAR.
So either add the 20 input fields onto the subject $subject .= 20things or use a tool that is inherently secure without having to remember to escape input and validate using regex.
I'm trying to do a foreach loop inside a foreach loop.
I have a form where the user type in some text and hit send. On the server site I loop through an array with email addresses and send out the text message.
Now I also want the user to be able to use variables in the textarea, like $name.
So my loop should first loop through the emails, and then str_replace the userinput $name variable, with the names in my array.
The loop works fine with the email part ($tlf), but not the replace $name part.
Could some spot what I am doing wrong?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array("name1","name2");
$test=array("mname1", "mname2");
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $name){
$to = $name. "#gmail.com";
foreach($test as $navn){
$message = str_replace('$navn', $navn, $message);}
mail($to,$subject,$message,$headers);
}
Thanks a lot.
EDIT:
The output is an email sent. Say the user type in "hello $name".
I want it to first loop through the $tlf array, in this case creating 2 emails. This goes in as $to in the first loop. This works.
Now the next loop should recognize the user input "hello $name" and loop through the $test array replacing the user $name variable.
The output would be 2 emails send.
Mail output:
to: name1#gmail.com
message: hello mname1
Mail output:
to: name1#gmail.com
message: hello mname2
Let me know if I need to explain better, its hard for me to explain sorry.
When you do the following:
str_replace('$navn', $navn, $message)
Then all literal occurences of $navn will be replaced (the first, the second, the third, ...). So running through that loop a second time can't possibly replace anything more.
You will need to define two placeholders, or make some distinction, or use preg_replace_callback if you want to declare in which order (or other logic) the possible replacement strings are applied.
If you had told us (but you haven't) you only wanted to replace the first occurence in each iteration, then a normal preg_replace(.., .., .., 1) would work.
Is this what you want?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array(
array("email" => "name1", "name" => "mname1"),
array("email" => "name2", "name" => "mname2")
);
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $contact){
$to = $contact["email"] "#gmail.com";
$replacedMessage = str_replace('$navn', $contact["name"], $message);
mail($to,$subject,$replacedMessage,$headers);
}
okay, here is the code
while (!feof($text)) {
$name = fgets($text);
$number = fgets($text);
$carrier = fgets($text);
$date = fgets($text);
$line = fgets($text);
$content = $_POST['message'];
$message .= $content;
$message .= "\n";
$number = $number;
mail("$number#vtext.com", "Event Alert", $message, "SGA");
Header("Location: mailconf.php");
}
I am trying to get a message sent to a phone, I have 'message' coming in from a text area, and then I assign it to "$content" then I put "$content" into "$message" and then as you can see, in my mail line, I want the address to be the variable "number"#vtext.com I have the while loop because there are many people in these files I wish to send a message to...
When I change the "$number#vtext.com" to my email address, it sends everything fine, I just cannot seem to get it to send to a phone number, please help!
thanks!
The problem is that fgets includes the newline character as part of the return. So you are effectively sending an email to:
1234567890
#vtext.com
Which of course is invalid. Change your line that reads $number = $number to:
$number = trim($number);
And the rest of your code should function as expected, with the email being received on your phone.
Part of my original answer
I would highly recommend using a (probably paid) service to send SMS messages to phones if you need anything remotely reliable. A quick search yielded a few results:
PHP/SMS Tutorial
A list of 5 ways to Text for free <-- This link is dated, but might still be helpful
You need to append the number variable and the "#vtext.com" literal string together:
mail($number . "#vtext.com", "Event Alert", $message, "SGA");