Sending HTML email, via PHP form - php

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

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. 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.

php pear mail cant send the html formatted mail

Hi here my mail is working fine, but i dont know how to send a html formatted mail, because i am using pear mail!here i am using the code to send mail using php , first calling the mail.php and mime.php. $to mention which mail id you want to send a mail, to send a mail you need specify email id and password of email which is the sender mail.
<?php
if(isset($_POST['submit']))
{
require_once "Mail.php";//calling mail function
require_once 'Mail/mimePart.php';
$from = "you#localhost.com";
$to = "you#gmail.com";//your mail id
$CompanyName = $_POST['CompanyName'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$paddress = $_POST['paddress'];
$subject = 'MOSONS Group';
$message = 'PersonName: ' .$CompanyName. "\n";
$message .= 'Mobile: ' .$mobile. "\n";
$message .= 'Email: ' .$email. "\n";
$message .= 'Message: ' .$paddress. "\n";
$host="ssl://smtp.gmail.com";
$port="465";
$username="you#gmail.com";// your email id
$password="yourpassword"; //password
$mime= "MIME-Version: 1.0\r\n";
$type= "Content-type: text/html\r\n";
$headers = array ('From' => $from,'To' => $to ,'Subject'=>
$subject,'Mime'=> $mime,'Contenttype'=> $type);
$smtp =# Mail::factory('smtp',array ('host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail = #$smtp-> send($to,$headers,$message);
if(#PEAR::isError($mail)){
echo ("<p>" .$mail->getmessage(). "</p>");
}
else
{
echo '<script type="text/javascript">alert("Thank You for Contacting us, our team will get in touch with you soon.!"); </script>';
}
}
?>
You are setting your headers incorrectly. e.g you're doing stuff like
'Contenttype'=> 'Content-type: text/html';
It should be just
'Content-Type' => 'text/html'
PEAR::Mail is a horribly crappy package. You should switch to something better, like PHPMailer or Swiftmailer.

PHP get form not receiving email

I'm using a simple web form and some PHP script to send the completed form to my email. When I hit "Submit" I get the PHP text "Thanks, we'll be in touch shortly" but I'm not getting the email with submitted form info. Not sure what's the problem.
from the HTML doc:
<div class='' "letstalk_form">
<form method="get" action="contact.php" name="contact" id="contact">
<p>
<label for="txtfname">Name:</label><input type="text" tabindex="1" name="txtfname" id="txtfname" class="clsTextBox">
</p>
<p>
<label for="txtcompany">Company:</label><input type="text" tabindex="2" name="txtcompany" id="txtcompany" class="clsTextBox">
</p>
<p>
<label for="txtemail">E-mail:</label><input type="text" tabindex="3" name="txtemail" id="txtemail" class="clsTextBox">
</p>
<p>
Message:<textarea name="comments" id="comments" rows="2" cols="20"></textarea>
</p>
<p class="submit">
<input type="submit" value="Submit">
</p>
</form>
</div>
and the PHP
$msg = "Sender Name:\t$txtfname\n";
$msg .= "Sender Company:\t$txtcompany\n";
$msg .= "Sender E-mail:\t$txtemail\n";
$msg .= "Comments:\t$comments\n\n";
$recipient = "receiversemail#somewhere.com";
$subject = "from PI Website";
$mailheaders = "From: Lets Do Business <> \n";
$mailheaders .= "Reply-To: $txtemail\n\n";
mail($recipient, $subject, $msg, $mailheaders);
echo "<HTML><HEAD><TITLE>Form Sent!</TITLE></HEAD><BODY>";
echo "<H3 align=center>Thank You, $txtfname</H3>";
echo "<P align=center>Your submission was sent successfully.<br />
I will be contacting you soon.</P>";
echo "</BODY></HTML>";
?>
</div>
I've used this is the past and worked great. Is there something I overlooked? Thanks for you help!
EDIT: I ended up using the link Turgut Dursun provided (html-form-guide.com/contact-form/php-email-contact-form.html ) to set up the form. Works great. Thanks.
Are you assigning the $_GET[] values from the form to variables prior to this portion of your code?
$msg = "Sender Name:\t$txtfname\n";
$msg .= "Sender Company:\t$txtcompany\n";
$msg .= "Sender E-mail:\t$txtemail\n";
$msg .= "Comments:\t$comments\n\n";
$recipient = "abosiger#embarqmail.com";
$subject = "from PI Website";
$mailheaders = "From: Lets Do Business <> \n";
$mailheaders .= "Reply-To: $txtemail\n\n";
mail($recipient, $subject, $msg, $mailheaders);
If not, you need to either initialize your $_GET[] values to individual variables:
$txtfname = $_GET['textfname'];
for each variable sent through get. Or you can access them directly from the $_GET array through the following syntax:
$msg = "Sender Name:\t" . $_GET['txtfname'] . "\n";
Let me know if this helps.
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
change it like this.
The problem probably comes from a bad configuration of the mail function. You have to configure it properly.
Also, you should provide an email in the "From" header.
if message sent and no mails is receieved, this authenticating issues from your domain
... your script might works fine
The two main ways to do this is either using PHPMail() or SMTP. PHPMail() is open and used by trojans and viruses. Most clients have infected files which just send mails using PHPMail(), because it doesn't authenticate. It doesn't check if the email being sent is actually coming from the domain and this puts a lot of strain on the mail server, making legitimate mails wait in queue for a long time.
If you already have a script using PHPMail() function it is very simple to change it to SMTP, you just need to change like 5 lines of your code and add an email address and password (already created in cpanel) into the code, and it will send emails just fine.
if ($_POST['submit']){
$from = "slick#meetslick.com"; //enter your email address
$to = "slickbozz#gmail.com"; //enter the email address of the contact your sending to
$subject = "Contact Form"; // subject of your email
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = ''; // text versions of email.
$html = "<html><body>Name: $name <br> Email: $email <br>Message: $message <br></body></html>"; // html versions of email.
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "localhost"; // all scripts must use localhost
$username = "support#meetslick.com"; // your email address (same as webmail username)
$password = " here "; // your password (same as webmail password)
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
// header("Location: http://www.meetslick.com/");
}
}
I hope this will be helpful to someone one day.

