PHP insert cc into mail function [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP Mail, CC Field
I am using the following php to send an email. I need to add cc to my email. When I try to insert into header the html message show the raw html. What is the best way to handle a cc?
Thanks
$headers = "From: $from_email\r\nReply-To: $from_email";
$headers .= "Return-Path: <info#premierinspectiontn.com>";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

just tested this, and it worked as expected:
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
I would also suggest moving your carriage return and new line to the end of the previous entry to $headers
$headers = "From: $from_email\r\nReply-To: $from_email";
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
$headers .= "Return-Path: <info#premierinspectiontn.com>\r\n";
// add boundary string and mime type specification
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
hope that helps

Try adding
$headers .= 'CC: other#url.com' . "\r\n";

Related

PHP fails to send UTF-8 Messages [duplicate]

This question already has answers here:
How to send UTF-8 email?
(3 answers)
Closed 5 years ago.
I have the following PHP code:
<?php
$to = 'info#neatgr.cf';
$subject = date("d/m/Y");
$message = 'Hey 123 !## αβγ';
$headers = "From: testsite < mail#testsite.com >\n";
$headers .= "Cc: testsite < mail#testsite.com >\n";
$headers .= "X-Sender: testsite < mail#testsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail#testsite.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
mail($to,$subject,$message,$headers);
echo "<script type='text/javascript'>alert('Message sent! We will get back to you soon!');</script>";
echo "<script>window.location.href = 'http://example.com';</script>";
?>
The mail gets sent fine. The problem is that αβγ (Unicode Characters) doesn't get on the mail recipient's end correctly.
This is what the recipient sees: Hey 123 !## αβγ
This is what he should see: Hey 123 !## αβγ
I've looked everywhere and tried everything, changing headers, converting my string to unicode etc. etc., but nothing has worked. Maybe I'm doing something wrong?
Use the UTF-8 charset header, for example:
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Goes to:
$headers .= "Content-Type: text/html; charset=UTF-8\n";

Email does not display in HTML format [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I am using the following code:
$useremail="xxx#gmail.com";
$admin_email="yyy#gmail.com";
$sub="some meassage";
$content=<design with tables>.................;
$reval=mail($useremail,$sub,$content,'From:'. $admin_email));
The code sends the email correctly, but it displays in text format not HTML format which is what I want.
I am using Content-type:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'. $admin_email. "\r\n";
$reval=mail($useremail,$sub,$content,$headers);
In this method the email is not delivered.
Try to replace :
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
By :
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Full code example :
$useremail="xxx#gmail.com";
$admin_email="yyy#gmail.com";
$sub="some meassage";
$content = "Good morning,<br />
<strong>This is an HTML msg sent by php mail.</strong><br />
Thanks :)";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"";
$headers .= 'From:'. $admin_email. "\r\n";
$reval=mail($useremail,$sub,$content,$headers);
if($reval)
{
echo "mail sent";
}
else
{
echo "error.";
}
it's work for me.

Sending a mail with hebrew content

I'm trying to send an email with an hebrew content but I can't get it right.. that's the header that I use:
$headers = "From: $from" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
For example, if I want to send:
איזה יום יפה היום
And the result I get instead of hebrew text is:
טקסט: שדגשדגשדגשדג
According to this Wikipedia entry, the ISO/IEC 8859 series of standards define hebrew characters in part 8, not part 1. Thus, you must specify
$headers .= "Content-type: text/plain; charset=ISO-8859-8";
instead of
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
in your code.
Anyway, it might be a good idea to use the UTF-8 encoding:
$headers .= "Content-type: text/plain; charset=UTF-8";
This allows you to send e-mails containing characters from different scripts or use special characters, eg. math characters.
Try giving out with this headers:
$headers = "From: $from" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8";

How to create PHP email form with optional file attachment field

I was testing a PHP Form for my client, when I realized that file attachment wasn´t optional, I have the full code here (it´s pretty long). Form works fine, but if you don´t choose any file, the e-mail will never arrive to it´s destination. I want to send the mail anyway and receive it without the attach.
I´m using PHP and jForm for validation.
Here is the input tag for file attaching (there to many input tags at the form.php file to list them here)
<input type="file" name="adjunto" id="adjunto">
And here is the send.php file (the true form is too long to publish here, i´m writing a shorter version of it)
<?php
// THE USER CHOOSES FROM A JOB LIST
$busqueda = $_POST["busqueda-list"];
$cargo = $_POST["cargo-list"];
// FILLS PERSONAL INFORMATION
$nombre = $_POST["nombre"];
$apellido = $_POST["apellido"];
$sexo = $_POST["sexo-list"];
// WE STYLE A MESSAGE WITH LOTS OF INPUTS
$mensaje = 'In the real form here is a table with lots of input styled';
// AND THEN WE ATTACH (in spanish spells adjunto)
$adjunto_name=$_FILES["adjunto"]["name"];
$adjunto_type=$_FILES["adjunto"]["type"];
$adjunto_size=$_FILES["adjunto"]["size"];
$adjunto_temp=$_FILES["adjunto"]["tmp_name"];
// IF THERE IS A FILE ATTACHED
if (is_uploaded_file($adjunto_temp))
{if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
// IF NOT, JUST SEND MAIL
else
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
?>
Please I need a hand with this, I´m doing my best (i´ve already search here and other forums) and I think this maybe a problem many people had before!
Thank you!
you need to give one more condition on top
if (is_uploaded_file($adjunto_temp)) {
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
........... // your other stuff with respect to headers
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
}
}
else
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje);
now the mail will go on both conditions.
UPDATED WITH YOUR CODE
if (is_uploaded_file($adjunto_temp))
{
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
}
// IF NOT, JUST SEND MAIL
else
{
// MAIL HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
Put the else statement for your first if condition, in your code you are using else for the condition2.
if (condition1) {
if (condition2) {
.........
.........
}
} else {
}

send html email using php deliver message as html code

iam sending html message contains table
when i recive message on gmail,hotmail,yahoo account it works fine but when receiving on other client
ie Microsoft outlook it resolve it as html code !!
here is my email headers
'MIME-Version: 1.0' . "\r\n" .
'Content-Type: text/html;charset= UTF-8' . "\r\n" .
'From: me <me#client.com>'
I Always use this function ,and it helps
function sendHTMLemail($HTML,$from,$to,$subject,$fromname)
{
$headers = "From: ".$fromname." <".$from.">\r\n";
$headers.= "Reply-To: ".$fromname." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
mail($to,$subject,"",$headers);
}
I know this isn't an answer for your question, but I suggest using a mailing library, that will allow you to send mails much more easily, and supports features such as attachments, authentication, etc.
I recommend SwiftMailer which works great, is simple and well documented.

Categories