0

I want to know how I can set up automatic SMTP mailing system in php 7.2 in 2020. I know about mail() function but that comes after the initial set up.

I have seen the other questions on stackoverflow about it, but I am posting this question bc I only found only outdated questions from like 2011 or 2012 and PHP has changed a lot from that time from security aspect and other aspects.

Here is what I tried:

From what I found I should be changing ini_set() in php.ini file but there are none of ini_set() functions there. What I did is I changed smtp to smtp=my-mail-server-of-choice-here and smtp_port to smtp_port=587 like I was told to.

Also, I should be updating sendmail.ini in sendmail folder but (guess what) sendmail folder doesn't exist -> that also means sendmail.exe and sendmail.ini also don't exist

I am also getting this error mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

This is the file:

$to = 'my-users-mail@some-random.mail';
$subject = "HTML email";

$message = "Hi Bob!";

$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers .= 'From: my-mail@gmail.com'."\r\n";

mail($to,$subject,$message,$headers);

which (from what I read) should be fixable from ini_set() which doesn't exist anywhere in php.ini file - I guess that bc only outdated solutions are available

What is the modern standard, php 7.2, way of doing this that's actually going to work and be safe?

BTW I am using XAMPP v3.2.4 (I will be moving to WAMPP in production) on localhost and I am using gmail as my mail service

Misa Pisa
  • 21
  • 4
  • Do you *need* to use mail()? If not there's an answer updated on Aug 3rd here: https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server Another option being: https://github.com/PHPMailer/PHPMailer returned from a quick google search. I'd answer, but not sure one is needed? I do appreciate you looking for a modern solution though – TCooper Sep 18 '20 at 22:49
  • 1
    I don't need to use mail(). I will check this out and I will let u know. Thanks! Also I don't really want to use extensions, classes and what not for the simple 'thank u for joining our website' mail. I was looking for simple solution. – Misa Pisa Sep 18 '20 at 22:51
  • I get that... if you want to send straight from your server: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-on-ubuntu-14-04 and/or https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-16-04 - obviously you'll have to adapt to a wamp(not sure where xamp is set up for testing) environment or find another tutorial, but incase these are helpful. If you're strictly windows based there are alternatives – TCooper Sep 18 '20 at 22:53
  • Thanks a lot, but I am using windows (wamp stack) and the link you sent is for ubuntu os – Misa Pisa Sep 18 '20 at 22:55
  • Gotcha, https://alternativeto.net/software/postfix/?platform=windows - the site is a good one to keep around in general – TCooper Sep 18 '20 at 22:57
  • Thanks a lot, I will try that and when I will let u know if I find a way. I will also write an answer to this question – Misa Pisa Sep 18 '20 at 22:59

1 Answers1

2

You have a bad idea of "modern" thinking. The new way of doing things is actually using extensions, frameworks etc etc... - reusable code is the word in this fast expanding it world

Back in like '90s or '80s people didn't have the internet so widely available or available at all and if you wanted to (lets say) make connection between two computers (think NASA or something) you would have to write your own protocols (POP or IMAP) from scratch and spend weeks or even months in programming.

Now its 2020. and we have a lot of reusable code widely available, repositories, open-source software etc... you get the picture

Sure you can write you own authentication in php in like a week or you can simply download

Net_Socket (i have 1.2.2) -> https://pear.php.net/package/Net_Socket/download/ Net_SMTP (i have 1.9.2) -> https://pear.php.net/package/Net_SMTP/download/ Mail_extension (I have version 1.4.1) -> https://pear.php.net/package/Mail/download/

extract everything with 7-zip and structure it like this

your main folder

.Mail-1.4.1
    >Mail.php
    >Mail
        > some default files(dont touch these)
        > Net(here you paste files from Net_SMTP and Net_Socket - they should be named SMTP.php and Socket.php)

. sendmail.php

In sendmail.php you write this:

//Make sure you made your folder/file structure like you should
require_once "./Mail-1.4.1/Mail.php";

$host = "your-mail-server-of-choice-here";
$port = "465";
$username = "your mail or username";
$password = "your password";

//setting up smtp connection
$smtp = Mail::factory(
    'smtp',
    array (
        'host' => $host,
        'port' => $port,
        //you don't need this if u are using mail server that doesn't need authentication
        'auth' => true,
        'username' => $username,
        'password' => $password
    )
);

$from = "your-mail-here";
$to = "recepient-mail-here";
$subject = "Ay bro!";
$body = "Your message here!";

$headers = array (
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
    exit( "Error happened :( -->".$mail->getMessage() );
}

That was easy right? If we went your way, you would spend a lot of time and tears(yes tears) on making all of these connections and stuff and make it safe etc etc. I hope you are happy with this results!

Jon Nezbit
  • 317
  • 9
  • @JonNezbit Thans a lot! What I also needed to do is reset everything I changed in `php.ini` and it worked finally! Upvoted and make an accepted answer to my question! – Jon Nezbit Sep 19 '20 at 13:32