sendemail.php is not working [duplicate] - php

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.

Related

Email sending function not sending data

While I am sending any mail, this will return only name: from: message: subject : , but all these fields remain blank..
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'me#example.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);
die;
Without knowing your environment, mail function accepts the headers argument as a string, not an array.
mail($to, $subject, $message, implode("\r\n", $headers));
http://php.net/manual/en/function.mail.php

warning: mail() expects parameter 4 to be string

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";

Undisclose recipients php without any extensions only mail() function

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);

PHP Mail Sending BCC not wordking

I am using PHP mail and trying to send BCC, but for some reason since I've added the lines with //ADDED NEW on it , it's just now sending any emails at all.
Here is the full code:
$to = "me#gmail.com";
$bcc = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $emailList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $bcc, $subject, $message, $headers); // $bcc ADDED NEW
Why is this not sending?
Your problem,
Set $bcc but in $headers does not using it
Putting invalid argument into mail function.
Try this
$to = "me#gmail.com";
$bccList = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $bccList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers); // $bcc ADDED NEW
There's no $bcc argument to the mail() function. It should be:
mail($to, $subject, $message, $headers);
The blind recipients will be retrieved from the Bcc header in $headers.

unable to set header in php and send email from cpanel

I am unable to set header in php for from address I have tried all possible values :
The code below is correct but code is not attached to header .
It shows this as default from but does not change.
From : webhost:cpanel#hosting
Is this the problem with cpanel ?
Please help me out really appreciate the help.
Thanks in Advance.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['contact'];
$subject = "feedback";
$question = $_REQUEST['question'];
$body = "<html>
<head>
</html>";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
$headers .= 'From:'.$email. "\r\n";
$to ='example#example.com';
mail($to,$subject,$body,$headers);
echo "<script>alert(' message sent.');</script>";
?>
also tried :
<?php
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender#domain.com>";
$headers[] = "Reply-To: Recipient Name <receiver#domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $email, implode("\r\n", $headers));
?>
Only this works:
$to ='example#example.com';
$subject="subject";
$body="body";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
mail($to,$subject,$body,$headers);
try this :
$to = 'me#gmail.com';
$subject = "Email from me by: John";
$body = "my body";
$headers = "From: "."hello#world.com"."\r\n" .
"Reply-To: "."hello#world.com"."\r\n".
"X-Mailer: php";
$sent=mail($to, $subject, $body, $headers);
if ($sent) {
echo "good!";
} else {
echo("<p>Message delivery failed...</p>");
}

Categories