send copy of message (HTML/PHP) - php

I have a php file that needs to send an email to me if the contact section of my contact page has been filled in. And a checkbox whether to send a copy of the message or not. But for some reason if the checkbox is checked, the sender/maker of the message is not receiving a copy of this message in his mailbox.
See code below.
Kind regards
PHP file being called
<?php
//////////////////////////
//Specify default values//
//////////////////////////
//Your E-mail
$your_email = 'info#sano-tech.be';
//Default Subject if 'subject' field not specified
$default_subject = 'Van contact formulier';
//Message if 'name' field not specified
$name_not_specified = 'Geef een correcte naam in';
//Message if 'message' field not specified
$message_not_specified = 'Geeft een correcte boodschap in';
//Message if e-mail sent successfully
$email_was_sent = 'Versturen geslaagd!';
//Message if e-mail not sent (server not configured)
$server_not_configured = 'Sorry, mail server niet geconfigeerd';
///////////////////////////
//Contact Form Processing//
///////////////////////////
$errors = array();
if(isset($_POST['message']) and isset($_POST['username'])) {
if(!empty($_POST['username']))
$sender_name = stripslashes(strip_tags(trim($_POST['username'])));
if(!empty($_POST['message']))
$message = stripslashes(strip_tags(trim($_POST['message'])));
if(!empty($_POST['email']))
$sender_email = stripslashes(strip_tags(trim($_POST['email'])));
if(!empty($_POST['subject']))
$subject = stripslashes(strip_tags(trim($_POST['subject'])));
if(!empty($_POST['nummer']))
$nummer = stripslashes(strip_tags(trim($_POST['nummer'])));
$sendcopy = $_POST['sendcopy'];
//Message if no sender name was specified
if(empty($sender_name)) {
$errors[] = $name_not_specified;
}
//Message if no message was specified
if(empty($message)) {
$errors[] = $message_not_specified;
}
$from = (!empty($sender_email)) ? 'From: '.$sender_email : '';
$subject = (!empty($subject)) ? $subject : $default_subject;
//$message = (!empty($message)) ? wordwrap($message, 70) : '';
$message = "
Onderwerp: $subject
Naam: $sender_name
E-mail: $sender_email
Nummer: $nummer
Boodschap:
$message
";
//sending message if no errors
if(empty($errors)) {
if($sendcopy == "yes")
{
if (mail($your_email, $subject, $message, $from) && mail($sender_email, $subject, $message, $from)) {
echo $email_was_sent;
} else {
$errors[] = $server_not_configured;
echo implode('<br>', $errors );
}
}
else{
if (mail($your_email, $subject, $message, $from)) {
echo $email_was_sent;
} else {
$errors[] = $server_not_configured;
echo implode('<br>', $errors );
}
}
} else {
echo implode('<br>', $errors );
}
} else {
// if "name" or "message" vars not send ('name' attribute of contact form input fields was changed)
echo '"naam" en "bericht" variabelen zijn niet ontvangen door de server. Gelieve attributen hun "naam" te controleren';
}
?>
HTML (checkbox)
<div class="sc_contact_form_item sc_contact_form_checkbox">
<input type="checkbox" name="sendcopy" value="yes" id="contact_form_checkbox" checked> Stuur mij een kopie van het bericht
</div>
What I did until now:
1) add a new mail account: no-reply#domain.be
2) added $headers with 'Reply-To:', 'From:' and 'X-Mailer:'
It's working now and showing the correct addresses in the mail.
But if the check is made to send a copy the mail still ends up in spam folder. How can I solve this?

