LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-08-2021, 10:23 PM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
Set up email sender on a cloud server


I am very new to setting up a cloud server.

On my shared hosting webpages, I suppose email is set up already, my contact page sends me a message, no problem.

I have set up the contact page on the cloud server, it opens as it should.

What do I need to enable PHP to send an email on this Ubuntu 20.04 Server?

What do I need to install?

On the shared hosting, this little bit of PHP works fine.

Code:
<?php 

$q1 = $_POST['name'];
$q2 = $_POST['email'];
$q3 = $_POST['message'];
	
$body = "
".$q1."
".$q2."
".$q3."
";

$to1 = "me@mymail.com";
$subject = $q1 . " message";
$headers = "From: peter@mywebpage.com\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
mail($to1, $subject, $body, $headers);
?>
 
Old 11-09-2021, 04:19 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
I use phpmailer
https://github.com/PHPMailer/PHPMailer
 
Old 11-09-2021, 05:16 PM   #3
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
@michaelk: Thanks for your reply. I read up on PHPMailer. I think maybe it is a bit of an overkill for my purposes.

Maybe I could change the question:

Why doesn't PHP mail() just work? Must be installed with PHP.

Maybe a setting in php.ini? On my shared webhost, the above little php works fine.

I actually don't need to send attachments with emails.

For messages with attachments, I use php to gather name, student number, email, message and file.

The file gets uploaded, (cunningly to a folder called "files"), with the student number prefixed, the other info is written to a text file with the student number prefixed, also in "files".

Actually, I don't need emails for this, that would be just another step.

It is easier for me to zip the the content of "files" and download all of them together than open a load of emails.

Last edited by Pedroski; 11-09-2021 at 05:18 PM.
 
Old 11-09-2021, 06:03 PM   #4
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
I found that I needed to enable this line in php.ini to make sendmail work:

Quote:
; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i
But now, I get this message in /var/log/apache2/error.log:

Quote:
Please install an MTA on this system if you want to use sendmail!
What is MTA?
 
Old 11-09-2021, 06:17 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
MTA... https://en.wikipedia.org/wiki/Message_transfer_agent
 
Old 11-11-2021, 01:56 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Pedroski, I use msmtp for this.
 
Old 11-11-2021, 03:22 PM   #7
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
A further question if you have time:

Can I use phpmailer from my localhost?

I like to try things on my localhost first, write down all the steps, then try it on the cloud server.

I installed composer, then used composer to install phpmailer. Now I have a folder vendor in my home directory. (Not sure if that is the right place!)

I got the PHP below from here.

When I open sendMail.php in my browser, it takes a long time, then I get this error message:

Quote:
2021-11-11 21:04:27 Connection: opening to smtp.qq.com:465, timeout=300, options=array()
2021-11-11 21:04:27 Connection: opened
2021-11-11 21:04:57 SMTP INBOUND: ""
2021-11-11 21:04:57 SERVER -> CLIENT:
2021-11-11 21:04:57 Connection: closing due to error
2021-11-11 21:04:57 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMail...roubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMail...roubleshooting
Can't really see what exactly the error is!

Code:
<?php
 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '/home/pedro/vendor/autoload.php';
 
