Many people want to send smtp email through wordpress. WordPress uses the PHPmailer in there CMS. But many wordpress developer dont know how to use the wordpress phpmailer functionality. Using PHPmailer you can send the email using STMP service.

send smtp email through wordpress

send smtp email through wordpress

Many people use the wordpress plugins for sending the email. But I recommend not to use any wordpress plugin code for this. Because many of wordpress plugin code are not necessary and that will not useful for your wordpress application.

Here in this article I will show you how to sent email using wordpress with SMTP settings.

Use the following code for this:


// SMTP email sent
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
$phpmailer->SMTPAuth = true;
$phpmailer->Username = 'Username';
$phpmailer->Password = 'password';

$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host       = "hostname.com"; // SMTP server
$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message];                      //HTML Body
$phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$phpmailer->WordWrap   = 50; // set word wrap
$phpmailer->MsgHTML($_POST[your_message]);
$phpmailer->AddAddress('support@wordpressapi.com/files/', 'Wordpress support');
//$phpmailer->AddAttachment("images/phpmailer.gif");             // attachment
if(!$phpmailer->Send()) {
 echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
 echo "Message sent!";
}

Use the above code for sending the email from wordpress. With godaddy hosting service above code is very useful for creating the contact us page.

Many wp developers asked me where to add the above code. So here is answer. You can create wordpress theme template page. Like contact us page and add the above code in that page according to your requirement.

If you are having any issues or question about using the code then please write to me.

Written by WPAPI

I am from India, Pune. I am WordPress and Drupal Architect and consultant. I worked on many technologies like Ruby on Rails, Flex, PHP, jQuery etc... Senior Technical Lead at Smartsourcing Global Pvt Ltd in Princeton, NJ, USA. I contributed to many wordpress plugins and Drupal Modules. I contributed to Drupal core also. Connect to purab on LinkedIn or Follow @purab on Twitter or Follow me on or Write Mail to purabdk@gmail.com or Chat with me on Skype : purabdk

This article has 19 comments

  1. Cameron Reply

    You didn’t say how you’re sending the $_POST[your_message] variable. Could you give an example of that? Say you want to email a post you just wrote to someone. How would you go about sending the text of that post through the phpmailer you’re showing?

  2. Millard Lymaster Reply

    Excellent stuff! I have to agreed with what you may have written, as well as the info that you just have provided will help a good deal of men and women available too. Maintain the excellent high quality work.

  3. wptemplates Reply

    hi

    thanks for the tutorial. one question though … what would be the username and password. is it email username and password or domain username and password or wp admin username and password or what?

    • Wordpress API Reply

      For sending email dont use the wp admin username and password. You need get that from your hosting provider. (ask them for give smtp details for email address of your domain)

  4. Carmon Gata Reply

    I REALLY liked your post and blog! It took me a minute bit to find your site…but I bookmarked it. Would you mind if I posted a link back to your post?

  5. Joan Dafoe Reply

    Intriguing post. I have been searching for some good resources for solar panels and discovered your blog. Planning to bookmark this one!

  6. Adelle Duggin Reply

    I REALLY liked your post and blog! It took me a minute bit to find your site…but I bookmarked it. Would you mind if I posted a link back to your post?

  7. Taylor Pniewski Reply

    Hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work.

  8. sameera Reply

    I got this error “SMTP server error: sender rejected by administrator ” any one can help?

  9. Mrinal Nandi Reply

    Hi,

    I just solved the smtp send mail problem from wordpress using a little bit coding. I didn’t used any plugin and didn’t found any configuration setting in wordpress for SMTP (I am new in WP). I did it for my gmail account.
    Here is the code snippet which I placed just bellow line no. 405 (with the content “$phpmailer->IsMail();”) and in the function “wp_mail” in wordpress_installation_folder/wp-includes/pluggable.php file.

    $phpmailer->SMTPAuth = true;
    $phpmailer->IsSMTP(); // telling the class to use SMTP
    $phpmailer->Host = “ssl://smtp.gmail.com”; // SMTP server
    $phpmailer->Username = ‘my.mail.id@gmail.com';
    $phpmailer->Password = ‘placed_my_password_here';
    $phpmailer->Port = 465;

    It worked fine for me and definitely will work for you, too.

    But my question is that, Is there any setting already for SMTP in the wordpress admin panel? Can anyone tell me plz?

  10. ReasonRefills Reply

    Can you tell us WHERE to put this code? which document does this go in?

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>