PHP Form - PHP Mailer cannot send to #Gmail.com - php

We have a PHP form on our website that has worked for years. When users fill the form out in sends us the details, and sends them an email and then re-directs the user to a "thank-you" page. However, if a user puts their email address as #gmail.com the form fails to send, it doesnt show the thank-you page.
This is really bizarre. We are using PHPMailer and validate if the form has been sent using:
if($mail->Send()) {
$mailsent = true;
If a user enters #hotmail.com or at #yahoo.com everything is fine. But enter #gmail.com and $mailsent is always false.
In this situation where does the problem lye? It seems to me that our web hosts SMTP connection to Gmail is failing. Would this seem right?
Here is the code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$name = '';
$email = '';
$mailsent = false;
$referer = '';
if(isset($_POST['name'])){
include('phpmailer/class.phpmailer.php');
$name = $_POST['name'];
$email = $_POST['email'];
$subject = 'Quote request from ' . $name;
$body = 'A quote request has been received from a user with following details:' . "\n\n" .
"Name: $name \n" .
"Email: $email \n" .
"----------------------------------------------------\n\n" .
"Place: UK".
$body .= "\n----------------------------------------------------\n\n";
$body .= "Refering: ".$referer." \n";
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->From = $email;
$mail->FromName = $name;
$mail->Body = $body;
$mail->AddAddress("dean#example.com", "Example");
if($mail->Send()) {
$mailsent = true;
} else {
$error[] = 'There was some error. Please try later.';
}
}
?>

Related

How to add smtp host to send mails with this phpmail script

I want to send mails using this PHP MAILING script but through SMTP and not through sendmail because my host rejects all emails as spam and I don't check my spam folder as its heavy loaded with spam emails daily.
I just want to know where to put the code which will use my SMTP logins to send mails using this php script instead of using sendmail/
I tried adding some things like
$host = "smtp.gmail.com";
$port = "587";
$username = "testtest#gmail.com";
$password = "testtest";
But these didn't worked, actually it broke the whole code itself,
Edit: I also tried adding this, reading an answer from another answered question here, and it also broke the code itself but didn't work.
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'johndoe#gmail.com',
'password' => 'passwordxxx'
Original Code
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG .= "E-mail is required ";
} else {
$email = $_POST["email"];
}
// MESSAGE
if (empty($_POST["message"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
// change this to fit !
$EmailTo = "demo#domain.com";
$Subject = "New Message from MENTOR";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Somethin went wrong...";
} else {
echo "An error has ocurred!";
//echo $errorMSG; Enable if you want full details of php error
}
}
?>

(Noob) - Simple PHP E-mail form code?

I bought a bootstrap theme just to make a simple site. It has a contact form on it and the theme creator said to make sure your hosting let's you use php. I asked my host and they said:
"You need to add email authentication to the form.
Create an email address in cpanel for use with the form, then use that
user name and password to authenticate the email sending from the
form.
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm"
I created the e-mail in cpanel, but unfortunately, I am not knowledgable enough for the next step. I was hoping someone could help me with the code to get this form working.
This is the email.php code in it's entirety:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Message";
$body = "From $name, $email, \n\n$message";
$to = "myemail#gmail.com";
mail($to, $subject, $body);
?>
Is there something simple I can add to this to authenticate and make the form work?
Updated answer:
You could edit your email.php / html code to the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email example</title>
</head>
<body>
<form method="post">
<input type="text" name="sender_email" placeholder="Senders email address">
<input type="text" name="receiver_email" placeholder="Receivers email address">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit">Send email</button>
</form>
</body>
</html>
<?php
require_once('send_email_class.php');
if(!empty($_POST)){
$sender_email = $_POST['sender_email'];
$receiver_email = $_POST['receiver_email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_email = new send_email_class($sender_email, $receiver_email, $subject, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
and upload the following code as a file called: send_email_class.php to your webserver.
<?php
class send_email_class {
// Class constructor storing variables
function __construct($sender, $receiver, $subject, $message){
$this->receiver = $receiver;
$this->subject = $subject;
$this->message = $message;
$this->header = 'From: ' . $sender . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->headers)){
return 'Mail sent.';
}else{
return 'Unable to send mail.';
}
}
}
?>
Ofcourse you don't have to enter all those informations from the form, you could store some informations like the $sender_email variable fixed in your PHP code.
But thats up to you. Hope this helped.
Edit:
Please replace the content of the email.php with the following:
<?php
class email{
// Class constructor storing variables
function __construct($sender_email, $sender_name, $message){
$this->sender = $sender_email;
$this->sender_name = $sender_name;
$this->message = $message;
$this->receiver = 'changeme#email.com';
$this->subject = 'Email from: ' . $sender_name;
$this->header = 'From: ' . $sender_email . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->header)){
return 'Thank you ' . $this->sender_name . ' for sending the email containing the following message: ' . $this->message;
}else{
return 'Sorry ' . $this->sender_name . ' a error occured, please try again.';
}
}
}
if(!empty($_POST)){
$sender_email = $_POST['email'];
$sender_name = $_POST['name'];
$message = $_POST['message'];
$send_email = new email($sender_email, $sender_name, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
And edit the following code line: $this->receiver = 'changeme#email.com';
Final answer:
After receiving the files, the fix was easy, the ajax been dealing with the handling itself.
mail.php content:
<?php
$receiver = 'xxxxx#gmail.com';
$subject = 'Email from: ' . $_POST['name'];
$message = $_POST['message'];
$header = 'From: ' . $_POST['email'];
mail($receiver, $subject, $message, $header);
?>

Contact form not sending message to Gmail

Please I need help with this. My contact form is not sending message to my gmail. I have checked my spam also and nothing came. Not sure if it's something to do with my code. Thanks in advance. I have realized that the message is going into my godaddy cpanel. The subject is
mail failure - malformed recipient address
The message body says
A message that you sent contained a recipient address that was incorrectly constructed:
from: missing or malformed local part (expect word or "<")
The message has not been delivered to any recipient.
Please also ignore the $ip field that part of my code wasn't added.
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname#gmail.com";
$email = "From: $email";
$comment = "Message: $comment";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";
$headers = "From: $email,";
$send_contact = mail($to,$email,$comment,$message,$headers);
header("Location: index.php");
}
?>
This should work.
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname#gmail.com";
$title = "Message from my website";
$message = "Comment: {$comment}" . "\r\n";
$message .= "Sent from the website with IP Address: {$ip}" . "\r\n";
$headers = "From: " . strip_tags($email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
mail($to,$title,$message,$headers);
header("Location: index.php");
}
?>
I Advice you learn how to use the mail function here http://php.net/manual/en/function.mail.php
Heyy Good Morning,
If you are working in offline mode maybe you need phpmailer.
First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip
Then paste in your folder. If my coding doesn't clear you, you can check from
https://github.com/PHPMailer/PHPMailer
<?php
require 'PHPMailerAutoload.php'; // Your Path
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // Your mail
$mail->Password = 'secret'; // Your mail password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Check Condition
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Second way.
If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste.
Doesnt required phpmailer.
<?php
$error = [];
$receipientName="Fido";
$receipientEmail ="receipientmail.gmail.com";
$ccEmail ="";
//declares variable
if(isset($_POST['name'])) $name = $_POST['name'];
else $name = "";
if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";
function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}
if(isset($Submit) && $Submit=="Go") {
$emailContent ='';
$sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
if($sent) {
echo $emailContent;
header('Location: contact.php');
}else{
echo "Failed";
exit;
}
}
?>