Warning: mail() [function.mail]: SMTP server response: 550 Invalid recipient:

I'm creating a 'forgot password' page where the user enters their email address and the script finds the password associated to that email, and sends it to the stored email address.
I believe the problem has to do with my SMTP Mailserver. I am using WAMP which doesn't have one so I downloaded one that was free.
This is the php script I'm using:
$id = checkEmail($email);
$record = readMemberRecord($id);
$password = #mysql_result($record,0,'cred');
if($password){
$email_subject = "Email Request";
$email_body = "Your password is ".$password.".";
$header = "NinjaMan";
mail($email, $email_subject, $email_body,$header);
$msg = "Your password has been sent to ".$email.".";
}else{
$msg = "Sorry, the email ".$email." wasn't found.";
}
The $msg outputs properly so I know the code is passing the mail function.
Try sending in a proper "From" in $header.
$emailFrom = "admin#yourdomain.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Email Request";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Your password is ".$password.".";
mail($email, $subject, $message, $headers);
See details of the mail() function.
If this doesn't work, try using PHPMailer. You configure it in code, no need to edit php.ini.
I've used it in some projects (v 2.0.4, I see the latest is 5.1) and had no problems.
Try using Google's server to send mails, you can see how to do that here
Try using this
//Email information
$to = "garggarima#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "support#sltechsoft.com";
$headers = "From:" . $from;
$mail=mail($to,$subject,$message,$headers);
if($mail) {
echo "Thanks for mail";
} else {
echo "Mail not Sent";
}
//Email response
echo "Thank you for contacting us!";

Categories