I'm trying to use the below code to send mail, but got the below error
Doesn't mail() take in arrays for headers?
Warning: mail() expects parameter 4 to be string, array given in ../email.php on line 16
code :
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'example#gmail.com';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
PHP mail() need string $to, string $subject, string $message, string $headers
if you want to use an array for the headers
mail($to, $subject, $message, implode("\r\n", $headers));
else change your code in
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
why \r\n (from mail() documentation)?
Multiple extra headers should be separated with a CRLF (\r\n) [...]
If messages are not received, try using a LF (\n) only. Some Unix mail
transfer agents (most notably » qmail) replace LF by CRLF
automatically (which leads to doubling CR if CRLF is used). This
should be a last resort, as it does not comply with » RFC 2822.
side note use "\r\n" and not '\r\n'
As the message says the 4th parameter ($additional_headers) should be a string. So you need to join the array elements:
$headers = implode("\r\n", $headers);
mail($to, $subject, $message, $headers);
From the documentation:
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.
You make $headers to be a array, but it has to be a string! So just change these lines:
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
to this and concatenate the strings together:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
Related
I have a website contact form that sends as an email to my iPhone. The problem is I am getting the following message:
"This message cannot be displayed because of the way it is formatted. Ask the sender to send it again using a different format or email program. multipart/mixed"
Is this has something to do with email content-type and how to fix it in my PHP code.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
$sentMail = mail($recipient_email, $subject, $body, $headers);
You specify the Content-Type as a header string.
Assuming you are appending your headers with the standard dot notation, you would use:
$headers .= "Content-Type: multipart/mixed";
And later this would become the fourth parameter of your mail() function:
mail($address, $subject, $message, $headers);
Here's a basic example:
$address = 'sample#sample.com';
$subject = 'Subject';
$message = 'Message';
$headers = "From: My Name<something#something.com>\n";
$headers .= "Reply-To: something#something.com \n";
$headers .= "Content-Type: multipart/mixed";
mail($address, $subject, $message, $headers);
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
This is my sendemail.php. It is not working. Are there any mistakes on it ?
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'richardhenokh#gmail.com';//replace with your email
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
try this. It is working fine
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'imaphpdeveloper#gmail.com';//replace with your email
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=iso-8859-1";
$headers .= "From: {$name} <{$from}>";
$headers .= "Reply-To: <{$from}>";
$headers .= "Subject: {$subject}";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
?>
Also you can try PHPmailer. headers should be a string not an array. Also {$from} etc.. in headers should be replaced properly. Now using above code mail is sending. But have to replace {$from} properly.
I am trying to not disclose the recipients when I send an email but I am failing on this:
$email = implode('; ', $email); // array where I have the emails
$to = $email; // webmaster#domain.com; webmaster#anotherdomain.com
$subject = 'Subject';
$headers = "From: noreply#domainfromwhereisendemail.com\r\n" . "X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: Undisclosed Recipients <noreply#domainfromwhereisendemail.com>\r\n";
//$headers .= "Cc: $to\r\n";
$message =
<<<START
This is the message
START;
mail($to, $subject, $message, $headers);
I tried adding Cc or Bcc, but not working, it is adding besides the emails Undisclosed recipients. I am trying to do this without any other extensions, I checked a lot of questions from stackoverflow here but did not accomplish this. Emails are still being shown to each.
Nope you can't stop that from nondisclosure, unless you send the emails separately using a loop.
$email = implode('; ', $email); // <---- Don't do this.
The loop up way..
foreach($email as $mail) #<---- Use a foreach and loop through
{
$to = $mail;
$subject = 'Subject';
$headers = "From: noreply#domainfromwhereisendemail.com\r\n" . "X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message =
<<<START
This is the message
START;
mail($to, $subject, $message, $headers);
}
Here is the ôkio solution with a single call to mail():
$subject = 'Subject';
$headers = "X-Mailer: php\r\n";
# $headers .= "MIME-Version: 1.0\r\n"; # do you really need that?
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
foreach($email as $mail){
$headers .= "Bcc: ".$mail."\r\n";
}
$message =
<<<START
This is the message
START;
mail('non#existing.email', $subject, $message, $headers);
I am trying to figure out why the bcc part of this PHP mail function is not working in the code below:
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = "Bcc: <support#mydomain.com>";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
I wouldn't think that there should be any problem specifying a bcc email address that is the same as the From address, but I'm not sure.
When I test this function, the recipient receives the message, but the BCC copy does not come through. Any idea why? Thanks.
Seriously, don't use the mail() function -- you're just letting yourself into a world of hurt.
If you want to do anything beyond absolutely the most basic email, I strongly recommend using a decent mail class, such as phpMailer.
It will make things much easier. No more messing around building the headers yourself, or trying to get the mime types working. Sending to multiple addresses, CC and BCC addresses becomes simple, and adding attachments goes from virtually impossible with mail() to dead simple.
Hope that helps.
try this one in your script you have to change "" to '' and also remove <> then it will be work here i edited your script
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = 'Bcc: support#mydomain.com';
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
here my mail function example
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: mydemo.com<$your_email>\r\n" .
$headers .= 'Bcc: mydemo#mydemo.com' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
Now If You Want To Use Html In Message
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
For Exampe Message
$message .='<table><tr><td></td></tr></table>';
Try to use Cci like this example:
$headers = array(
'From' => $from,
'To' => $to,
'Cci' => $bcc,
'Subject' => $subject
);
it working fine, i have tested the script found that email drop in Junk box.
try to add name of email holder :
"Bcc: Support <support#mydomain.com>";
function sendEmail($address,$subject,$message)
{
$headers = "Reply-To: miloAds Team <admin#miloads.com>\r";
$headers .= "Return-Path: miloAds Team <admin#miloads.com>\r";
$headers .= "From: miloAds Team <admin#miloads.com>\r";
$headers .= "Organization: Milonas Media LLC\r";
$headers .= "MIME-Version: 1.0\r";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r";
$headers .= "X-Priority: 3\r";
$headers .= "X-Mailer: PHP". phpversion() ."\r";
mail($address, $subject, $message, $headers);
}
When sending out an email, the header is appearing in the body.
Try changing each of the \r escapes to \r\n and see if that helps.
Quoth the PHP manual:
additional_headers (optional)
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc).
Multiple extra headers should be separated with a CRLF (\r\n).
Make sure to not include the trailing \r\n on the last header either.
Also make sure to strip any newlines from the $subject as that could cause problems. See if those help.
Add \n to \r, i.e. \r\n and remove the last one:
function sendEmail($address,$subject,$message)
{
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: miloAds Team <admin#miloads.com>\r\n";
$headers .= "Reply-To: miloAds Team <admin#miloads.com>\r\n";
$headers .= "Organization: Milonas Media LLC\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion();
mail($address, $subject, $message, $headers);
}