your own mail server

View previous topic View next topic Go down

your own mail server

Post by jamied_uk on 2nd September 2010, 16:07





Trip-Wire
youtube.com/user/Jamieduk999/search?query=TRIPWIRE


Last edited by jamied_uk on 27th September 2016, 23:35; edited 2 times in total
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by jamied_uk on 2nd September 2010, 16:15

Code:
ssh pi@192.168.0.222

(ip or hostname)


Code:
sudo apt-get update -y && sudo apt-get upgrade -y
Code:
sudo su

apt-get install -y sendmail-bin sensible-mda php sendmail mutt locate php

Code:
sudo nano /etc/mail.rc



to add the following lines :


Code:
set from=yourname@gmail.com


set smtp=smtp.gmail.com


set smtp-auth-user=yourname


set smtp-auth-password=yourpasswd


set smtp-auth=login




Code:
nano /etc/mail.rc



~~~~~~~~~~~~~~~~

Config php for mail

Code:
sudo nano /etc/php5/apache2/php.ini


add path to above .ini file

Code:
sendmail_path = /usr/sbin/sendmail -t -i -F"J~Net" -f'youroutgoingemail@gmail.com'


~~~~~~~~~~~~~~~~~~~~

Testing email with miutt for sending pics:

Code:
echo | mutt -a /usr/share/scratch/driveway.jpg -s "Photo" -- someuser@hotmail.com




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create a file using following content.


Code:
nano /tmp/email.txt




Code:
Subject: Terminal Email Send





Email Content line 1


Email Content line 2





Subject: line will be used as subject for email.





Code:
cat /tmp/email.txt



Code:
sendmail user@example.com  < /tmp/email.txt



Or:

Code:
sudo sendmail -s "Test Email" email@hotmail.com < /dev/null



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Send email with attachment:

To send email with attachment.

Code:
sendmail -a /opt/backup.sql -s "Backup File" user@example.com < /dev/null



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Testing mutt

Code:
mutt -s "Test Email" user@example.com < /dev/null



Code:
sudo mutt -s "Test Email" test_email@hotmail.com < /dev/null


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Rebooting rpi (via ssh or cli):

Code:
sudo reboot


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


ssmtp admin@example.com
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
^d

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


mc file edits for relays


Code:
sudo nano /etc/mail/sendmail.mc





; 587





