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');
Related
How can I send an HTML-formatted email with pictures using PHP?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
The main problem is to attach files. How can I do that?
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob#example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.
Below is the PHP code for your reference: It’s taken from mail:
<?php
// Multiple recipients
$to = 'aidan#example.com' . ', '; // Note the comma
$to .= 'wez#example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have this code and it will run perfectly for my site:
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/>support#pms.com</td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
Mail function
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing#gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
Sending an HTML email is not much different from sending normal emails using PHP. What is necessary to add is the content type along the header parameter of the PHP mail() function. Here is an example.
<?php
$to = "toEmail#domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
You can also check here for more detailed explanations by W3Schools.
You can easily send the email with HTML content via PHP. Use the following script.
<?php
$to = 'user#example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info#codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome#example.com' . "\r\n";
$headers .= 'Bcc: welcome2#example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
The simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symfony.
You can do it with the standard mail function too, but you'll need a bit more knowledge on how to attach pictures.
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mail() function documentation.
Use PHPMailer.
To send HTML mail, you have to set $mail->isHTML() only, and you can set your body with HTML tags.
Here is a well written tutorial:
How to send mail using PHP
The trick is to know the content id of the image MIME part when building the HTML body part.
It boils down to making the img tag—<img src="cid:entercontentidhere" />
Kronolith.php
Look at the function buildMimeMessage for a working example.
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
I have the following function for sending an email:
function send_email($email){
$subject = "TITLE";
$message = "HERE IS THE MESSAGE";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <emaily>' . "\r\n";
mail($email,$subject,$message,$headers);
}
Instead of $message being a string, I want to call in the file email.html which holds my template.
I have added this:
require 'email.html';
But how can I call in the file?
$message = [call in email.html here]
Require is used when you want to call functions within another php file, or when you want to include some data to an HTTP response.
For this problem, file_get_contents('email.html') is the preferred option. This would be the method I would use:
function send_email($email){
$subject = "Subject of your email";
$message = "";
if(file_exists("email_template.html")){
$message = file_get_contents('email_template.html');
$parts_to_mod = array("part1", "part2");
$replace_with = array($value1, $value2);
for($i=0; $i<count($parts_to_mod); $i++){
$message = str_replace($parts_to_mod[$i], $replace_with[$i], $message);
}
}else{
$message = "Some Default Message";
/* this likely won't ever be called, but it's good to have error handling */
}
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <doNotReply#myDomain.com>' . "\r\n";
$headers .= "To: <$email>\r\n";
$header .= "Reply-To: doNotReply#myDomain.com\r\n";
mail($email,$subject,$message,$headers);
}
I modified your code a little bit and added in both the file_get_contents and file_exists. file_exists confirms that the file is there. If it's not, it avoids the potential error from trying to read it in and can be changed to use some default. My next addition was a for loop. In the $parts_to_mod array, enter in the default values from the template that need to be replaced. In the $replace_with array, put in the unique values that you want to replace parts of the template with.
As an example where I use this, I have a template URL for one of my programs that says id=IDENTIFIER&hash=THEHASH so in my program, my parts_to_mod says $parts_to_mod = array("IDENTIFIER", "THEHASH"); and my replace_with says $replace_with = array($theUsersIdentifier, $theUsersHash);. It then enters the for-loop and replaces the those values in parts_to_modify with the values in replace_with.
Simple concepts and they make your code much shorter and easier to maintain.
Edit:
Here is some sample code:
Let's the say the template is:
<span>Dear PUTNAMEHERE,</span><br>
<div>PUTMESSAGEHERE</div>
<div>Sincerely,<br>PUTSENDERHERE</div>
So, in your php code you'd say:
$parts_to_mod = array("PUTNAMEHERE", "PUTMESSAGEHERE", "PUTSENDERHERE");
$replace_with = array($name, $theBodyOfYourEmail, $whoYouAre);
just use file_get_contents('email.html') This method returns a string with the file contents
You can use this function to call custom email template.
function email($fields = array(),$name_file, $from, $to) {
if(!empty($name_file)) {
$mail_tem_path = 'templates/mails/mail--'.$name_file.'.php'; //change path of files and type file.
if(file_exists($mail_tem_path)) {
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-Type: text/html;charset=UTF-8". "\r\n";
// Additional headers
$headers .= "From:".$fields["subject_from"]." <$from>". "\r\n";
$headers .= "Content-Transfer-Encoding: 8Bit". "\r\n";
$message = file_get_contents($mail_tem_path);
$message = preg_replace("/\"/", "'", $message);
foreach ($fields as $field) {
// Replace the % with the actual information
$message = str_replace('%'.$field["name"].'%', $field["value"], $message);
}
$send = mail($to, $fields["subject"], $message, $headers);
} else {
$send = mail($to, "Error read mail template", "Contact with admin to repair this action.");
}
} else {
$send = mail($to, "Error no exist mail template", "Contact with admin to repair this action.");
}
return $send;
}
Template html
<html>
<body>
TODO write content %value_on_array%
</body>
</html>
Array and execute function.
$fields = array(
"subject" => "tienda_seller_subject",
"subject_from" => "tienda_email_subject_from",
0 => array(
"name" => "value_on_array",
"value" => "result before change"
),
);
//email($fields = array(),$name_file, $from, $to);
email($fields, 'admin', 'owner#email.com', 'client#email.com');
Result
I have a strange problem in chrome, I am using window.open to open a new tab and create a pdf using tcpdf and then send it as attachment to a specific address, now in chrome what happens is that it sends 2 mails. If popup is enabled then I get 2 window actions 1. Popup and 2. New Tab, firstly I don't understand how is that possible, I have never seen this.
Then If is disable the popup, it only opens in new tab, but still 2 mails.
Code used to call the popup page and email code is mentioned below. Please help.
window.open('/loancal/rhexportemail.php' + qstring,"_blank");
//Code used to open popup
//------------- EMAIL CODE ----------------//
$to = "someone#gamil.com";
$from = "info#mydomain.com";
$subject = "Loan Enquiry Calculation Cashback - " . $client;
$message = "Please find attached Loan Enquiry Calculation Cashback statement for ".$client."." ;
//a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = $subject.".pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output('LECC'.$separator.'.pdf', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
if(#mail($to, $subject, $message, $headers))
{
echo "<script type='text/javascript'> alert('mail sent');window.close();</script>";
}
else{
echo "<script type='text/javascript'>alert('mail not sent');window.close(); </script>";
}
I strongly believe that you somehow call this twice.
Hi I am trying to send an entire webpage via e-mail. I receive it in Mail.app in my Mac and i can see it fine. But when i see it in gmail or hotmail, It didn't get the styles
How can I do this correctly. Or how to transform the webpage to PDF or PNG and send it via Mail [I can't install anything to the server]
Edit: It send all the information but without Style
My code:
$mail = $_POST['mail'];
if(($Content = file_get_contents("http://google.com")) === false) {
$Content = "";
}
$Headers = "MIME-Version: 1.0\n";
$Headers .= "Content-type: text/html; charset=iso-8859-1\n";
$Headers .= "From: a#a.com <a#a.com>\n";
$Headers .= "Reply-To: a#a.com\n";
$Headers .= "X-Sender: <a#a.com>\n";
$Headers .= "X-Mailer: PHP\n";
$Headers .= "X-Priority: 1\n";
$Headers .= "Return-Path: <a#a.com>\n";
if(mail($mail, $subject, $Content, $Headers) == false) {
//Error
}
Thanks
Remove all external style sheets . All the styles on the page should be inline or defined within the page