PHPMailer with Gmail SMTP server

I am trying to get phpmailer to send emails using Gmail’s smtp server. But I am not sure what I am doing wrong.

I am copying my code for my php file and my html files below. Any help would be greatly appreciated.

My php code


<html>
<head>
<title>Mail processing</title>
</head>
<body>

<?php
	//configs
	$Host = "smtp.gmail.com";
	$Port = 587;
	$Username = $_POST['mailfrom'];
	$Password = "gmailpassword";


	// processing start
	if(isset($_POST['nmto'])){
		
	$fromName 					=	$_POST['nmfrom'];
	$toName 						=	$_POST['nmto'];
	$mailAddressfrom 		=	$_POST['mailfrom'];
	$mailAddressto 			=	$_POST['mailto'];
	$subj 							=	$_POST['mailsubj'];
	$description 				=	eregi_replace("[\]",'',$_POST['maildsc']);
	



require_once('PHPMailer/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
//  $mail->Host       = "smtp.gmail.com"; // SMTP server
//  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = $Host; // sets the SMTP server
  $mail->Port       = $Port;                    // set the SMTP port for the GMAIL server
  $mail->Username   = $Username; // SMTP account username
  $mail->Password   = $Password;        // SMTP account password
  $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
  $mail->AddAddress($mailAddressto, $toName);
  $mail->SetFrom($mailAddressfrom, $fromName);
  $mail->AddReplyTo($mailAddressfrom, $fromName);
  $mail->Subject = $subj;
  $mail->AltBody = $description; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML($description);
//  $mail->AddAttachment('images/phpmailer.gif');      // attachment
//  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
	

	
  echo "Message Sent</p>\n";
	echo '<a href="javascript: history.go(-2)">GO BACK 2</a><p>';
	echo '<a href="javascript: history.go(-3)">GO BACK 3</a><p>';
} catch (phpmailerException $e) {
//  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
//  echo $e->getMessage(); //Boring error messages from anything else!
}

}
	?>

</body>
</html>

My HTML form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
<link href="style.css" rel="stylesheet">
</head>

  <body>
  <form action="carolyn.php" method="post">
		<table cellpadding="10" cellspacing="0">
    	<tr>
      	<td valign="top"><p>Name (from)</p> <input type="text" name="nmfrom"  required="required" size="30" value="persons First Name"></td>
        <td valign="top"><p>Name (to)</p> <input type="text" name="nmto" required="required" size="30" value="Steve"></td>
      </tr>  
      <tr>
      	<td valign="top"><p>Email (from)</p> <input type="text" name="mailfrom" required="required" size="30" value="[email protected]"></td>
        <td valign="top"><p>Email (to)</p> <input type="text" name="mailto" required="required" size="30" value="[email protected]"></td>
      </tr>
      <tr>
      	<td valign="top" colspan="2"><p>Subject (required)</p> <input type="text" name="mailsubj" required="required" size="50" value="check unsub"></td>
      </tr>
    	<tr>
      	<td valign="top" colspan="2"><p>Your message (required)</p> <textarea name="maildsc" required="required" cols="60" rows="10" > check if works in gmail</textarea> </td>
      </tr>
    	<tr>
      	<td valign="top" colspan="2"><input type="submit" value="Submit" /></td>
      </tr>
    </table>
  </form>  
    
  </body>
</html>

What happens? Do you get any error messages?

There is no error message it just returns a blank page and the email is not sent.

Hi braddx15,

Could you let me know where you found the PHPMailer you’re using. The one I have found seems to have different syntax than yours, and I would need to be able to see their suggested or example syntax to see what’s happening with your code.

I am including my full code and supporting files.
http://www.simgratton.com/help.zip
Let me know if you need anything else and I appreciate the help you all have given me so far.

Are you trying to use this on a paid hosting server? I ran into the same thing and after talking to my host, found that they don’t allow smtp mail sending on their servers. Just a thought.

I have a few questions before I’m able to really help you out here.

I’ve noticed you’re taking in the username for the SMTP server from the form but you’re never asking for the password to an account. You’re simply passing in ‘passwordgoeshere’ as the password. I understand if this is a placeholder.

The issue I see is if I filled out this form and my email address is [email protected] then in order to use that as the username for the SMTP connection you’ll also have to pass along my password so gmail could authenticate me. As of right now I don’t see how you’re planning on allowing gmail to authenticate anyone who is filling in the form so you’re connection is just being refused.

As a side note if I try to set the sent from address to anything else it won’t matter. If I connect to gmail with an account of [email protected] and want to send out something but set it to [email protected] it’s still going to send it out from [email protected] because that’s the account I’m logged in with.

I hope this makes sense, if not let me know

Valandor

It makes complete sense and I get what you are asking. The form will only be used by one or two people who will have different email addresses but the same password. the script is part of a larger application.

Okay,

I would double check to make sure your hosting service allows SMTP mail as suggested by fastsol.

In order for it to work on my local host I had to change the port to 587 and let it know it was going to use a TLS connection as it defaults to SSL.

Also if they have their accounts set up for application specific passwords it may end up not allowing them to authenticate as it would make them use a different password for than they log in with at Gmail.

If you haven’t run it with the debug enabled I would suggest seeing if that gives you any error messages. It’s set to off by default and may be the reason for simply seeing a blank page. It could be suppressing it from throwing a new error message.

So try running

[php]

<?php //configs $Host = "smtp.gmail.com"; $Port = 587; //Changed from 587 $Username = $_POST['mailfrom'];; $Password = 'passwordhere'; // processing start if(isset($_POST['nmto'])){ $fromName = $_POST['nmfrom']; $toName = $_POST['nmto']; $mailAddressfrom = $_POST['mailfrom']; $mailAddressto = $_POST['mailto']; $subj = $_POST['mailsubj']; $description = eregi_replace("[\]",'',$_POST['maildsc']); require_once('PHPMailer/class.phpmailer.php'); //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch $mail->IsSMTP(); // telling the class to use SMTP try { // $mail->Host = "smtp.gmail.com"; // SMTP server $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) -- Disable In Live Environment $mail->SMTPAuth = true; // enable SMTP authentication $mail->Host = $Host; // sets the SMTP server $mail->Port = $Port; // set the SMTP port for the GMAIL server $mail->Username = $Username; // SMTP account username $mail->Password = $Password; // SMTP account password $mail->AddAddress($mailAddressto, $toName); $mail->SetFrom($mailAddressfrom, $fromName); $mail->AddReplyTo($mailAddressfrom, $fromName); $mail->Subject = $subj; $mail->AltBody = $description; // optional - MsgHTML will create an alternate automatically $mail->MsgHTML($description); $mail->SMTPSecure = "tls"; //Set Connection To TLS -- SSL Is Default // $mail->AddAttachment('images/phpmailer.gif'); // attachment // $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment $mail->Send(); echo "Message Sent\n"; echo 'GO BACK 2

'; echo 'GO BACK 3

'; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { // echo $e->getMessage(); //Boring error messages from anything else! } } ?>

[/php]

The above works fine when on my localhost running XAMPP when I add in my gmail information as the username/password. It wasn’t authenticating prior to making the few changes I made.

Yep!!! That did it. Thanks a ton everyone.

Sponsor our Newsletter | Privacy Policy | Terms of Service