Code:
define(`RELAY_MAILER_ARGS', `TCP $h 465')





define(`ESMTP_MAILER_ARGS’, `TCP $h 465′)





More:

https://encrypted.google.com/search?q=send+test+email+with+sendmail+linux+bash

https://stackoverflow.com/questions/7800492/setting-up-a-ubuntu-apache-php-machine-to-send-email



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More



Code:
sudo nano /etc/apache2/apache2.conf



sendmail path = "/usr/sbin/sendmail -t -i"



Code:



sudo mutt -s "Test Email" test@hotmail.com < /dev/null





sudo sendmail -s "Test Email" test@hotmail.com < /dev/null






~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for hosted or gmail smtp:


Code:
sudo nano /etc/ssmtp/ssmtp.conf



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



How to replace Sendmail with Gmail smtp on Ubuntu server

Contents [hide]

    1 Open a Gmail account
    2 Install MSMTP
    3 Configure MSMTP
    4 Test MSMTP
    5 Troubleshooting MSMTP
    6 Configuring Apache\PHP to use MSMTP
    7 Reference

Today my goal was to be able to send mails from PHP from my web-server using Gmail smtp.

    My server runs on Apache with vhosts: I need a separate mailbox for every domain.
    I don’ want to manage a MTA because it’s resource and time expensive, so I want to send mails trough a Gmail account.
    I want the system to be fast and easy to maintain.

The standard packages such as postfix, sendmail, qmail etc doesn’t meet the last two requirements, so I had to choose an alternative to postfix: MSMTP

Open a Gmail account

My suggestion is to not use your personal Gmail account, so probably will be a good idea to open a brand new one dedicated to the server.
Install MSMTP

That’s an easy task: use apt-get to achieve it.

Code:
sudo apt-get install -y msmtp ca-certificates


With this command we have installed also some authority certificates useful to verify SSL connection.
Configure MSMTP

The default config files for MSMTP are located in SYSCONFDIR/msmtprc and ~/.msmtprc.
Neither the first or the second was created by apt, so I preferred to have a folder in /etc where store more than one config file if needed.
Same for the logs.

Code:



sudo mkdir /etc/msmtp


sudo mkdir /var/log/msmtp



Then I’ve created the config file: citex0 is the name of my server, change it.


Code:
sudo touch /etc/msmtp/citex0


sudo nano /etc/msmtp/citex0



this is what’s inside: remeber to change your mailbox and password

Code:



# Define here some setting that can be useful for every account


defaults


        logfile /var/log/msmtp/general.log





# Settings for citex0 account


account citex0


        protocol smtp


        host smtp.gmail.com


        tls on


        tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt


        port 587


        auth login


        user my_server_account@gmail.com


        password my_server_password


        from my_server_account@gmail.com


        logfile /var/log/msmtp/citex0.log





# If you don't use any "-a" parameter in your command line,


# the default account "citex0" will be used.


account default: citex0



Test MSMTP

change the your_personal_mail@gmail.com with your own recipient, the file path if needed and run:

Code:
echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" | msmtp --debug --from=default -t your_personal_mail@gmail.com --file=/etc/msmtp/citex0


Code:

<?php

/* change to email address(s) */

//

$headers = 'From: youroutgoingmail@gmail.com';



mail('to_mail@hotmail.com', 'Test email using PHP', 'This is a test email message', $headers, '-fwebmaster@example.com');



?>

save file as test.php and run via webpage to test it!


Troubleshooting MSMTP

Probably the first time you’ll try to send a mail to Google’s smtp from a server’s ip, BigG will deactivate your account.
This is the result on the log or command line output:

<-- 535-5.7.1 Please log in with your web browser and then try again. Learn more at
<-- 535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 b49sm36363995eem.16
msmtp: authentication failed (method LOGIN)
msmtp: server message: 535-5.7.1 Please log in with your web browser and then try again. Learn more at
msmtp: server message: 535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 b49sm36363995eem.16
msmtp: could not send mail (account default from /etc/msmtp/citex0)

To avoid this you only need to verify your Google Account by inserting your phone.
Simply try to login with your server account and you'll be prompted for your phone number.
Insert the right one and they will send you a code to unlock your server account.

This is what happens when everything is alright!

<-- 235 2.7.0 Accepted
--> MAIL FROM:
<-- 250 2.1.0 OK e2sm36547326eeo.8
--> RCPT TO:
<-- 250 2.1.5 OK e2sm36547326eeo.8
--> DATA
<-- 354  Go ahead e2sm36547326eeo.8
--> Subject: Test Mail
-->
--> This is a test mail
--> .
<-- 250 2.0.0 OK 1355059058 e2sm36547326eeo.8
--> QUIT
<-- 221 2.0.0 closing connection e2sm36547326eeo.8

Anand Rai told me there is a way to do it without entering a captcha or password using web browser.

    Allow low security app login from the Google Account Security Panel
    Allow device from your machine from here, After this gmail will allow your server to send email.
    Also check the activity panel just to be sure the server is listed

I suggest to not use this on production server for the security implications coming from enabling the "less secure apps". BTW thanks Anand!
Configuring Apache\PHP to use MSMTP

To force PHP to use MSMTP when called by Apache you have to set the environment variable sendmail_path.

php_admin_value sendmail_path "/usr/bin/msmtp --debug --from=citex0 --file=/etc/msmtp/citex0 -t"


;sendmail_path "/usr/bin/msmtp --debug --from=citex0 --file=/etc/msmtp/citex0 -t"

sendmail_path = "/usr/bin/msmtp --debug --from=citex0 --file=/etc/msmtp/citex0 -t"


At this point I had to fix some permission and owners


Code:
sudo chmod 600 /etc/msmtp/citex0


sudo chown -R www-data:www-data /var/log/msmtp


sudo chown -R www-data:www-data /etc/msmtp/




More Links:

https://raspberrypi.stackexchange.com/questions/12405/how-to-set-up-smtp-and-send-emails


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

More
Raspberry Pi Send Mail

iqjar.com/jar/sending-emails-from-the-raspberry-pi



Alternatives

http://www.smartertools.com/smartermail/free-mail-server.aspx


Last edited by jamied_uk on 8th May 2017, 11:00; edited 3 times in total
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by jamied_uk on 27th September 2016, 23:17

PHP Mail Test Function


Code:


<?php
function SendMail(){

/* change to email address(s) */

//

$headers = 'From: youremail@gmail.com';



mail('to_email@hotmail.com', 'Test email using PHP', 'This is a test email message', $headers, '-fwebmaster@example.com');

}



SendMail();




?>
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by jamied_uk on 27th September 2016, 23:24

Advanced Version


Code:
<?php

function SendMail($ip){

/* change to email address(s) */

//
$link="https://www.google.co.uk/#safe=off&q=whois+$ip";

$headers = 'From: youremail@gmail.com';

$msg="Unauthorized User IP Is $ip";

mail('to_email@hotmail.com', 'J~Net Email Trip-Wire Alarm', $msg, $headers, '-fwebmaster@example.com');

}



SendMail($ip);




?>
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by jamied_uk on 8th May 2017, 11:04

Restarting sendmail

Code:
/etc/init.d/sendmail restart


php5 is out so if you cant find php.ini file try looking in /etc/php/7.0/apache2/
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by jamied_uk on 8th May 2017, 11:32

For me I find sendmail to fail and mutt to work so heres mutt:





More Found @
linux.com/blog/setup-mutt-gmail-centos-and-ubuntu
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: your own mail server

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum