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

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

Related

Mail function in PHP does not allow me to send email to multiple recipients

I am trying to send emails to a team and some invited individuals to indicate their assigned cooperation. However, only the first recipient (the team) in the $to seems to be receiving an email. The invited individuals do not receive any emails. Can anyone tell what I did wrong?
<?php
/* Template Name: send emails */
include_once ('important.php');
function get_emails($campus) {
global $mydb;
$individuals = array();
$individuals = $mydb->get_results("SELECT * FROM individuals WHERE campus = '$campus'");
return array_map(function($e) {
return $e->individual_email; }, $individuals);
}
$team_email = 'teamA#gmail.com';
$campus = 'NY';
$individuals = get_emails($campus);
$to = $team_email. ', ' .implode(', ', $individuals);
$subject = 'Team invitation from ' . $team_email. '!';
$message = 'Hello everyone, you are now working together';
send_email($message, $subject, $to);
function send_email($message, $subject, $to, $from='admin#gmail.com', $from_name='Admin Team Creator') { $message = '';
$message .= '<html>';
$message .= '<body>';
$message .= $msg;
$message .= '-<br/>';
$message .= '<font face="tahoma, sans-serif" color="#0eafd6"><b>The Admin Team Creator</b></font><br/>';
$message .= '</body>'; $message .= '</html>';
$headers = ''; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: ' . $from . ' <' . $from_name . '>' . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
return mail($to, $subject, $message, $headers, '-f ' . $from); }
?>

PHP: Mail not sent

I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n";
// $headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}
Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com);
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';

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

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

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
}

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

Categories