how to send html email using php - php

I would like to use <<< to send html email in php. So far i remember it works great previously but not working right now.
//ALL HTML MUST BE LEFT ALLIGNED.
$php_var="variable value";
$body = <<<EmailBody
<html>
<body>
$php_var
</body>
</html>
EmailBody; //EmailBody will not show in Email.
$headers = 'From: info#mydomain.com' . "\r\n" .
'Reply-To: info#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject="Test HTML Email";
$body="test email from mydomain";
$to="aminulsumon#gmail.com";
mail($to,$subject,$body,$headers); //$header type should be html
any help is highly appreciated.

There should NOT be anything before AND/or after EmailBody;; it being the closing identifier.
Read the documentation on heredoc
Use this:
//ALL HTML MUST BE LEFT ALLIGNED.
$php_var="variable value";
$body = <<<EmailBody
<html>
<body>
$php_var
</body>
</html>
EmailBody;
$headers = 'From: info#mydomain.com' . "\r\n" .
'Reply-To: info#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject="Test HTML Email";
$body="test email from mydomain";
$to="aminulsumon#gmail.com";
mail($to,$subject,$body,$headers); //$header type should be html
Having something (space, text, etc.) after the closing identifier, will result in the following error:
Parse error: syntax error, unexpected end of file in....(path to file) on line X
had error reporting been set
http://php.net/manual/en/function.error-reporting.php
Add this just before your opening <?php tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);

Remove the comment on the end of:
EmailBody; //EmailBody will not show in Email.
So it is only:
EmailBody;
Also you define $body twice, so take out:
$body="test email from mydomain";

Here is one basic example to send html in email -
$to = 'abcd#gmail.com';
$from = "sender#gmail.com"; // sender
$subject = "Test email";
$message = '<html><body>';
$message .= "<p>This is The Email Address</p><br><span class='nonLink'>responder#example.com</span>";
$message .= '<br/>click here to complete your registration';
$message .= '</body></html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
// Additional headers
$headers .= "From: <$from>" . "\r\n";
// Mail it
if (mail($to, $subject, $message, $headers)){
echo "email sent successfully";
}

Related

sending mail through phpmailer and its working properly but the problem is i have inserted one templet in it and i am receiving code of that templet

<?php
function sendCOTP($REmail,$RVID) {
$to = $REmail;
$subject = 'Allloooooooooooo Bhindiiiiiii';
ob_start();
include './mailtemplet.php';
$body = ob_get_clean();
$message = 'Dear,sir'
. 'Guess what ??? '.$RVID.' venue that you checked earlear is now finallyyy available so check it out and book it before again it get booked '
. 'Thank you'
. 'team venueazy';
$headers = 'From: bookvenue01#gmail.com' . "\r\n" .
'Reply-To: bookvenue01#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject,$body, $message, $headers);
}
?>
so here everything is working fine but the templet which i have included that is not working like i am getting html code instead of that templet so need help with that how can i solve this issue.
thank you in advance.Mail screenshot where i am getting html code instead of templet
need help is how can i make "IsHTML(true);" in my code?
It looks like you forgot to include a header when submitting, add this to your code after Reply-To
$headers .= "Content-Type: text/html;\r\n";
use this meta tag inside your head tag .you must specify content type.else it can be take it as string.that's why your whole code displayed in mail.
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
Add this line
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
This link will help you.
Send HTML in email via PHP
function sendCOTP($REmail,$RVID) {
$to = $REmail;
$subject = 'Venue is Available';
$headers = 'From:bookvenue01#gmail.com';
$headers .= 'Reply-To: bookvenue01#gmail.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$email_template = './mailtemplet.html';
$message = file_get_contents($email_template);
mail($to, $subject, $message, $headers);
}

How to protect against head injection

I am new here and this is my first post. Unfortunately, I am not familiar with php coding and so I need help for the following script. I would like to use this code on my website containing download files. I want to add a link or button next to the download link. When clicking the link the script should be executed and send an email to me with a given text.
Now, I read that this code could be a victim to header injection. As I am not familiar with php I do not know what to change to be protected. Is there anyone who might help me out with a solution? This is the code:
<?php
$to = 'name#example.com';
$subject = 'Broken Download-Link';
$from = 'Subject-Title <name#example.com>';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h2 style="color:#080;font-weight:normal;">Hello!</h1>';
$message .= '<p style="color:#000;font-size:18px;font-weight:normal;">Text here:</p>';
$message .= '<p style="color:#f40;font-size:22px;font-weight:bold;">Another text here</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Thank you in advance for any help.
Best regards,
Feechen

