Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Trying to send an email using the Simple PHP course, and I'm messing up just about every step.

Alright, this is going to be a complicated explanation. I have an old Mac which I have turned into my hosting server. Advantages and disadvantages galore, but even though there's constant need of updates and such, I do get to try anything and everything almost immediately. Anyways here's my problems.

So I tried transferring the files over using Filezilla and everything went over well except the class.phpmailer.php file, which turned into gibberish that looked like this: 8^@^A?xic14^@^C?{ic09^@^C?{ic10^@ ?^Kis32^@^@^B? ??????????+?? ??????F??^K??F:?????K??M{?C??3??? ?O??^X

...for about four thousand lines. I managed to quasi fix it by walking over to my server, which luckily still has a working screen and a simple graphical user interface, and simply copied the code from the PHPmailer homepage. When I opened class.phpmailer.php outside of command line programs and instead use a text editor, "book" (without quotes) was the only text in the whole file. I promptly deleted it and added in the new code from the homepage, which I can still see, so I'm under the assumption that it works.

So with the current class.phpmailer.php and class.smtp.php files that I got from the link in the videos (which are <em>way</em> different than the video, so I don't even know if I'm doing that remotely correctly) I set up a contact form that is identical to the form from Randy Hoyt's videos and subsequent blog post, aside from a few extra variables of course. I am using smtp.postmarkapp.com and port 25 (which was what they told me to do on Postmarkapp.com even though Randy used 26). It wasn't until I checked the forums that I saw a lot of people use Gmail for their SMTP...

Anyways, I hit submit on this dumb form and get this crazy error:

Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP Connect() failed.' in /var/www/hibenmedia/inc/phpmailer/class.phpmailer.php:1026 Stack trace: #0 /var/www/hibenmedia/inc/phpmailer/class.phpmailer.php(899): PHPMailer->SmtpSend('Date: Sat, 7 Se...', '--b1_eda4efa921...') #1 /var/www/hibenmedia/inc/phpmailer/class.phpmailer.php(820): PHPMailer->PostSend() #2 /var/www/hibenmedia/questionnaire.php(119): PHPMailer->Send() #3 {main} thrown in /var/www/hibenmedia/inc/phpmailer/class.phpmailer.php on line 1026

I'm not gonna lie, the words "Fatal error" terrify me. It makes me think I'm going to hear an explosion over my shoulder and see chunks of my computer fly across my little office space in my home, followed by smoke, followed by fire.

Anyways, I'm great at following directions, so I would greatly appreciate some more experienced people to help guide this beginner through the treacherous forests of contact forms.

I really enjoyed being walked through the project step by step, but the blog sort of felt like we were dropped in the water and told to swim. I'm not opposed to that, I'm just kind of drowning it seems. I need a lifeline tossed to me.

So here are some questions I would love answered and that I believe would help a lot of n00bs like myself:

Can anybody walk me through the steps of getting it to work on my localhost? I have no idea whether or not filling out the form on my localhost is supposed to be able to access my SMTP third party server or if this can only be done from the actual server in order to test it.

Also do I need to do something with my gmail in order to use it as my SMTP (like turn on a particular setting) or can I just enter the username and password?

http://phpmailer.worxware.com/?pg=examplebgmail

I can't tell from this example, so I'll keep looking as I await a reply.

Final question, should I post my code?

Thanks bunches to the saint that helps me with this.

1 Answer

I used the following set up for my computer (localhost) to send through Gmail. This was taken from my shirts4mike contact form.

$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;

$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "myemail@gmail.com";
$mail->Password = "MyPassword";

$mail->SetFrom($email, $name);
$address = "myemail@gmail.com";
$mail->AddAddress($address);
$mail->Subject    = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->IsHTML(true);
$mail->Body = $email_body;

if($mail->Send()) {
    header("Location: " . BASE_URL . "contact/?status=thanks");
    exit;
} else {
    $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}

I used the following blog post.

http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

Thank you for this. I have since gone into attempting to use a MySQL database, so now I get to try and do them both at the same time. hehe, this should be fun. I'm getting all sorts of fun learning experience from this site.