Mail Fucntion Syntax:
mail(to,subject,message,headers,parameters);
How add BCC
Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).
Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file.
Example
$to = "name#mydomain.com";
$subject .= "".$emailSubject."";
$headers .= "Bcc: ".$emailList."\r\n";
$headers .= "From: no-reply#thepartyfinder.co.uk\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';
if (mail($to, $subject, $message, $headers)) {
$sent = "Your email was sent!";
} else {
$sent = ("Error sending email.");
}
Why Is My Email Going to The Spam Folder?
Don’t use your email address for the “from” email address.
If you send a email that is “from” your email (let’s say test#activecampaign.com) and you send it to a contact with the email (test#activecampaign.com) it will most certainly be marked as spam as the “from” and the “to” is the same.
Pay attention to the spam filter testing before sending.
Don’t send a single graphic/image
Don’t use a free email address as your “from” email
Test different subjects & email contents
Pay attention to your links in your email
Don’t include links that use link shortening services
Take the time to code your HTML correctly
Remove inactive contacts
Send using a consistent “from” email address
Never include Javascript, form code, or video within your email
Avoid copying anything directly from Microsoft Word, Excel,
Powerpoint, etc..

Related

Add e-mail and sender - PHP form

I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
Hello.
I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
<?php
session_start();
//Ajax Questions Form
if(isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$arrival = $_POST['arrival'];
$departure = $_POST['departure'];
/// $adults = $_POST['adults'];
// $children = $_POST['children'];
// $room = $_POST['room'];
$requests = $_POST['requests'];
$to = 'contact#test.camp'; //Replace with recipient email address
$subject = 'Hotel Booking'; //Subject line for emails
$message = 'From: '.$name."\r\n".'Email: '.$email."\r\n".'Arrival: '.$arrival."\r\n".'People: '.$departure; //."\r\n".'Adults: '.$adults."\r\n".'Children: '.$children."\r\n".'Room: '.$room."\r\n".'Requests: '.$requests;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
}
//Contact Php Form
if(isset($_POST['contact_email'])){
$contact_name = $_POST['contact_name'];
$email = $_POST['contact_email'];
$contact_message = $_POST['message'];
$to = 'marek#gmail.com'; //Replace with recipient email address
$subject = 'Contact Form'; //Subject line for emails
$message = 'From: '.$contact_name."\r\n".'Email: '.$email."\r\n".'Message: '.$contact_message;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // This line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
}
}
?>
The php mail function does not have much functionality try using something like PHPMailer which allows you to send more complex emails
For add CC or BCC or ReplyTo add header to your email structure :
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'To: andreas<mail1#gmail.com>, thomas<mail2#gmail.com>';
$headers[] = 'From: from <from#gmail.com>\r\nReply-to: <ReplyTo#gmail.com>';
$headers[] = 'Cc: Cc#gmail.com';
$headers[] = 'Bcc: Bcc#gmail.com';
mail($to, $subject, $message, implode("\r\n", $headers));

php mail() not sending Cc and Bcc

I have a php file which sends an email after a contact form has been submitted.
The email delivers correctly to the recipient, but it does never deliver to the Cc and Bcc. In the received email, anyways, the Cc is there(of course Bcc doesnt show up but if Cc is there Bcc should be too), but it just does not deliver to them.
Here is the code
<?php
$from = 'xx#xxx.com';
$to = $_POST['mail'];
$name = $_POST['nombre'];
$lastname = $_POST['apellido'];
$msg = "this is the content of the mail";
$subject = 'this is the subj';
$headers = "From:".$from."\r\n";
$headers .= "Cc:".$from.", xxx#xxx.com,xxy#xxy.com\r\n";
$headers .= "BCC:xyx#xyx.com\r\n";
if ($name=="" || $lastname=="" || $_POST['mensaje']=="" || $to==""){
echo '0';
}else{
if(mail($to, $subject, $msg, $headers)){
echo '1';
}else{
echo '-1';
}
}
?>

Php mail. Can't get email on one address

I have a problem with my mail in php. I code form to send email. I receive email on gmail but I have other mail address and I can't get email on it.
I checked in spam and there is no email also.
Below is my code.
<?php
$emailErr = "";
$endMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Proszę uzupełnić pole e-mail";
}
else if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$namesurname = $_REQUEST['name_surname'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$message = $subject . ": " . $message . " " . $number . " " . $namesurname . " " . $email;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail("szafor#szafor.pl", "Zamówienie pomiaru",
$message, "From: formularz#szafortest.pl \r\n"."Content-Type: text/plain; charset=UTF-8\r\n");
$endMessage = "Dziękuję za przesłanie wiadomości.";
}
}
?>
One important thing to consider with sending mail is that you should at least have the return path of the message be an email address that is actually hosted on the server that you are sending from.
You can set the From and the Reply-To address as any address, but the return-path should be set to a valid email address hosted on your server. Let's say that you want the "reply" button to send back to "this_email#wherever.com" but the server you are using hosts email for "mydomain.com". Create an email account on your server, "info#mydomain.com" for example.
$recipient = "sendto#email.com";
$subject = "Test email";
$message = "This is the message.";
$headers .= "From: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Reply-To: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Return-Path: Your Name Here <info#mydomain.com>\n\r";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .="X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
mail($recipient, $subject, $message, $headers);
I have found that the more valid header information that I provide, the more likely the email will be delivered. Right now these headers always work for me, and I have a scheduling program that is sending email to a hundred different email addresses every day. See if that works better for you.