PHP mailer unable to send html messages

I tried sending HTML mails through PHP but nothing is working. I tried two options.
One with file_get_contents:
<?php
$to = 'teas#gmail.com';
$subject = 'Marriage Proposal';
$from = 'support#blabla.com';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = file_get_contents("email_template.html");
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
And one with HTML in a PHP string:
<?php
$to = 'anthony#gmail.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
The response for both functions is:
Unable to send email. Please try again. Unable to send email. Please
try again.
Can anyone tell me whats wrong?
Just enable the HTML in php mailer
$mail->isHTML(true);
For refrence please see here the example in github

PHP 500 Internal Server Error- mail function

I'm getting the 500 Internal Server Error. I called goDaddy and they told me it's not something on their end. I also don't have a .htaccess file. I checked the contact page and checked spelling it is calling the right .php file.
This code was working perfectly. I tried to change something it didn't work. I tried going back to the original code and now that doesn't work either. I don't have a backup. I commented out line by line to pin-point the problem if I erase the mail($to, $subject, $message, $headers); part I don't get an error but obviously don't receive anything. Here is the website if that helps to inspect.
http://www.crownjewelre.com/contacts.html
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
// multiple recipients
$to = 'michaelgaraysi#yahoo.com' . ', '; // note the comma
// subject
$subject.= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
// message
$message = "
<html>
<head>
<title>Contact Information</title>
</head>
<body>
<table>
<tr>
<td>Name: </td><th>".$first_name."</th><th>".$last_name."</th>
</tr>
<tr>
<td>Email: </td><th>".$email."</th>
</tr>
<tr>
<td>Telephone: </td><th>".$telephone."</th>
</tr>
</table>
<br><br>
<p>Comments: <strong>".$comments."</strong></th>
</body>
</html>
";
// 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";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
like the others $subject.= is auto-expand of a non existing var but that not the problem here :)
1st : have you tested a blank page with only the function mail :
mail ('michaelgaraysi#yahoo.com','test','message corpus');
if its working its not a missconfig of youre PHP.
2nd : How can you use in 2015 the mail function, its have a high spam rate, instead use https://github.com/PHPMailer/PHPMailer or other.
I think it will solve your problem or maybe you just need to remove the "\n"; on $subject :)
try to change,
$to = 'michaelgaraysi#yahoo.com' . ', ';
to
$to = 'michaelgaraysi#yahoo.com';
and
$subject.= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
to
$subject= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
and finally, try to run this simple php mailer script, and then apply your changes,
<?php
$to = 'michaelgaraysi#yahoo.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: birthday#example.com' . "\r\n" .
'Reply-To: birthday#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

How to display my html page in mail box

<?php
$to = 'abc#gmail.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = file_get_contents('http://domain.com/newsletter.html');
$headers = 'From: xyz#gmail.com' . "\r\n" .
'Reply-To: xyz#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
This in my mail code. Here I am sending my newsletter to my existing customer.
Can anyone help me how can I send/open the newsletter as the page through mail ?
I suggest you use the following, yet you won't be able to use an http:// call, because this goes against security reasons.
By doing so, anyone could include whatever they want from whatever website. The file must reside on your server in order for the following to work.
Making use of the include(), ob_start() and ob_get_clean() functions.
You must use a relative path to the file you wish to include.
<?php
ob_start();
include 'file.xxx';
$message = ob_get_clean();
$to= 'email#example.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: email#example.com' . "\r\n" .
'Reply-To: email#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo "Message sent.";
}
else{
echo "There was a problem. Check your logs.";
}
?>
Footnotes:
I quote jsalonen from a comment: "Keep in mind that CSS support in email readers is (probably intentionally) very limited: campaignmonitor.com/css - so if it works in a regular browser, it may or may not work in an email reader."
Most Email clients such as Google will ignore CSS, so it's best to use inline CSS.
Images:
Another thing you will need to do is, if you are using images, then that's where you will need to use an http:// call to the image location(s).
I.e.: <img src="http://www.example.com/images/image_1.jpg">
if allow_url_fopen is available in the ini file, you can do this:
$html = file_get_contents('http://domain.com/newsletter.html');
$message = $html;
The reason this should work is because most email clients can read emails sent as HTML (as long as you're sending emails as HTML and not just plaintext emails).
You can use echo htmlspecialchars($html); to see what HTML you're sending.

Categories