How do I specify to PHP that mail() should be sent using an external mail server?
Asked Answered
G

7

11

I have my email hosted at Rackspace Email and would like to use that as my mail server for the contact form on my website.

Looking at the php.ini file, I'm only able to specify the sendmail_path on UNIX systems, from which I've read points to the program that actually sends mail on the server.

I do not want to send mail from my Ubuntu server since I'm not experienced enough to make a secure setup for email... I would like to relay everything to Rackspace's mail.emailsrvr.com.

My question is, how do I specify to the PHP setup on my server that the mail() function should be using an external mail server?

Goblet answered 4/7, 2011 at 15:35 Comment(2)
Props for not trying to send mail from your webserver directly. While you could reconfigure your sendmail.cf to use an external mailserver, it's going to be easier to use a PHP package as mentioned in the answers. I use Swiftmailer with a 3rd-party SMTP (SendGrid, a Rackspace cloud-based service) which so far seems to work quite well.Unarmed
Got Swiftmailer set up with Rackspace Email, will be switching to SendGrid through Rackspace later once email volume increases. Thanks!Goblet
J
14

mail() is intended to hand off to a local SMTP server, and does a poor job of it. For proper mail support, use Swiftmailer or PHPMailer, both of which fully support external SMTP servers and are far easier to use (plus letting you do things like mixed text/html mails, attachments, etc...)

Janik answered 4/7, 2011 at 15:39 Comment(1)
Swiftmailer was extremely easy to set up, worked instantly with the proper SMTP settings.Goblet
R
15

Since I was researching this issue and stumbled across this post and a third-party php library was not an option for me.

As we know, php uses the sendmail command of the server by default The sendmail_path option in php.ini can be changed to override the setting to your own command with it's own arguments, etc. For example: sendmail_path = /usr/bin/unix2dos | /usr/bin/dos2unix | /usr/sbin/sendmail -t -i

SSMTP will allow you to direct outbound emails to a mailhost from your web/php server. https://wiki.archlinux.org/index.php/SSMTP

apt-get install ssmtp

Then you can use sendmail_path = /usr/sbin/ssmtp -t to tell php to use ssmtp instead of sendmail. Be sure to restart your web server after you have made changes to php.ini

Also ensure you have configured ssmtp and validated your SPF, DKIM, DMARC records before you make the changes to sendmail_path in php.ini

For example gmail Mail server. /etc/ssmtp/ssmtp.conf

# The user that gets all the mails (UID < 1000, usually the admin)
[email protected]

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also http://mail.google.com/support/bin/answer.py?answer=78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=yourdomain.com

# The full hostname
hostname=FQDN.yourdomain.com

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
[email protected]
AuthPass=postmaster-password

# Email 'From header's can override the default domain?
FromLineOverride=yes

For a stack exchange question to the same see https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server

To expand on this.

If using Google, each From: email address must be setup on the sending account as an "Account You Own" setting under accounts. Otherwise google will rewrite the headers with x-google-original-from and specify the From as the sending account instead.

Reparation answered 12/2, 2015 at 0:12 Comment(1)
An alternative method is to configure the local mail server (MTA) to use the SMTP relay address for your email hosting provider instead. Documentation GMail and O365. and RackspaceReparation
J
14

mail() is intended to hand off to a local SMTP server, and does a poor job of it. For proper mail support, use Swiftmailer or PHPMailer, both of which fully support external SMTP servers and are far easier to use (plus letting you do things like mixed text/html mails, attachments, etc...)

Janik answered 4/7, 2011 at 15:39 Comment(1)
Swiftmailer was extremely easy to set up, worked instantly with the proper SMTP settings.Goblet
J
5

For those who don't want to use a PHP library such as Swiftmailer (and ultimately those who don't want to touch their PHP codebase just to switch SMTP servers), you can do either one of the following:

1.) Windows Servers: Modify your PHP INI file to use an external SMTP relay host. You'll see it in the mailer section labeled "For Windows servers only" - or something similar.

2.) Linux Servers: Install Postfix (e-mail relay service) and configure that to use an external SMTP host. Your PHP installation will attempt to use this to send e-mails by default without any additional configuration.

**This is obviously not intended to give you step by step details on either option above, but rather to point you in the right direction if you're looking for a solution that doesn't require changing instances in your code where PHP's mail() is called.

Jarrett answered 12/10, 2015 at 17:35 Comment(0)
B
2

The default PHP function 'mail()' will only get the basic functionality to send an email. For Rackspace, you'll probably need to setup an SMTP connection to their mail server. To do this it's best to get a more advanced and developed mailing class. Several code frameworks have them available. If you're looking for a good package, check out PHP Mailer. It's almost a standard these days.

http://phpmailer.worxware.com/

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "[email protected]";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
Banville answered 4/7, 2011 at 15:43 Comment(0)
D
2

Not related to the question, but there are mailer daemons that only acts as a sendmail daemon, but relays to an external mail.

http://freshmeat.net/projects/nullmailer/

If you don't even need an exim/sendmail install on your machine, I suggest you try that. Of course, you can still use other third party alternatives, however if you run a daemon locally it will be able to queue the mail as well, which a php lib can not, if the relaying smtp isn't available.

It is part of the normal repo for Debian so I guess that is true for ubuntu as well, just apt-get install nullmailer should suffice. Then you can configure it with 1 or more smtp relays that it is allowed to use.

See more here: http://packages.ubuntu.com/oneiric/nullmailer

As a side note, a linux system without a mailer system becomes crippled in many other ways, so I think it's a good idea either how.

Dumpcart answered 4/7, 2011 at 15:44 Comment(0)
T
1

Setting up the internal mail function to use SMTP is only available on Windows. On other platforms, PHP should use the locally available sendmail or sendmail drop-in just fine.

If you want to use a SMTP under a non-Windows server you will have to use a third party library such as my favorite Switfmailer.

With Swiftmailer sending a email looks like this:

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$result = $mailer->send($message);
Theoretician answered 4/7, 2011 at 15:42 Comment(0)
T
0

This can easily be done with msmtp as described in this article.

Thigh answered 22/8, 2023 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.