How can i add cc to mail function? [duplicate] - php

This question already has answers here:
PHP Mail, CC Field
(3 answers)
Closed 7 years ago.
this is my code, But i am getting blank mails also. what was the wrong with this?
'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'To: <someone#gmail.com>, <anotherone#gmail.com>' . "\r\n";
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;

Please read documentation mail function Here they explained detailedly.
Particularly Example #4 Sending HTML email
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);

Related

php mail not showing senders email address

I¨ve got a problem answering mails sent from my website, as the senders emailaddress isn´t showing anywhere, only the webmasters email
Here is my code:
<?php>
$from="kim.s.nielsen#mail.dk";
$email="info#intotext.dk";
$name=$_POST['name'];
$message=$_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" . 'Reply-To: '. $fromEmail . "\r\n" . mail($email, $name, $message, $headers);
print"Din besked er sendt. Vi vender tilbage så hurtigt som muligt." ?>
Use this format to send your email, this will show the senders email address, try this. you have concatenated the mail function with your header, mail() is not header it is used to send your email.
$fullname = "full name";
$from = "sender#mail";
$to = "reciever#mail";
$subject = "Your subject";
$message = "<h1> heading </h1><p> Message </p>";
$headers = [
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1',
'From: ' . htmlspecialchars($fullname) . " <$from>",
'X-Mailer: PHP/' . phpversion()
];
mail($to,
htmlspecialchars($subject),
nl2br(htmlspecialchars($message)),
implode("\r\n", $headers)
);

Sending email headers

I have such code for send email:
$email = $_POST['message_email'];
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
before that I check whether the email is correct so it has to be.
Then I send an email:
$sent = wp_mail($newTo, $subject, $companyName . "\n" . $email . "\n" . $phoneNumber . "\n" . strip_tags($message) . "\n" . $outputMail, $headers);
and it doesn't work. I've tried change headers to:
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n\\";
and then it works. Why my code does't work? I need to provide that email retreived from form.
Try like this:
****Ive edited the code****
$from = "your#mail.com";
$to = "to#mail.com";
$message = "Message";
$subject = 'subject';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
if(mail($from, $subject, $message, $headers)){
//success
}else{
//error
}

Why is PHP's mail() sending me dozens of blank emails?

I'm using PHP's mail() function within a WordPress theme to send form submissions to my email.
However, mail() has automatically sent me at least 20 blank emails,
within the last five minutes, without being submitted.
Could someone shed some light on what I've done to create this situation?
Here is the code from my (functions.php file)
function send_my_form(){
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
add_action('wp_head', 'send_my_form');
This is what I mean by running it in a conditional. Verify the fields are set before running the mail script.
function send_my_form(){
if(isset($_POST['fstname']) && isset($_POST['lstname']) && isset($_POST['email']) && isset($_POST['message']))
{
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
}

Multiple recipients with PHP script?

So the issue is I want multiple recipients for my PHP form.
What happens is the site user enters there email and then another email for another peroson (for example there doctor).
So what I need is the the doctor to be emailed to.
This is what I am using to know success
$mail_to = $field_emaildoc .$field_email;
This doesent seem to work?
Any ideas would be great :)
Thanks
one option is to add a "Cc" to your header:
$sender_email = 'email#domain.com';
$sender_name = 'YOUR NAME';
$send_to = 'email#domain.com';
$send_to_copy = 'anotheremail#domain.com';
$message = 'THIS IS YOUR MESSAGE';
$subject = 'THIS IS YOUR SUBJECT';
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= "From: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "X-Sender: $sender_name<" . $sender_email . ">" . "\n";
$headers .= "X-Mailer: PHP " . phpversion() . "\n";
$headers .= "X-Priority: 3" . "\n";
$headers .= "X-Sender-IP: " . $_SERVER['REMOTE_ADDR'] . "\n";
$headers .= "Return-Path: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Reply-To: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Cc: $send_to_copy" . "\r\n";
mail($send_to,$subject,$message,$headers);
The problem with this, is that the person receiving the email can see who was copied in. An alternative would be to use: "Bcc" instead of "Cc" or just use the mail() function twice and remove the "Cc" or "Bcc":
mail($send_to1,$subject,$message,$headers);
mail($send_to2,$subject,$message,$headers);
You should put comma between mail addresses . Look explanation of to parameter, here : http://php.net/manual/en/function.mail.php
You'll need a comma:
$mail_to = $field_emaildoc . ',' . $field_email;
Use the default mail function from PHP.
The recipients are devided by a Comma.
When you want CC and BCC you can set the header. Example from PHP.net:
$header .= 'To: Simone <simone#example.com>, Andreas <andreas#example.com>' . "\r\n";
$header .= 'From: Geburtstags-Erinnerungen <geburtstag#example.com>' . "\r\n";
$header .= 'Cc: geburtstagsarchiv#example.com' . "\r\n";
$header .= 'Bcc: geburtstagscheck#example.com' . "\r\n";
mail($empfaenger, $betreff, $nachricht, $header);

Why is the From line not working on my mail function?

I am using a mail function to send html to an email address, but the From name and email address aren't showing up. This is my code:
$name = $_POST['name'];
$mailTo = 'name#email.com';
$subject = 'Message from ' . $_POST['name'];
$message =
'<html>
<head>
<title>HTML email</title>
</head>
<body>
<p><b>Name:</b> ' . $_POST['name'] . '</p>
<p><b>Email:</b> ' . $_POST['email'] . '</p>
<p><b>Message:</b> ' . $_POST['mainmessage'] . '</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Postmaster <some#body.com>';
mail($mailTo, $subject, $message, $headers);
I would expect the email to show up as being from Postmaster at the email address some#body.com, but it is showing up as coming from ideapale#box486.bluehost.com, which is my hosting provider.
What did I not set up correctly?
Chris, try adding the \r\n after the <some#body.com>. I've found that php can be very picky when talking to mail servers.
Edit: just to help a little more, I have this (almost exactly what you have) in one of my working scripts:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$to . "\r\n";
$headers .= 'From: ' .$from. "\r\n";
...where $from = $fromname.' <'.$fromemail.'>'; and $to is just an email address.

Categories