Combining PHP code run on server with ready-made HTML email template - php

I was wondering how can I possibly combine on my FTP server the PHP sourcecode that meant to send emails while launched on server side with an e-mail template which I have already made using some email generator.
The main problem I've got is that I can't find any answer on how to setup these files on server and combine them with eachother, should I use one file or several named differently. I'm sorry that I represent such novice level, so excuse me for my ignorance.
Here's my PHP sourcecode that is meant to be a base for an e-mail sending, I just don't know how can I possibly combine it with my HTML email template.
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "Message example";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>
Is there any way I can simply include the sourcecode of my emails HTML template to this code ?

As far as I understood the problem, you can easily write php and html in a common file. Open and close the php, html tags as and when needed.
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "Message example";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>
<html>
<body>
<p>Write HTML here</p>
</body>
</html>

You need to include your HTML in the $message variable, one way to do it is like this:
<?php
$receiver = "receiver#gmail.com";
$subject = "Subject example";
$message = "<div>Your email HTML Template here</div>";
$sender = "sender#gmail.com";
$header = "From: " . $from;
mail($receiver, $subject, $message, $header);
echo "ok!";
?>

Related

PHP E-mail form - Define sender

I'm trying to create a HTML form, which will in the end send an e-mail using the folling PHP Code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$destinationemail = "myemail#domain.com";
$emailcontent = "Name: {$name}\n\nE-Mail: {$email}\n\nMessage: {$subject}\n{$message}";
$subject = "Contact from Domain.com";
$from = $email;
mail($destinationemail, $subject, $emailcontent) or die("Error!");
echo "Thank you $name!";
?>
The problem is, everytime i receive an e-mail, i get is as if being sent from a what i guess is the Webhost general e-mail.
htgkaylg#server776.web-hosting.net
<htgkaylg#server776.web-hosting.net>
dom 08/10/2017, 18:40
Você;
I would like it to be received something like this:
myemail#domain.com
<myemail#domain.com>
dom 08/10/2017, 18:40
Você;
Is it possible?
Thank you,
Vítor
You have to use the Header as 4th parameter in mail() function.
Add this line and change mail as follow:
$headers = "From:" . $from . "\r\n";
mail($destinationemail, $subject, $emailcontent, $headers) or die("Error!");
Ref. https://www.w3schools.com/php/func_mail_mail.asp

php-html email with variable

Hallo I've got a problem with sending html E-mails with a dyanamic Link via PHP.
That is my PHP Code:
$empfaenger = $_POST['email'];
$test = sha1($_POST['email']);
$betreff = "Test Mail";
$from = "From: Peter <tets#gmail.com>\n";
$from .= "Reply-To: test16#gmail.com\n";
$from .= "Content-Type: text/html\n";
$text = "Please click <p><a href='http://defaultpage/default.php?res=".$test."'>here</a></p>.";
mail($empfaenger, $betreff, $text, $from);
In the mail Log all looks normal like the mail is sended, but I didn't receive one. In PHP error Log also nothing. If I set $test to a specified string it's working as expected, but not with the dyanamic variable $test. I hope that someone can help me becuse I can't get rid of this problem.

Why the mail content sent are the raw html codes?

Mail content:
<html><head></head><body><p> Message : 鏽嫌╒杜米土 杯屎</p>Share Link : Press here to enter <br><img src ='http://203.80.1.28/FlippingBook/development/demo/medium/Web081112_P070_medium.jpg' /></body></html>
PHP:
if (isset($_POST["data"])){
$info = explode("&", $_POST["data"]);
$headers = 'MIME-Version: 1.0\r\n';
$headers .= "Content-Type: text/html; charset = \"UTF-8\";\n";
$headers = "From: =?UTF-8?B?" . base64_encode(substr($info[0],strpos($info[0],'=')+1, strlen($info[0]))) . "?=";
$to = substr($info[1],strpos($info[1],'=')+1, strlen($info[1]));
$subject = "=?UTF-8?B?" . base64_encode('日報分享') . "?=";
$message = trim(substr($info[2],strpos($info[2],'=')+1, strlen($info[2])));
$message = '<html><head></head><body><p> Message : '.$message;
$url = substr($info[3],strpos($info[3],'=')+1, strlen($info[3]));
$message = $message. '</p>Share Link : Press here to enter ';
if (isset($info[4])){
$firstImg = substr($info[4],strpos($info[4],'=')+1, strlen($info[4]));
$message = $message."<br><img src ='".$firstImg."' />";
}
if (isset($info[5])){
$secondImg = substr($info[5],strpos($info[5],'=')+1, strlen($info[5]));
$message = $message."<br><img src ='".$secondImg."' />";
}
$message = $message.'</body></html>';
if (mail($to, $subject, $message, $headers))
die ('Mail sent');
else
die ('Fail');
}else{
die ('Fail');
}
I am writing a simple program to send email. However, my mail content is not english based so I used utf-8 to encode.
When I changed the encode method, it can not send the processed html code, instead the mail content is the raw html code shown above, how to fix this problem?
If using PEAR lib is not a problem than you can look for Pear Mail-Mime lib to send mail or HTML contents. More details you can get from here - http://pear.php.net/manual/en/package.mail.mail-mime.example.php

Can you insert an if statement in php email

// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.com';
$body = HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var

PHP Message send with PHP Session

The aim of this script is to send an email, but instead of including your name and email, it would include it on the message because the website already has details of this from the PHP session. Currently the "2Email" works.. it can send a message to the recipient's mail box, and includes the user's input message. But it doesn't follow the template. I.E. It doesn't include the text "Sent via the dashbaord"
<?php
session_start();
if(!isset($_SESSION["USER"])){
header("Location: ../login.php?NotAuth");
}
$to = $_POST['2Email'];
$name = $_SESSION["USER"]["FullName"];
$email = $_SESSION["USER"]["Email"];
$subject = $_POST['regarding'];
$message = $_POST['msg'];
$Cc= $_SESSION["USER"]["Email"];
$headers = "From: $email";
$tracker = "This message was sent via the TrackerSystem dashboard";
$message = "Hi, "."\n".$message. "\n". "Regards, ".$name. "\n".$tracker.
$sent = mail($to, $subject, $message, $headers) ;
if($sent) {
print "Sent successfully! XD ";
} else {
print "The server made a booboo, the email didn't send :'( ";
}
?>
Is it a typo on line 19: "\n".$tracker. ? If so, that's what prevents your tracker being appended to the message.
What's actually happening: the whole expression on lines 19-20 (this one):
$message = "Hi, "."\n".$message. "\n". "Regards, ".$name. "\n".$tracker.
$sent = mail($to, $subject, $message, $headers) ;
is being evaluated from right to left (if it even works, of which I doubt). So (first) mail is sent and result is assigned to $sent and (second) the whole concatenated string is assigned to $message.
To fix it, make "\n".$tracker. "\n".$tracker;.

Categories