$mail = new PHPMailer(true); 
try {
    //Server settings
    $mail->SMTPDebug = 4;
    $mail->isSMTP();
    $mail->Host = 'smtp.qq.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'peter@foxmail.com';
    $mail->Password = 'mypassword';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 465;
 
    $mail->setFrom('peter@mydomain.com', 'Admin');
    $mail->addAddress('peter@foxmail.com', 'Recipient1');
    $mail->addAddress('pedroski@163.com');
    $mail->addReplyTo('noreply@example.com', 'noreply');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');
 
    //Attachments
    //$mail->addAttachment('/backup/myfile.tar.gz');
 
    //Content
    $mail->isHTML(true); 
    $mail->Subject = 'Test Mail Subject!';
    $mail->Body    = 'This is SMTP Email Test';
 
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
 
Old 11-11-2021, 03:58 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Just a quick glance looks ok except are you using the correct port? Should it be 587?

https://www.postbox-inc.com/setup/em...-via-imap-smtp
 
Old 11-11-2021, 10:05 PM   #9
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
@michaelk: Thanks for your reply.

I changed the port to 587, now I quickly get a much bigger error message. Any more tips?

My password is correct, I double-checked it. I also have an imap password, but I don't think I need that to send an email, do I??

Quote:
2021-11-12 03:56:11 Connection: opening to smtp.qq.com:587, timeout=300, options=array()
2021-11-12 03:56:11 Connection: opened
2021-11-12 03:56:11 SMTP INBOUND: "220 newxmesmtplogicsvrsza28.qq.com XMail Esmtp QQ Mail Server."
2021-11-12 03:56:11 SERVER -> CLIENT: 220 newxmesmtplogicsvrsza28.qq.com XMail Esmtp QQ Mail Server.
2021-11-12 03:56:11 CLIENT -> SERVER: EHLO localhost
2021-11-12 03:56:11 SMTP INBOUND: "250-newxmesmtplogicsvrsza28.qq.com"
2021-11-12 03:56:11 SMTP INBOUND: "250-PIPELINING"
2021-11-12 03:56:11 SMTP INBOUND: "250-SIZE 73400320"
2021-11-12 03:56:11 SMTP INBOUND: "250-STARTTLS"
2021-11-12 03:56:11 SMTP INBOUND: "250-AUTH LOGIN PLAIN XOAUTH XOAUTH2"
2021-11-12 03:56:11 SMTP INBOUND: "250-AUTH=LOGIN"
2021-11-12 03:56:11 SMTP INBOUND: "250-MAILCOMPRESS"
2021-11-12 03:56:11 SMTP INBOUND: "250 8BITMIME"
2021-11-12 03:56:11 SERVER -> CLIENT: 250-newxmesmtplogicsvrsza28.qq.com250-PIPELINING250-SIZE 73400320250-STARTTLS250-AUTH LOGIN PLAIN XOAUTH XOAUTH2250-AUTH=LOGIN250-MAILCOMPRESS250 8BITMIME
2021-11-12 03:56:11 CLIENT -> SERVER: STARTTLS
2021-11-12 03:56:11 SMTP INBOUND: "220 Ready to start TLS from 112.23.178.123 to newxmesmtplogicsvrsza28.qq.com."
2021-11-12 03:56:11 SERVER -> CLIENT: 220 Ready to start TLS from 112.23.178.123 to newxmesmtplogicsvrsza28.qq.com.
2021-11-12 03:56:11 CLIENT -> SERVER: EHLO localhost
2021-11-12 03:56:12 SMTP INBOUND: "250-newxmesmtplogicsvrsza28.qq.com"
2021-11-12 03:56:12 SMTP INBOUND: "250-PIPELINING"
2021-11-12 03:56:12 SMTP INBOUND: "250-SIZE 73400320"
2021-11-12 03:56:12 SMTP INBOUND: "250-AUTH LOGIN PLAIN XOAUTH XOAUTH2"
2021-11-12 03:56:12 SMTP INBOUND: "250-AUTH=LOGIN"
2021-11-12 03:56:12 SMTP INBOUND: "250-MAILCOMPRESS"
2021-11-12 03:56:12 SMTP INBOUND: "250 8BITMIME"
2021-11-12 03:56:12 SERVER -> CLIENT: 250-newxmesmtplogicsvrsza28.qq.com250-PIPELINING250-SIZE 73400320250-AUTH LOGIN PLAIN XOAUTH XOAUTH2250-AUTH=LOGIN250-MAILCOMPRESS250 8BITMIME
2021-11-12 03:56:12 Auth method requested: UNSPECIFIED
2021-11-12 03:56:12 Auth methods available on the server: LOGIN,PLAIN,XOAUTH,XOAUTH2
2021-11-12 03:56:12 Requested auth method not available:
2021-11-12 03:56:12 Auth method selected: LOGIN
2021-11-12 03:56:12 CLIENT -> SERVER: AUTH LOGIN
2021-11-12 03:56:12 SMTP INBOUND: "334 VXNlcm5hbWU6"
2021-11-12 03:56:12 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-11-12 03:56:12 CLIENT -> SERVER: cGV0ZXJuYW5qaW5nQGZveG1haWwuY29t
2021-11-12 03:56:12 SMTP INBOUND: "334 UGFzc3dvcmQ6"
2021-11-12 03:56:12 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-11-12 03:56:12 CLIENT -> SERVER: MjBKaWFuZzE4TmluZw==
2021-11-12 03:56:12 SMTP INBOUND: "535 Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256"
2021-11-12 03:56:12 SERVER -> CLIENT: 535 Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/h...28&&no=1001256
2021-11-12 03:56:12 SMTP ERROR: Password command failed: 535 Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/h...28&&no=1001256
SMTP Error: Could not authenticate.
2021-11-12 03:56:12 CLIENT -> SERVER: QUIT
2021-11-12 03:56:12 SMTP INBOUND: "221 Bye."
2021-11-12 03:56:12 SERVER -> CLIENT: 221 Bye.
2021-11-12 03:56:12 Connection: closed
SMTP Error: Could not authenticate.
Message could not be sent.Mailer Error: SMTP Error: Could not authenticate.
 
Old 11-11-2021, 10:21 PM   #10
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Fixed it, there were 2 problems:

1. I used a fictitious "From" email address, got the error: email must be the same as authorization user.

2. I changed the plain login password to my imap password, then it worked.

Thanks for your help!!

Last edited by Pedroski; 11-11-2021 at 10:22 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Cloud 5: Dispelling cloud myths, the cloud security excuse, and the fight for cloud supremacy LXer Syndicated Linux News 0 06-20-2014 04:12 PM
LXer: Cloud 5: NSA not killing cloud, cloud IT jobs, rise of cloud brokers LXer Syndicated Linux News 0 03-02-2014 09:51 AM
LXer: Cloud 5: Netflix's cloud-connected brain, 5 cloud myths and from cloud to fog LXer Syndicated Linux News 0 02-21-2014 02:20 PM
LXer: It's a cloud, cloud, cloud, cloud world LXer Syndicated Linux News 0 07-23-2013 05:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration