Jump to content

Mail function not working


Go to solution Solved by requinix,

Recommended Posts

hey guys,


i am trying to auto generate an email. i have a function that calls for the following use of :


	$emailSub = 	'Test Subject';
	$emailTo = 		'myemail@gmail.com';
	$emailMsg = 	'You have a new email that must be read below:<br> ';


i dont seem to get any email. 


any suggestions as to why ?




Link to comment
Share on other sites

  • Solution

There is so much more to doing emailing properly, and so many ways it can go wrong.


Do yourself a favor and get yourself a copy of PHPMailer. (There are other things but it's the most popular.) There are tons of examples and plenty of documentation on how to use it, and it will take care of everything for you.

  • Like 2
Link to comment
Share on other sites

Thanks for the feedback Requinix.


I used a mail function library and installed this into my site. ( i added the following code into a file called mailtest.php ) ran it and success, i got my test email sent to me and everything was all good.


I then tried to add this into my existing ( working ) php contact form that uses jquery and ajax to run a no refresh UI form. the form worked and all the functions ran perfectly. So i assumed just adding the code that worked inside one file would work inside my working conditional of the "completed" form. 


Does not seem to work :(.


Not sure why the email wont go out anymore.



php file that the ajax / jquery is taking from.



$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$honeypot = $_POST['honeypot'];
$humancheck = $_POST['humancheck'];

if ($honeypot == 'http://' && empty($humancheck)){  /// RUN SPAMBOT CHECK AND IF PASS CHECK TO MAKE SURE ALL FIELDS ARE OK

	$errorMsg = '';
	$reg_exp = "/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/";
	if(!preg_match($reg_exp, $email)){
			$errorMsg .=  "<p>A valid email is required</p>";
			$errorMsg .= '<p>Please provide your name</p>';
			$errorMsg .= '<p>Please provide a message</p>';
		$return['error'] = true;
		$return['msg'] = 'Sorry the request was successful but your form was not correctly filled. ' . $errorMsg;
		echo json_encode($return);

                // ADD FOLDER FOR MAIL LIBRARY

		require 'PHPMailer-master/PHPMailerAutoload.php';
		$mail = new PHPMailer;

		//$mail->SMTPDebug = 3;                               // Enable verbose debug output

		$mail->isSMTP();                                      // Set mailer to use SMTP
		$mail->Host = 'smtp.mandrillapp.com';  // Specify main and backup SMTP servers
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		$mail->Username = 'MYusername';                 // SMTP username
		$mail->Password = 'hidden_pass';                           // SMTP password
		$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
		$mail->Port = 587;                                    // TCP port to connect to

		$mail->From     = "myemail@domain.com";
		$mail->FromName     = "DO-NOT REPLY";

		$mail->Subject  = "First PHPMailer Message";
		$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
		$mail->WordWrap = 50;

		if($mail->Send()) {
		$return['error'] = false;
		$return['msg'] = '<h4>' . $name . ', thank you for your email.</h4>  Your email from: ' . $email.' has been sent and one of our team members will contact you shortly..';
		echo json_encode($return);
		}	else {
		$return['error'] = true;
		$return['msg'] = 	'THE EMAIL WAS NOT SENT .. PLEASE TRY AGAIN';
		echo json_encode($return);

} else {

		$return['error'] = true;
		$return['msg'] = 'Oops...there was a problem with your submission. Please try again' ;
		echo json_encode($return);


Again, the mail() function works when i load it outside the conditional. 


but then in checking the conditional, it works within the PHP script and executes properly (displaying, that the user is submitting and validating from correctly.

$return['msg'] = '<h4>' . $name . ', thank you for your email.</h4>  Your email from: ' . $email.' has been sent and one of our team members will contact you shortly..';

They just dont seem to work together ??


any suggestions ?



Link to comment
Share on other sites

So you're seeing that "thank you for your email" message but not receiving the email? Did you change anything after taking the code from the original test script (where you got the email using PHPMailer) and copy/pasting it into the script that handles the contact form? And I assume the values for Username and Password and From are all correct in your real code and you edited those just for posting it here?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.