I would like to send an Email with PHP mail function but my HTML is being sent as plain text.
This is my code:
$from = 'admin#bla.com';
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$message = file_get_contents('cancelemail.php'); //Includes my HTML Page
thanks!
your headers should be array like:
$headers = array("Content-Type: text/html; charset=UTF-8", "From: $from ");
Also, there is no point setting content as php file, content inside will be read as html, the php code will not be executed;
Related
I am trying to send emails with HTML content and whenever i pass headers of any kind the method mail() return false but with no headers i get true .. any clue ?
$to = $user->email;
$name = $user->first_name.' '.$user->last_name;
$from = $this->config->item('admin_email', 'ion_auth');
$subject = $this->config->item('site_title', 'ion_auth') . ' - ' . $this->lang->line('email_forgotten_password_subject');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval = mail ($to,$subject,$message,$headers);
die(var_dump($retval));
Make sure that variables $name, $from, $to, $subject has the right data and are not empty/null. Your code is functional and should send the email.
If still doesn't work, check the configurations on postfix or what other mail server you're using.
And I suggest to use PhpMailer or SwiftMailer instead of simple mail function.
Try adding a CRLF after your last header. Used to work for me in the olden days.
i.e.,
...
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n";
...
Also, the header Content-Type has a capital T. Sometimes, such trivial things may also break it. That's why everyone suggests to move away from it.
That should do it.
Suggestion: Switch to PHPMailer.
I am trying to send few emails and ofcourse I need to breake some lines and put some spaces.
This is how I'm trying to do it, without luck:
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'New member message';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.com' . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
But the result leads to a single line text, including the \r\n in it as text - it is not inserting a new line.
How can I insert a new line? Why \r\n is not working?
I know that I'm missing a really, really small part here, but as a php beginner, I'm not able to spot it.
For Content-type: text/html, new line is <br>
For Content-type: text/plain, new line is \r\n
Try <br> tag before /r/n. hope it will work.
You have to use a simple
<br />
for a new line, the
\r\n
is just for the header
Try this code.
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'Testing sendmail.exe';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers))
{
echo 'mail send';
}
else
{
echo 'mail fail';
}
}
Change content type of header to 'Content-type: text/html; charset=iso-8859-1'
I've looked at the documentation and here's how I'm sending my email:
$headers = 'From: aaaaaa#aaa.com' . "\r\n" .
'Reply-To: aaaa#aaa.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail('aaaaa'aaaa.com', 'Contact: ' . $name . ' from ' .$company, $message, $header);
Where $message is a string containing several <br /> tags for line breaks.
I've set the Content-type to text/html so I would expect the email to arrive as HTML, but in my inbox it's clearly interpreted as just text (IE: I just see the text of my <br /> tags)
It's this
$headers = 'From: email#example.com' . "\r\n" .
^ - notice the s?
$company, $message, $header )
^ missing the s
I am trying this on wordpress, trying to email multiple email address. I have 2 email addresses in the database.
Here is the code:
$r = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_users", $ID, $user_email ));
foreach ($r as $row) {
$to = 'someone#myhost.com';
$bcc = $row->user_email;
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo "Email sent";
}
else {
echo "Email sending failed";
}
It's sending emails BUT what's happening is that the TO (someone#myhost.com) is getting 2 emails and the $bcc is getting none.
What am I doing wrong here?
Yep, this behavior it is pretty normal, you forgot to put in $headers the Bcc: part, it should be like:
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Bcc: '.$bcc. "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
My contact form is sending emails out but for someone reason, I am not receiving emails in my gmail account. I am receiving emails on other email providers like Yahoo. So I think that there is a problem with my headers.
$headers = 'From: ' .$email . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
You have a period instead of a semi-colon
'Reply-To: ' .$email . "\r\n" .
^
Change it to:
$headers = 'From: ' .$email . "\r\n" .
'Reply-To: ' .$email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
which is breaking your headers