Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP Authentication with PHP mail() function

Tags:

php

email

I'm stuck on trying to add SMTP Authentication to my php script using the PHP mail() function.

The script currently works but because it isn't using SMTP Authentication the path of the php file and many other sensitive details are being included in the header (account username, etc.).

I'm currently specifying some of the header information using "$headers = ", etc., but I understand I need to use SMTP Authentication to fix this.

Is there a simple way to make my script use SMTP Authentication without having to use phpmailer, etc? Can I simply specify the port, authentication, username, password?

Update: Here is come code:

            `code`$eol = PHP_EOL;
            $headers =  "From: Test <[email protected]>".$eol;
            $headers .= "Reply-To: [email protected]".$eol;
            $headers .= "MIME-Version: 1.0".$eol;
            $headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"".$eol.$eol;
            $subject = 'Subject Goes Here';
            $message="--".$random_hash.$eol;
            $message.="Content-Type: text/plain; charset=UTF-8".$eol;
            $message.="Content-Transfer-Encoding: 8bit".$eol.$eol;
            $message.="Hello,".$eol;
            $message.="Body content goes here.".$eol.$eol;
            $message.="Thank you,".$eol.$eol;
            $message.="--".$random_hash.$eol;
            @mail(to, subject, message, headers);`code`
like image 441
Beccas Avatar asked Sep 05 '15 13:09

Beccas


People also ask

What is the difference between PHP mail () and SMTP Authentication?

Unlike PHP mail (), SMTP authentication allows you to use a third-party email account with your contact form. The main thing here is to configure the connection properly, that is to set the corresponding email server, port, username and password. Below you can find the list of different CMS and the way PHP mail ()/SMTP authentication can be set up:

How do I send PHP mail via SMTP?

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 the PEAR Mail package. This article explains how to use the PEAR option. When should you use this option?

Does postphp have authentication on the mail-command?

PHP does have authentication on the mail-command! The following is working for me on WAMPSERVER (windows, php 5.2.17) [mail function] ; For Win32 only. SMTP = mail.yourserver.com smtp_port = 25 auth_username = smtp-username auth_password = smtp-password sendmail_from = [email protected] Show activity on this post.

How to configure SMTP settings in phpBB?

There you need to configure SMTP details as follows: Once the settings are entered, hit Save Config button: Now you can test this option via contact form in the same way as in example for PHP mail() above. By default phpBB is configured to use PHP mail() option which works only with local mailbox.


1 Answers

Why don't you try the Pear Mail interface something like this:

require_once "Mail.php";
$username = '[email protected]';
$password = 'password';
$smtpHost = 'ssl://smtp.gmail.com';
$smtpPort = '465';
$to = '[email protected]';
$from =  '[email protected]';

$subject = 'Contact Form';
$successMessage = 'Message successfully sent!';


$replyTo = '';
$name = '';
$body = '';


$headers = array(
    'From' => $name . " <" . $from . ">",
    'To' => $to,
    'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
            'host' => $smtpHost,
            'port' => $smtpPort,
            'auth' => true,
            'username' => $username,
            'password' => $password
        ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo($mail->getMessage());
} else {
    echo($successMessage);
}

More information at https://goo.gl/HjffYA

EDIT:

The only way to this without more coding or using external library, is to update sendmail:

Define SMTP Server

smtp_server=mail.mydomain.com

If you need to change the smtp and SSL ports ; smtp port (normally 25)

smtp_port=25

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=auto

And finally your authentication credentials for SMTP server:

auth_username=username
auth_password=password

Ref: http://php.net/manual/en/ref.mail.php

like image 56
Moïze Avatar answered Oct 15 '22 06:10

Moïze