send an email using data the user has given in a form?

I have a contact form for my website and am hoping to modify it so that a confirmation email is sent to the user when they click submit. Can anybody advise me on the best way to do this?
My php is pretty simple:
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" &&
$msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$EmailFrom = "someone#somewhere.co.uk";
$EmailTo = "someone#somewhere.co.uk";
$Subject = "Online contact form";
$full_name = Trim(stripslashes($_POST['full_name']));
$Phone_Num = Trim(stripslashes($_POST['Phone_Num']));
$email_addr = Trim(stripslashes($_POST['email_addr']));
}
// prepare email body text
$Body = "";
$Body .= "full_name: ";
$Body .= $full_name;
$Body .= "\n";
$Body .= "Phone_Num: ";
$Body .= $Phone_Num;
$Body .= "\n";
$Body .= "email_addr: ";
$Body .= $email_addr;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
?>
First we get the mailto function to work with localhost and email client:
Check this link on stackoverflow:
URL link: send email to owner by collecting information provided by user through form?
Then I recommend using Swiftmailer. http://swiftmailer.org/docs/sending.html They got the best manual.
As others have already suggested the "if" statement is useless since it is achieving nothing. I think your idea was to verify if the fields that you have in your contact form are filled or not. If the fields are unavailable you should throw some error which you are not doing.
Also, if you are to send a confirmation mail to the user who clicks the submit button then the $EmailTo variable should take the email from $msg_email or $msg2_email which according to the code is not done here.
Check this simple snippet this might help you:
if ($_POST['submit']) {
if ($_POST['name1] == '')
echo "Please provide the first name";
if ($_POST['name2] == '')
echo "Please provide the last name";
if ($_POST['email1] == '')
echo "Please provide the email";
if ($_POST['email2] == '')
echo "Please provide the alternate email";
if ($_POST['message] == '')
echo "Please provide the Message";
//Send email to your inquiry mail with above details
// Confirmation mail
$message =<<<EOM
Dear $_POST['name1'],
Thank you for your inquiry.
Our executives will get back to you ASAP.
Thanks,
Sales
EOM;
$to = $_POST['email1'];
$subject = "Acknowledgement of Inquiry";
$headers = "Content-Type: text-plain";
$headers .= "From: sales#yourcompany.co.uk";
mail($to, $subject, $message, $headers);
}

Sending HTML email, via PHP form

I am trying to send my website visitors and email with some directions and tips before they show up to my studio via PHP form mailer. ( i am simplifying some of the form fields )
However the HTML formatting is not working correctly.... did i not declare the mime type of charset correctly?
<?php
if (isset($_POST['submit'])) {
//if (empty ($_POST['name']) || empty($_POST['email']))
//{
//echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>";
//}
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
//A bunch of other fields are here
//Additional Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$yoursite = "My Site";
//$youremail = $email;
$subject = "Website Form";
$message = "
$name would like you to contact them about your service.
Contact PH: $phone
Email: $email
Legal Guardian: $legal
//more stuff here
";
$subject2 = "Directions and Information";
$message2 = "<html><head></head><body>
$message2 .= "<h1>Directions</h1>
<p>text</p>
<p><a href='http://example.com/schedules'>Click here</a>
<h2>How Do I find your Photo Studio?</h2>
<h2>What do I have to bring with me?</h2>
</p>";
$message2 .= "</body></html>";
$email3 = "me#mysite.com";
$email4 = "mysite#gmail.com";
//This email sends their details to me from the visitor
mail($email3, $subject, $message, "From: $email");
//This email sends directions to the visitor from me
mail($email, $subject2, $message2, "From: $email4");
echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}
?>
There is a lot of random junk that needs to be done to an email to send correctly. I generally tend to outsource all that responsibility to a pre-packaged class that exists in the wild, something like http://swiftmailer.org/
Maybe someone else would have a better class to offer.
I swear by the PEAR Mail_Mime package. It's simple and powerful.
PEAR: Mail_Mime
//Basic mail headers
$headers['To'] = "test#domain.com";
$headers['From'] = "sender#domain.com";
$headers['Subject'] = "Test";
//Set up the mail module
$mime = new Mail_mime("\r\n");
$mime->setTXTBody("This is a test");
$mime->setHTMLBody("<p>This is a test.</p>");
$body = $mime->get();
$headers = $mime->headers($headers);
//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($headers['To'], $headers, $body);

Categories