PHP contact form, not receiving email [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So, here we go with silly question number too many to count!
I've made a very simple PHP contact form using tutorials from the internet (I still need to add security measures to it but I wanted to get it working first) When I click on the send button on my website I do get the message sent script however no email arrives in my in box.
Any ideas what I'm doing wrong? The website is currently hosted locally via XAMPP.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$from = 'From: me#mywebsite.co.uk';
$to = 'me#mywebsite.co.uk';
$subject = 'Enquiry';
$body = "From: $name\n Company: $company\n Email: $email\n Telephone: $tel\n Message: $message\n";
if ($_POST['send']) {
if(mail($to, $subject, $body, $from)) {
echo '<p> Your message has been sent!</p>';
} else {
echo '<p>Message could not be sent.<br>Please check that you have completed the name, email and message fields and try again</p>';
}
}

Alright:
Step 1: check your error logs for any problems with the mail not being sent. Normally when installing an Apache inside windows the most people skip to set the from server and credentials.
I used WAMP alot and that one works normally only with an external account.
Step 2: If anything fails.
Download a mailer library and use Gmail to send the emails. Here is an tutorial on that : http://phpmailer.worxware.com/?pg=examplebgmail
Works great. Sure there is a lot of files in phpmailer but it works and easy to upgrade when new software versions are released.

As when I've previously set up contact forms I've been doing so using Code Igniter I didn't realise that I couldn't use mail() without installing a mail server.
Thanks to Parris Varney and RiggsFolly for pointing this out and thanks again to Riggs for letting me know that Code Igniter uses the PHPMailer library.
By using PHPmailer I was able to correct the code and get the form working perfectly in very short order.
For anyone interested the new code used with the latest version of PHPmailer is:
$name = $_REQUEST['name'];
$co = $_REQUEST['company'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];
require("PHPMailerAutoLoad.php");
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "mail.mydomain.co.uk";
$mail->SMTPAuth = true;
$mail->Username = "me#mydomain.co.uk";
$mail->Password = "password";
$mail->SMTPAutoTLS = false;
$mail->From = $email;
$mail->addAddress("me#mydomain.co.uk", "Me");
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Enquiry";
$mail->Body = "From: $name<br>Company: $co<br>Email: $email<br>Telephone: $tel<br>Message: $message";
$mail->AltBody = "From: $name Company: $co Email: $email Telephone: $tel Message: $message";
if(!$mail->Send())
{
echo "Message could not be sent";
}
echo "Message has been sent";
?>

Related

mail.php script not working for subscribe and contact page [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
My web developer made this script to send notifications to people subscribing to my mailing list and the people who contact me, through the form on my website
The problem is, the recipients are not able to receive the emails, and I am not able to receive the notification on my mail id about a new subscriber or a contact
Sender id - notifications#llaveshagarwal.com
Moreover, the same script works on his server (which we used for testing) but doesn't work on my server
Is there a problem with my SMTP server settings ?
Also, I am using Google Apps for Gmail
Here is his script
What should I do ?
<?php
if($_POST['type'] == "subscribe") {
/*$to = "notifications#llaveshagarwal.com";
$subject = "New Email Subscriber";
$txt = "Name: ".$_POST['name']."\r\nFrom: ".$_POST['from'];
$headers = "From: ".$_POST['from'];
mail($to,$subject,$txt,$headers);
$to = $_POST['from'];
$subject = "Thank you for subscribing with us.";
$txt = "Hey ".$_POST['name']." ,\r\nGreetings from LLavesh Agarwal Textile Agency\r\nThank you for subscribing with us for Exclusive and Latest Catalogs and product launches.\r\nWe will update you soon.\r\n\r\nRegards,\r\nTeam LLavesh Agarwal\r\n\r\n*This is an automated Email. Please do not reply to this Email id. If you wish to talk to us, kindly Email us at hello#llaveshagarwal.com";
$headers = "From: notifications#llaveshagarwal.com";
mail($to,$subject,$txt,$headers);*/
$link = mysql_connect('localhost', 'llavesha_admin', 'Admin#1234');
$db= mysql_select_db('llavesha_contact_system', $link);
$insert='insert into subscribe (name,email) values ("'.$_POST["name"].'","'.$_POST["from"].'")';
if(mysql_query($insert)) {
echo "success";
} else {
echo "fail";
}
}
elseif($_POST['type'] == "contact") {
$to = "notifications#llaveshagarwal.com";
$subject = "New Contact";
$txt = "Name: ".$_POST['name']."\r\nContact: ".$_POST['mobile']."\r\nCategory: ".$_POST['bussiness_type']."\r\nDescription: ".$_POST['project_detail'];
$headers = "From: ".$_POST['from'];
mail($to,$subject,$txt,$headers);
$to = $_POST['from'];
$subject = "Thank you for contacting us.";
$txt = "Hey ".$_POST['name']." ,\r\nGreetings from LLavesh Agarwal Textile Agency\r\nThank you for contacting us.\r\nWe will update you soon !\r\n\r\nRegards,\r\nTeam LLavesh Agarwal\r\n\r\n*This is an automated Email. Please do not reply to this Email id. If you wish to talk to us, kindly Email us at hello#llaveshagarwal.com";
$headers = "From: notifications#llaveshagarwal.com";
mail($to,$subject,$txt,$headers);
echo "success";
}
?>
Are you working on windows (wamp)? Have you enabling php sendmail on php.ini? read: http://us3.php.net/manual/en/function.mail.php
or you can read: php mail() function on localhost
If you want to make it simpler, use phpmailer instead: https://github.com/PHPMailer/PHPMailer
How to use it by #pooria: How to configure PHP to send e-mail?
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender#gmail.com';
$mail->Password = '123!##';
$mail->SetFrom('me.sender#gmail.com', $name);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1#gmail.com', 'title1');
$mail->AddAddress('abc2#gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();

PHP - Send copy of form input to user [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to send a copy of a submitted form to the user of the client-side. I tried implementing some code I already found around here but can not get it to work successfully. Would you guys mind taking a look? ...All input appreciated, as always!
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_From>");
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
First you need to declare the variables, and then send out the email:
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($email_to, $Subject, $Body, "From: <$email_from>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_from>");
Try to be consistent in variable naming conventions (not mixing CamelCase and underscores). That will make it easier for you to stay on top of your code.
Try to send mail using PHPMailer. Try the below standard php code but first download phpmailer.php and save it.
// include the PHPMailer
require_once('PHPMailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
$body = "hello";
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "name#yourdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

phpmailer doesn't send important information (email address entry)

Someone built this site for me and I have no idea about php and which part I should fix :S
Basically I have this single text entry form on my site which users can enter in their email address to that form and once they send submit I'll get the email address they input in.
The dev used phpmailer and at the moment when someone submitted their email address, I don't get any of their email address. The only entry/ email content I get is from "me" and a content of "Thank you. We'll update you as soon as possible."
This is the sendmail.php on the root folder:
<?php
$from = "ask#michaelfrieda2014.com";
if (isset($_REQUEST['email']) && $_REQUEST['email'] != '')
{
$email = $_REQUEST['email'];
sendMailSingup($from, $email);
sendMailSingup($from, "ask#michaelfrieda2014.com");
}
function sendMailSingup($from, $to)
{
include_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$body = "Thank you. We'll update you a soon as possible.";
$mail->From = "$from";
$mail->FromName = "";
$mail->Subject = "Thank you!";
$mail->AltBody = "Thank you!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("$to", "");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
#echo "Message sent!";
echo 1;
}
}
How do I make it so that I get the important information? (Their email address sent to my email). Am I missing something?
Thanks!
Below this line...
$body = "Thank you. We'll update you a soon as possible.";
add:
$body .= "<br><br>Sent from: $to";
(note the .= -- this will append to the $body variable)
If you want to fix the reply-to address, add this line:
$mail->AddReplyTo($to, $to);
before:
$mail->From = "$from";

Sending email via PHP error

im a php beginner, and I am building a website, and website is supposed to let people send me email. the thing is i never knew anything about sending emails through php. I looked it up online and tried using the codes i found. The thing is my program says it sent the email but i never happen to get the email. I thought maybe it is because I am using apache server to test my php, and maybe it is gona work when I upload it to a real server??(yes this was a question)
Just in case, this is my code, and it is the all php code in my website, also form works fine, there is nothing wrong with it.
<?php
if (isset($_POST['name'])) {
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "From: " . $_POST['name'] . ", " . $_POST['email'] . "\n Message: " . $_POST['message'];
try {
mail("serdarufuk95#gmail.com", $subject, $message, " ");
unset($_POST['name']);
header("Location: success.php");
}
catch (PDOException $e)
{
include 'index.html';
exit();
}
exit();
}
include 'contact2.php';
?>
is there a problem with the code? or do i have to call something from a library or anything or am i missing a code! HELP ME MAKE THIS WORK! when i execute it, it takes me to success.php, so i assumed nothing is wrong with my code, but you guys know better!
It might not work after uploading to a real server if the server's mail configuration has not been set yet. So I suggest you to use a better and simpler version in order to send mails through PHP: PHPMailer
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->AddAddress("mail#domain.com","Display name");
$mail->Subject= "Mail subject";
$mail->Body= "Mail content";
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "formmail#domain.com";
$mail->Password = "123456";
$mail->IsHTML(true); //true if you want to send html content. false for plain text message
$mail->From = $_POST['Email'];
$mail->FromName = $_POST['Name'];
$mail->Send();
You may download the class clicking here

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

Categories