2

How do I send PHP mail via SMTP?

 2 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/216140597-How-do-I-send-PHP-mail-via-SMTP-
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I send PHP mail via SMTP?

Overview

When you use the PHP mail function, you are sending email directly from your web server. This can cause issues if the FROM address isn’t set properly or if your email isn’t hosted with DreamHost. Sending mail via SMTP is recommended as email is sent from the mail server rather than the web server. View the PHP mail troubleshooting article for details.

There are a few options to send PHP mail via SMTP. For example:

  • Using PHPmailer
  • Using the PEAR Mail package.

This article explains how to use the PEAR option.

When should you use this option?

You should only use this option if you're creating a custom built mail form. If you're using software such as WordPress, you should use the built-in tools or plugins to send via SMTP instead.

Configuring PEAR to send mail

The following steps walk you through how to enable this:

  1. Visit the PEAR article to install PEAR on your web server.
  2. Install the necessary PEAR Mail packages. Visit PEAR Troubleshooting for instructions. For example, you'll need the Mail and Net_SMTP packages.
    [server]$ pear install --alldeps Mail
    
  3. Create a PHP file that uses PEAR to send mail via SMTP.

Configuring PHP to send SMTP mail

The following example can be used to send SMTP mail using the PEAR Mail package. Create a file named something like phpmail.php with the following code.

You only need to update the fields which are highlighted.

dh-kb-important-icon.svg

The Mail.php file is located in the /home/username/pear/share/pear directory. It was created when you installed the Mail package and is being referenced from the code you placed in your phprc file from when you installed Pear.

<?php

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

require_once "Mail.php";

$host = "ssl://smtp.dreamhost.com";
$username = "[email protected]";
$password = "your email password";
$port = "465";
$to = "[email protected]";
$email_from = "[email protected]";
$email_subject = "Subject Line Here:" ;
$email_body = "whatever you like" ;
$email_address = "[email protected]";

$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);

if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?>

If you’re using Google to host your email, you must change the $host line to the following:

$host = "ssl://smtp.gmail.com";

Allowing GMAIL to send from your mail application

If you're using your GMAIL address to send via SMTP, you must first allow your application access to your GMAIL address. If you do not do this, your email will not authenticate and not send. View the following article for details:

See also

Did this article answer your questions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK