Linux - Server This forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
11-08-2021, 10:23 PM
#1
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Rep:
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);
?>
11-09-2021, 04:19 AM
#2
Moderator
Registered: Aug 2002
Posts: 25,702
11-09-2021, 05:16 PM
#3
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Original Poster
Rep:
@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 .
11-09-2021, 06:03 PM
#4
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Original Poster
Rep:
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?
11-09-2021, 06:17 PM
#5
Moderator
Registered: Aug 2002
Posts: 25,702
11-11-2021, 01:56 AM
#6
LQ Addict
Registered: Dec 2013
Posts: 19,872
Pedroski, I use
msmtp for this.
11-11-2021, 03:22 PM
#7
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Original Poster
Rep:
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:
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;
}
?>
11-11-2021, 10:05 PM
#9
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Original Poster
Rep:
@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.
11-11-2021, 10:21 PM
#10
Senior Member
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116
Original Poster
Rep:
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 .
All times are GMT -5. The time now is 09:21 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News