Our blog, keeping you up-to-date on our latest news.

 

Use Gmail SMTP Server as a Free Mail Server for Your WordPress

September 4, 2010 at 12:26 pm | Blog | 5 comments

 

Folks like me are using free web hosting for personal website. And one of the main headaches for free hosting is that, it does not provide SMTP server for free. I am using bytehost for my wordpress blog hosting. Everything seems fine so far, but the problem is always the SMTP server. For example I am using a contact form, and user can leave messages to me through email. the original code reads:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_to = trim($_POST['your_email']);
$your_web_site_name = trim($_POST['your_web_site_name']);
 
$headers = 'From: ' . $your_web_site_name . ' <' . $email_to . '>' . "\r\n" . 'Reply-To: ' . $email;
 
if (mail($email_to, $subject, $message, $headers)) {
    echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..      
} else {
    echo 'failed'; // ... or this one to tell it that it wasn't sent    
}

but, we can not use PHP’s mail function if it is not enabled by the hosting provider. Fortunately enough, Google’s Gmail is coming to save us. It is totally free, and more stable than any other providers. And overall, it is very easy to setup.
1. use phpmailer class to send email.
2. use smtp configuration for email.

the changed file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
include ('../../../wp-includes/class-phpmailer.php'); 		
include ('../../../wp-includes/class-smtp.php'); 
    //we need to get our variables first
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_to = trim($_POST['your_email']);
$your_web_site_name = trim($_POST['your_web_site_name']);
 
    $mail=new PHPMailer();  		
    // Let PHPMailer use remote SMTP Server to send Email 		
    $mail->IsSMTP();  		
    // Set the charactor set. The default is 'UTF-8' 		
    $mail->CharSet='UTF-8';  		
    // Add an recipients. You can add more recipients 		// by using this method repeatedly. 		
    $mail->AddAddress($email_to);  		
    // Set the body of the Email. 		
    $mail->Body=$message;  		
    // Set "From" segment of header. 		
    $mail->From=$email;  		
    $mail->AddReplyTo($email, $email);  		
    // Set addresser's name 		
    $mail->FromName=$name;  		
    // Set the title 		
    $mail->Subject=$subject;  		
    // Set the SMTP server. 		
    $mail->Host='smtp.gmail.com'; 		
    $mail->Port= 465; 		
    $mail->SMTPAuth = true; 		
    $mail->SMTPSecure = "ssl";   		
    $mail->Username='youraccount@gmail.com'; 		
    $mail->Password='yourpassword'; 		 		
    // Send Email. 		
    if($mail->Send()){
        echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..      
    } else {
        echo 'failed'; // ... or this one to tell it that it wasn't sent    
    }

The following settings specify using gmail’s smtp.

// Set the SMTP server.
$mail->Host=’smtp.gmail.com’;
$mail->Port= 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = “ssl”;
$mail->Username=’youraccount@gmail.com’;
$mail->Password=’yourpassword’;

Now you can send me messages through the contact form in the contact page!!

And if you are using my Aurelius theme, you can replace the send_mail.php with the code above to send email.

Thanks for reading.

the end!!

<< Back to Blog Discuss this post

 

5 Comments to “Use Gmail SMTP Server as a Free Mail Server for Your WordPress”

  1. popxi says:

    great post

    admin Reply:

    thanks, your website is also good

  2. Asshu says:

    Great article !

  3. Maris says:

    Great blog it’s not often that I comment but I felt you deserve it.