Get Contents from Text File and Replace Keywords PHP - php

I have a form. I'm using "post" method that sends and emails to multiple people that registered. Then the $body of the email is based on a template. No database, no classes, simple form. Yes I read up about this already they're all there I just couldn't put it together, related to this case.
The text "email_template.txt" should have something like:
Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#
Would look like this upon processing by PHP
Hello **John Doe**. Thank you registering! Your registered email is **example#example.com**.
On my php I have something like:
<?php
//form and validation
$firstname = "John"; // inputed name on the form, let say
$lastname = "Doe"; // inputed last name
$email = "example"; // inputed email
//email message
$body = ... some type of a get_file_content....
mail($_POST['email'], 'Party Invitation', $body, 'From: admin#admin.com');
?>
Where $body is the email message to the registrants submitted via this PHP form.

sprintf will work good
your template could be:
Hello %s %s. Thank you for registering! Your registered email is %s
$body = sprintf($fileContents,$firstname,$lastname,$email);
or str_replace(), pretty much a find replace.
$body = "Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#";
$body = str_replace("#firstname#",$firstname,$body);
$body = str_replace("#lastname#",$lastname,$body);
$body = str_replace("#email#",$email,$body);

Related

How to make form fields required? [duplicate]

This question already has answers here:
Making email field required in php [closed]
(4 answers)
Closed 8 years ago.
I have this existing code and I am wondering how to make the name and email field required?
<?php
if(isset($_POST['submit'])){
$to = "xxx#email.com"; // this is your Email address
$from = $_POST['gift_email']; // this is the sender's Email address
$first_name = $_POST['gift_name'];
$subject = "Free Gift Request";
$msg = "A free gift has been requested from the following:"."\n";
$msg .= "Name: ".$_POST["gift_name"]."\n";
$msg .= "E-Mail: ".$_POST["gift_email"];
$headers = "From:" . $from;
mail($to,$subject,$msg,$headers);
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header('Location:free_program_thankyou.php');
}
?>
For form
<input type="text" name="gift_email" required>
<input type="text" name="gift_name" required>
For Php
if(empty($_POST['gift_email']))
{
echo 'This field is required';
}else {
//Do what you want to do here
}
A two basic ways to do this:-
Within the php program check each required form field has been filled in send a new page with an error message back if it is not. Be sure to return the contents of any fields already filled in or your users will wish a plague of boils on your person.
Validate in javascript. Have a function triggered by the "onsubmit" condition which checks for all required forms fields are filled and highlights any that are not. see here
In practice a robust web site will do both. This seems like duplication however the javascript function is much more responsive and user friendly, BUT, the php server side validation cannot be gamed by turning JS off or spoofing responses.

PHP : Read GMAIL Email with imap

here's my problem: I have a mailbox where came from 2 types of email:
1) e-mail with plain text
2) email, I think, base64-encoded text / html
I have a simple page in php that goes to read this email box, and prints out an HTML table with some information of each email
I recognize the two types of email from the object of the email, in the first case I have no problems, while in the second i have a problem
i read the "body" of the mail of the first type in this way (and I have no problem, I can print the contents of the email as plain text) :
$id = imap_uid($stream ,$email_id);
$message = imap_qprint(imap_body($stream, $email_id,FT_PEEK));
but in the second case it does not work this way, and I have tried the following
$message = imap_fetchbody($stream,$email_id,"");
$message = imap_fetchbody($stream,$email_id,"0");
$message = imap_fetchbody($stream,$email_id,"1");
$message = imap_fetchbody($stream,$email_id,"1.1");
$message = imap_fetchbody($stream,$email_id,"1.2");
$message = imap_fetchbody($stream,$email_id,"2");
echo "*".base64_decode($message)."*";
echo "*".imap_base64($message)."*";
but I have no result

can anyone help me with mail function in php?

I'm new to php, i just start to use mail function. I have a problem like below:
Suppose, i have more recipients than one that will get my email.
<?php
$to = $_POST['to']; //xxxxx#yahoo.com,yyyyy#yahoo.com,zzzzz#yahoo.com
$from = $_POST['from']; //aaaaa#yahoo.com
$from = "myinfo <$from>";
$subject = $_POST['subject']; //New campiagn
$content = $_POST['content'];
$headers = "From:" . $from;
mail($to,$subject,$content,$headers);
?>
The code above work correctly. But when user get this email, they will see:
To Me, yyyyy#yahoo.com, zzzzz#yahoo.com
I don't want all users that get this email show when user view this email. Below is what i want:
To myuser#info.com
Does it is possible to do like this? I'm appreciate to all of your answer :)
Thank in advance
If you use a list in the To: field of a regular mail client the list will appear to every recipient. This is normal behaviour. If you want to hide the list then your best option is to send each recipient their own individual copy.

How to make from-name in email appear with a space using PHP?

I am currently sending text email in PHP and doing something like this:
$from = "from: Hike Attendance Update#comehike.com";
$to_email_address = 'some email';
$subject = 'Some subject';
$contents = 'Blah';
mail($to_email_address, $subject, $contents, $from);
When I send it, it appears as sent from Hike.Attendance.Update which doesn't look very good. How can I make it appear with spaces instead of dots?
Thanks,
Alex
Try:
$from = "from: \"Hike Attendance\" <Update#comehike.com>";
I think that's specified in some RFC for email, namely RFC 5322.
When specifying the actual name, put the email address in <> chars:
$from = "from: Hike Attendance <Update#comehike.com>";

How can I add HTML formatting to 'Swift Mail tutorial based' PHP email?

I have developed a competition page for a client, and they wish for the email the customer receives be more than simply text. The tutorial I used only provided simple text, within the 'send body message'. I am required to add html to thank the customer for entering, with introducing images to this email.
The code is:
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Thanks for entering the competition');
$message ->setFrom(array('info#examplemail.com' => 'FromEmailExample'));
$message ->setTo(array($info['email'] => $info['name']));
$message ->setBody('Thanks for entering the competition, we will be in touch if you are a lucky winner.');
$result = $mailer->send($message);
return $result;
}
This function.php sheet is working and the customer is recieving their email ok, I just need to change the
('Thanks for entering the competition,
we will be in touch if you are a lucky
winner.')
to have HTML instead...
Please, if you can, provide me with an example of how I can integrate HTML into this function.
You can also just add 'text/html' to setBody (ref):
->setBody($this->renderView('YouBundleName:Default:email.html.twig'), 'text/html');
$message = Swift_Message::newInstance();
$message->setContentType('text=html');
Will the same text remain or should it be styled somehow?
Editing your emails in html requires inline css styles eg:
('<p style="font-size:1.2em; color:#f0f0f0;">Thanks for entering the competition, we will be in touch if you are a lucky winner.</p>')
if you need a table just add:
('<table style="font-size:1.2em; color:#f0f0f0;"><tr><td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td></tr></table>')
or to make it more simpler
$message_body'
<table style="font-size:1.2em; color:#f0f0f0;">
<tr>
<td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td>
</tr>
</table>
';
$message ->setBody($message_body);
I know that when I need to send html emails I need to set the content-type to html which I believe you did on the following line
$body = format_email($info,'html');
I hope this is what you were looking for. if not let me know

Categories