Mail/mime email not sent when displaying form with jquery - php

I have a relatively basic email form put together which I intend to email to a recipient using mail mime. This approach has worked for me in the past when the form was displayed on its own page i.e. a contact page.
Here I am using jquery to display a hidden div which contains my form from the home page. The problem is that although my validations pass the email is not being sent. I am at a loss for why and hope someone here can help me out!
The relevant php is as follows:
$path = get_include_path() . PATH_SEPARATOR . '/home/host/php';
set_include_path($path);
include('Mail.php');
include('Mail/mime.php');
$recipient_email = 'somebody#somewhere.com';
//validations are here
//send the email
$name = $_POST['name'];
$visitor_email = $_POST['from'];
$subject = $_POST['subject'];
$user_message = $_POST['message'];
$to = $recipient_email;
$from = $visitor_email;
$text = "From: $name \n Subject: $subject \n Message $user_message";
$message = new Mail_mime();
$message->setTXTBody($text);
$body = $message->get();
$extraheaders = array("From"=>$name, "To"=>$recipient_email, "Subject"=>$subject, "Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
echo "Success";
The jquery to display the form looks like this:
jQuery.fn.fadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle'}, speed, easing, callback);
};
$(document).ready(function() {
$('#tellfriend').hide();
$('li a.email, #tellfriend a.close').click(function() {
$("#tellfriend").fadeToggle('slow');
});
});
One of my thoughts (and I'll experiment with it) is that because when the link is clicked to display form, the link is appended to the URL and so it may affect my form action? Not sure.. Any help is appreciated!

I was able to solve my problem by following a previous question I eventually found on Stack Overflow HERE
It turns out that I had to specify an outgoing mail server in order to send the form out. Still, it is odd that I have had it working in the past where I didn't need to follow such steps.

Related

Generate Email PHP

I've spent some time trying to figure out a way to allow users to contact me via my landing page. Being fairly new to programming and having used snippets of code from multiple sources I am at my wits end and would love some help. My troubleshooting has centered around:
I. Did I setup my local server correctly?
And if so, am I missing a package on WAMP and/or mail server? I referenced php mail setup in xampp this post.
II. Is my HTML code faulty?
<div class="container-contact100-form-btn">
<button type="submit" value="Send" class="contact100-form-btn">
Send Message
</button>
</div>
III. Am I needing to edit/adjust my JS file to accommodate the additional functionality?
function showValidate(input) {
var thisAlert = $(input).parent();
$(thisAlert).addClass('alert-validate');
$(thisAlert).append('<span class="btn-hide-validate"></span>')
$('.btn-hide-validate').each(function(){
$(this).on('click',function(){
hideValidate(this);
});
});
}
function hideValidate(input) {
var thisAlert = $(input).parent();
$(thisAlert).removeClass('alert-validate');
$(thisAlert).find('.btn-hide-validate').remove();
}
IV. Is something missing from the PHP code?
<?php
if( isset($_POST['Send Message']))
$name = $_POST['name'];
$email = $_POST['email'];
$bar = $_POST['bar'];
$message = $_POST['message'];
$email_from = $email;
$email_subject = "New Contact Us Email";
$email_body = "You have received a new message from the user $name.\n".
"Here's the message: \n $message".
$to = 'I had my email here';
mail($to, $email_subject, $email_body)
?>
I really would appreciate any help and/or links that might lead me down the right path. Thank you!

How to send same php filled form to client also

How to send same PHP filled form to client also?
Like the below code sent the file to website owner in HTML/PHP forms
$to = "owner#gmail.com";
Now how to add client also here like
$to = "owner#gmail.com,$email";
Or what?
Use this
$field_email = "sender#mail.com"
$to = "owner#site.com, $feild_email ";
$subject = "Subject"
$message = "MESSAGE";
mail($to,$subject,$message);

Email header not working PHP

I'm using BlueHost and I can't seem to get my email form to work. This is the PHP:
$to = "test#email.com";
$subject = "test";
$message = "test message";
$from = $_POST['cf_email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo $headers;
This doesn't seem to send the email. However, if I add ANY string in front of the email, then it works. e.g.:
$from = "zz".$_POST['cf_email'];
Can anyone tell me what I'm doing wrong? Thanks.
You cannot use any personal email ($_POST['cf_email']) in the 'From' header.
Also you have to add 'reply-to'.
Please check below thread for more info.
problem with php mail 'From' header

Need a PHP email send script for HTML an form

I have a single page portfolio website that I'm building and I have a contact form that I need to process using php send script. I'm a novice when it comes to PHP so I'm having trouble getting this to work. I've done some searching but I can't find what I'm looking for.
Here's what I have done, I copied this from a PHP contact page that I had built but the PHP and form are on the same page and I need an external send.php to process my form.
<?php
$error = ''; // error message
$name = ''; // sender's name
$email = ''; // sender's email address
$company = ''; // company name
$subject = ''; // subject
$comment = ''; // the message itself
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// the email will be sent here
// make sure to change this to be your e-mail
$to = "example#email.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "From : $name \r\ne-Mail : $email \r\nCompany : $company \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
}
}
if(!isset($_POST['send']) || $error != '')
{
header("location: http://www.#.com/#contact");
}
?>
So for my form I want to have:
<form method="post" action="send.php" class="form">
I plan on using HTML5 and jQuery to validate the form, so I really only need the script to capture the info and send the email to a single address. After it sends I want the script to redirect back to the Contact page.
Edit:
I found a solution after spending a while on google.
http://www.website.com/#contact");
?>
For one thing, you haven't initialized the value for $message
$message = stripslashes($message);
You probably meant to use $comment instead of $message
Not sure what problem you're facing. Simply copy the PHP you have into a file called send.php and my first glance says it'll work if you change $message back to $comment and add error checking. If you still have issues, post back with more details.

php email - $to multiple recipients

Even this must have been be asked many times, I will ask again since I cannot get it to work.
I am using php mail($to, $subject, $message, "From: $mysite<$myemail>\nX-Mailer:PHP/" .phpversion()); to send email to a single recipient.
Now I need to sent it to more than one recipients. I know that normaly I could do:
$to = "emailA#here.com,emailB#there.com";
But I need the one of the recipients to be the user that fills in the form e.g.:
//get all form details
$email = $_POST['email'];
$to = "$email,emailB#there.com";
The above ($to) I don't know if it is correct or not but is not working for me...
If I leave only the $to = "$email"; it gets send to $email (meaning that my rest of the code is ok).
Any suggestion on what is or may be wrong here?
Thank you.
Add a CC to your header.
$header ="From: $mysite<$myemail>" . PHP_EOL;
$header .= 'CC: emailB#there.com' . PHP_EOL;
//Rest of headers here

Categories