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.
Related
$to = 'jay#mywhitecard.ph';
$subject = 'Coupon Claimed!';
$header = 'From: info#mywhitecard.ph';
$claimed = '<html><body>';
$claimed .= '<h1>Hello, World!</h1>';
$claimed .= '</body></html>';
mail($to, $subject, $claimed, $header);
I have this simple html php email but when it is sent everything including tags gets included.
googled tutorials show similar examples like the code i did, would like to know how I can only send the div content.
You need to add the Content-type to your header like this:
$to = 'jay#mywhitecard.ph';
$subject = 'Coupon Claimed!';
$claimed = '<html><body>';
$claimed .= '<h1>Hello, World!</h1>';
$claimed .= '</body></html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: info#mywhitecard.ph\r\n';
mail($to, $subject, $claimed, $headers);
I'm looking for a solution to put the e-mail's header like address, subject, date etc... Into the e-mail's message body. Like it works when forwarding an e-mail from any e-mail client.
Is there a good practice for it in PHP? I couldn't find it in the manual.
I'm using php with laravel.
$to = 'me#mydomain.com';
$subject = 'Subject';
$headers = "From: me# mydomain.com\r\n";
$headers .= "BCC: someonelese# mydomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= "<html><body>…Email Message 1…</body></html>";
mail($to, $subject, $message, $headers);
}
if(count($errors) == 0) {
$to = ($_POST['email']);
$subject = 'Subject';
$url = 'mydomain.caom';
$headers2 = "From: me# mydomain.com\r\n";
$headers2 .= "BCC: someonelese# mydomain.com\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>…Email Message 2…</body></html>";
mail($to, $subject, $message, $headers2);
echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
}
Can I just concatenate the headers into the body?
Yes you can!
You can do something like this
$to = 'me#mydomain.com';
$subject = 'Subject';
$headers = "From: me# mydomain.com\r\n";
$headers .= "BCC: someonelese# mydomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message_headers = str_replace("\r\n", "<br>", $headers);
$message = "<html><body>…Email Message 1…{$message_headers}</body></html>";
mail($to, $subject, $message, $headers);
The above will replace the new lines (\r\n) in $headers with the HTML <br> tag and the formatted headers will be added to the $message just before the closing <body> tag.
Also notice that I have removed the . from the $message .= "..."; and replaced it with $message = "...";
You dont need the . unless you are appending the $message variable.
i have a php script
<?php
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$message = 'mysitedomain.com';
$from = 'support#mysitedomain.com';
$headers = 'From:' . $from;
mail($to,$subject,$message,$headers);
echo 'Mail Sent.';
?>
When i run this code mail not send. If i change message to mysitedomaincom (without dot before com) the mail send succesfull.
Anybody have a solution for this?
This codes that tells the mailer and the recipient that the email contains well formed HTML that it will need to interpret
If you want you can change content of $message, now with this codes you can send HTML content mail.
<?PHP
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$headers = "From: Support <support#mysitedomain.com>" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>mysitedomain.com</h1>';
$message .= '</body></html>';
mail($to, $subject, $message, $headers);
?>
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
How can i add tag inside the email php ? for instance , i want to bold my $for_pass .Any idea ? thanks
$subject = "Password Recovery";
$message = "Hi! Your member password is <strong>$for_pass</strong>";
$from = "smilepartyplanner2014#gmail.com";
$headers = "From:" . $from;
// send mail
$mail_sent = #mail( $forgot_email, $subject, $message,$headers );
//echo "Thank you for sending us feedback";
?>
<script>
alert("Your password has been sent to your email address. ");
</script>
<?php
You need send MIME headers in your mail to tell its HTML, as follows:
$headers = "From:" . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail_sent = #mail($forgot_email, $subject, $message, $headers);
As you have it in the mail body but you need to declare content type on header
...
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From:" . $from;
the message body can have any inline styling you wish.