PHP Pear email message format wrong

I know the title is weird I cant for the life of me phrase it well lol.
I have done searches with multiple ways of phrasing the question and nothing shows up for this.
I have the email scripting working on the website im building and its fantastic! but when i edited the mail code to add extra message lines its made the sequence go wrong.
here is the code im using for the email message area:
<?php
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$message = stripslashes($message);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "email#email.com";
$message = "Name: ".$name."\n".$message;
$message = "From: ".$from."\n".$message;
$message = "Comments: ".$message."\n".$message;
$message = "Allow feedback to be Published: ".$acp."\n".$message;
$message = "[ OPTIONAL DATA ]"."\n".$message;
$message = "Company Name: ".$compname."\n".$message;
$message = "Phone Number: ".$ph."\n".$message;
$message = "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
In my mind this should spit out the message body as this:
Name: name here
From: Email address
Comments: Message here
Allow feedback to be published: response
[ OPTIONAL DATA ]
Company Name: Company
Phone Number: Phone
Allow extra Marketing:
This should be how its seen in the email right?
What im actually getting is this:
Allow feedback to be Published: response
[ OPTIONAL DATA ]
Company Name: company
Phone Number: phone
Allow extra Marketing? Response
From: Email address
Name: name here
Comments: Message here
Is this normal? or have i inadvertently snuffed it somewhere along the lines and its messing with my head as payment?
Thanks for any help on this.
EDIT: Updated code.
<?php
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace your#email.com with your own email address lower in this code.
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$comment = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$comment = stripslashes($comment);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "jim#digital2go.com.au";
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$comment."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
Make your message "continue" in the order you wish by doing this:
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$message."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;

PHP reply-to error - need person's email, not admin

I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);

Categories