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>