Send an email templating during a include in PHP - php

I try to send a email with templates. I have severals templates (one to reset password, one to upgrade the account, ...).
All of these templates are stored in a mail directory and it looks like this:
<?php
// Template to reset password
$message = "Hello Mr, ...";
$subject = "Your new password";
$email = "mr#company.com";
?>
Now I have a function:
function sendMail($template, $USR_Id) {
ob_start();
include 'mail/'.$template.'.php';
$message = ob_get_contents();
ob_end_clean();
// Start configuring the email
$headers .= 'From: company <noreply#company.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);
}
My problem:
How to get back the infos store in my template files (i.e.: $subject and $email) ?

Good to see you using my previously suggested setup. That setup depended on the echo however, and if you're not using that you can make your function even simpler. If your templates look like that; you can simply do:
function sendMail($template, $USR_Id) {
include 'mail/'.$template.'.php';
// Start configuring the email
$headers .= 'From: company <noreply#company.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);
}
If you use include / require; you can use all variables across multiple files

Related

How Do I Include PHP Strings In The Message Of The Mail() Function?

I'm trying to figure out how to include custom PHP strings in the message of the PHP mail function.
I have a user registration area on my website. Once a user signs up, I want to email them a success message, such as "Welcome to The site...." I want to include their name in the email, so I would need to pull the value from the field where they entered their name. I will be pulling over field values as well, but this is just an example.
I have:
$name = $conn->real_escape_string($_POST['name']);
$email = 'myemail#gmail.com'
$subject = 'Welcome!'
$message = '<HTML><body><p>Thank you for signing up' .$name. 'We are so glad that you are here</body></html>';
mail($email,$subject,$message);
What am I doing wrong?
add header in your mail like this
$headers = "From: myemail#gmail.com\r\n";
$headers .= "CC: myemail#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);

How to send a html / html5 template email with php?

I want to send a html / html5 email with PHP. I have a html template with the email message which I am planning to send to a user.
HTML template - default-message.php
<?php include 'mailservice.php'; ?>
<div>
<h3>Dear <?php echo $name; ?>,</h3>
<p>I send you this custom message!!</p>
</div>
PHP - mailservice.php
/* getting the content from te default-message.html file*/
ob_start();
include('default-message.php');
$message = ob_get_contents();
ob_end_clean();
$name = "John";
$to = "sadas#yahoo.com";
$subject = "Hello";
$headers .= 'From: Me <abc#mysite.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail ($to, $subject, $message, $headers);
You need to set the variables before you include the template script, so they'll be expanded when the template runs.
ob_start();
$name = "John";
$to = "sadas#yahoo.com";
include('default-message.html');
$message = ob_get_contents();
ob_end_clean();
$subject = "Hello";
$headers .= 'From: Me <abc#mysite.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail ($to, $subject, $message, $headers);
Also, default-message.html should not contain <?php include 'mailservice.php'; ?>, because then it will go back and forth with each script including the other.

PHP Emails are sending, but headers are not

I'm using several different mailers for a single website, because right now, none of them are working correctly. And they're all malfunctioning in the exact same way, ,so I don't believe that it's any of the mail functions, but I'd like another set of eyes on this to make sure I'm not missing something.
Here's what's happening:
The emails send just fine, but it looks like none of the header info I set is being used. When you get an email, the html is displaying as code, and the subject shows as part of the message.
Here's the header info:
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine
Here's the Send:
if ($test == true) {
mail($to, $from, $subject, $message, $headers);
echo "<script>window.location = '/key-holders/index.php';</script>";
}
And here is what the email looks like:
<html><body><center><table bgcolor='#ededed' width='680px'><tr><td><img src='http://example.com/img/logo.png' /></td></tr><tr><td><p>User Name has left you a special message on The Website.<br /> Please go to <a href='http://example.com/message/5493536206ab2'>http://example.com/message/5493536206ab2</a> to view your message.</p></td></tr></table></center></body></html>
User Name Has a Special Message For You
Edit - Here's $newLine:
$newLine = "\r\n";
Remove $from parameter from mail function:
mail($to, $subject, $message, $headers);
Make sure, that $newLine = "\r\n";

custom php email settings?

I have this logic
$subject = "something.com Signup -
Please do not reply to this email. It was automatically generated.";
$body = "A new person has signed up to receive something updates:";
$headers = "From: webinquiries#something.com\n";
$headers .= "Reply-To: something#gmail.com\n";
// $headers .= 'Bcc: something#something.com' . "\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\n";
mail($email, $subject, $body, $headers);
which seems ok but one thing.... can i set the smtp info like this
server="smtp.something.net",
username="webinquiries#somthing.com",
password="asda.1sda",
port="587"
you can set the server in php.ini, but user\password as php's build in mail does not support authentication. you should look at a third party library (phpmailer) as the php mail() function's very under powered.

Sending Web Page through Email in php

as in asp we have function to send complete web page in email, which basically save lot of time for developer in creating & sending email
see the following code
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="xxx#example.com"
myMail.To="xxx#example.com"
myMail.CreateMHTMLBody "mywebpage.html",cdoSuppressNone
myMail.Send
set myMail=nothing
%>
as we know that CreateMHTMLBody will get data from mywebpage.html and send it as a body of email.
i want to know does any function like (CreateMHTMLBody) this is available in php ?
if Not can we crate any function if yes, please give me some hints.
Thanks
Example below:
<?
if(($Content = file_get_contents("somefile.html")) === false) {
$Content = "";
}
$Headers = "MIME-Version: 1.0\n";
$Headers .= "Content-type: text/html; charset=iso-8859-1\n";
$Headers .= "From: ".$FromName." <".$FromEmail.">\n";
$Headers .= "Reply-To: ".$ReplyTo."\n";
$Headers .= "X-Sender: <".$FromEmail.">\n";
$Headers .= "X-Mailer: PHP\n";
$Headers .= "X-Priority: 1\n";
$Headers .= "Return-Path: <".$FromEmail.">\n";
if(mail($ToEmail, $Subject, $Content, $Headers) == false) {
//Error
}
?>
To add to Erik's answer, if you want to import a local (or remote!) file instead of specifying the HTML in the code itself, you can do this:
// fetch locally
$message = file_get_contents('filename.html');
// fetch remotely
$message = file_get_contents('http://example.com/filename.html');
Use the output buffer functions of PHP and include the desired webpage. Example:
// Start output buffering
ob_start();
// Get desired webpage
include "webpage.php";
// Store output data in variable for later use
$data = ob_get_contents();
// Clean buffer if you want to continue to output some more code
// in which case it would make sense to use this functionality in the very beginning
// of your page when no other code has been processed yet.
ob_end_clean();
Here's how:
$to = 'joe#example.com';
$subject = 'A test email!';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Put your HTML here
$message = '<html><body>hello world</body></html>';
// Mail it
mail($to, $subject, $message, $headers);
You've just sent HTML email. To load an external HTML file replace $message = '' with:
$message = file_get_contents('the_file.html');

Categories