<?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.
Related
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
This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 4 years ago.
This is in code-igniter. To send mail I have written a function using php mail(). The code is:
function mailsend()
{
$to="mymail#gmail.com";
$subject="test";
$message="<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>";
echo $message;
mail($to,$subject,$message);
}
When I send mail from server side, I get the html code in my mail, not the real table. But the $messageshows the real table in browser. Is there any way in mail() to get the real table in the mail?
You can send the html content in email via setting the header portion. Below is the sample code you can use.
<?php
$to = 'maryjane#email.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.';
}
?>
In order to send html content in your email with mail function you will need to add header options to it, like so:
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: test#yourdomain.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($sendTo, $subject, $message, $headers);
Add an header to your mail function like this
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: hello#example.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
My problem begins when I had to add a single landing page to an existing Drupal website. I've never worked with this CMS, so I just created a folder for this page in the root folder and put all the content there.
Then it appeared that I need to send mail from this page after submitting a form there. As I understood I can't use drupal_mail() function there, so I tried something like that:
$to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
But that gave me no result.
Then I installed SMTP auth module for Drupal and tried to send mail, but again I had no result. However, test messages from SMTP module have been sent correctly.
<?php
$to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\n";
$headers .= "From: $email" . "\n";
$headers .= "Reply-To:: $email" . "\n";
mail($to, $subject, $message, $headers);
?>
Some hosts require you to use an additional parameter, -f, to send mail.
Try:
mail($to, $subject, $message, $headers, "-f".$email);
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";
}
I am having a problem sending emails when I add header information.
However when I just remove the header parameter it works. What is wrong? Is it the code? Or some setting I need to change on the web server admin panel to say "Allow-Headers" or something?
I am trying to send to hotmail in case this has any relavance in determining the problem.
Any help would be greatly appreciated. Thanks.
Below Doesn't Send Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message, $headers);
?>
Below Sends Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message);
?>
I use these headers in my php mailing function and it works well. Note: I also use a third party mail-routing service to avoid having my mails marked as coming from a spammy IP. You might want to look into that also.
$headers = 'From: '.$from.'#foo.net' . "\r\n" .
'Reply-To: '.$from.'#foo.net' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
I also use the optional fifth parameter to mail() to set the envelope address, e.g.:
$parameters = '-f '.$from.'#foo.net';
so the final call is:
mail($to, $subject, $message, $headers, $parameters);
You can just delete the "FROM:" from the headers list .. it prevents it in some hosts .But the real question then will be how ca I change the sent from email address to a specific email that I want