wiki:Docs/825gen2/Dev/Networking/SendEmail

Version 25 (modified by Don Wilson, 2 months ago) ( diff )

--

Sending Email

The 825 Gen2 can send emails.

The sendmail and msmtp commands are included in the 825 Gen2 Linux operating system.

Create the msmtp config file

nano ~/.msmtprc

Type the email account information such as:

defaults
tls on

account gmail
auth on
host smtp.gmail.com
port 587
user wilsonwareapps@gmail.com
from wilsonwareapps@gmail.com

account default : gmail

Save the configuration file.

Google no longer allows applications to use regular Google login password. To send email using a Google account create an app specific password in the Google web portal for the account to be used.

Keep a record of the generated password

ueut jrqv eapo eaid

Create an email as a text file.

nano message.txt

Type the email such as:

From: wilsonwareapps@gmail.com
To: dwilson@cardet.com
Subject: Test email from 825

Test 825ARM Variscite

123456
abc

Send the email

msmtp -t < message.txt
password for wilsonwareapps@gmail.com at smtp.gmail.com: 

Enter the app password. Email was sent successfully.

How to store password to not require password entry every time email is sent?

Password can be added to config file such as:

nano ~/.msmtprc

Add the password line with the previously obtained app specific password from Google.

defaults
tls on

account gmail
auth on
host smtp.gmail.com
port 587
user wilsonwareapps@gmail.com
from wilsonwareapps@gmail.com
password ueut jrqv eapo eaid

account default : gmail

Do the send again:

root@imx8mq-var-dart:~# msmtp -t < message.txt

The Email sent successfully with no prompt for password.

This could be done from app code using a system call.

How to send file attachments?

msmtp does not have support for file attachments directly. The message must be encoded into mime format.

Update message.txt to add MIME encoding

nano message.txt
From: wilsonwareapps@gmail.com
To: dwilson@cardet.com
Subject: Test email from 825
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MyBoundaryString"


--MyBoundaryString
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

This is a message.

--MyBoundaryString
Content-Type: text/plain; file="file123.txt"
Content-Disposition: attachment; filename="file123.txt"
Content-Transfer-Encoding: base64

Create attachment file

nano attachment.txt
This is an attachment
123456
abcdefg

Use base64 to convert the attachment to base64 encoding:

base64 attachment.txt > /tmp/attachment

Now pipe both files to the msmtp command

cat message.txt /tmp/attachment | msmtp -t

This successfully sends email with attachment

To send an image attachment modify the message.txt

From: wilsonwareapps@gmail.com
To: dwilson@cardet.com
Subject: Test email from 825
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MyBoundaryString"


--MyBoundaryString
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Test 12345
ABCD

--MyBoundaryString
Content-Type: image/jpeg
Content-ID: <image11>
Content-Transfer-Encoding: base64

Note that there has to be a blank line at the end of the file

Encode the jpeg image into base64 such as:

base64 /usr/share/apache2/default-site/htdocs/images/v000011_1.jpg /tmp/attachment

Send the email.

cat message.txt /tmp/attachment | msmtp -t

Example of sending an email from an app:

void TestSendEmail(const char* from, const char* to, const char* subject)
{
	FILE* file = fopen("/tmp/emailmsg.txt", "w");
	if(file != NULL) {
		fprintf(file, "From: %s\n", from);
		fprintf(file, "To: %s\n", to);
		fprintf(file, "Subject: %s\n", subject);
		fprintf(file, "MIME-Version: 1.0\n");
		fprintf(file, "Content-Type: multipart/mixed; boundary=\"MyBoundaryString\"");
		fprintf(file, "\n");
		fprintf(file, "\n");
		fprintf(file, "--MyBoundaryString\n");
		fprintf(file, "Content-Type: text/plain\n");
		fprintf(file, "Content-Transfer-Encoding: 7bit\n");
		fprintf(file, "\n");
		fprintf(file, "\n");
		fprintf(file, "Transaction %d\n", tran.tktNum);
		fprintf(file, "\n");
		fprintf(file, "Out: %s %s\n", tran.date.c_str(), tran.time.c_str());
		fprintf(file, "In: %s %s\n", tran.inDate.c_str(), tran.inTime.c_str());
		fprintf(file, "\n");
		fprintf(file, "Truck: %s\n", tran.id[DB_TRUCK].c_str());
		fprintf(file, "Product: %s\n", tran.id[DB_PROD].c_str());
		fprintf(file, "Customer: %s\n", tran.id[DB_CUST].c_str());
		fprintf(file, "Job: %s\n", tran.id[DB_JOB].c_str());
		fprintf(file, "\n");
		fprintf(file, "Gross: %s\n", tran.gross.c_str());
		fprintf(file, "Tare: %s\n", tran.tare.c_str());
		fprintf(file, "Net: %s\n", tran.net.c_str());
		fprintf(file, "\n");
		fprintf(file, "--MyBoundaryString\n");
		fprintf(file, "Content-Type: image/jpeg\n");
		fprintf(file, "Content-ID: <image11>\n");
		fprintf(file, "Content-Transfer-Encoding: base64\n");
		fprintf(file, "\n");

		fclose(file);

		// Convert second pass image into base64
		char tmp[128];
		sprintf_s(tmp, "/usr/share/apache2/default-site/htdocs/images/v%06d_1.jpg", tran.tktNum);
		if(base64_file_encode(tmp, "/tmp/attachment") <= 0) {
			DEBUG_MSG("Error encoding jpg into base64 [%s]\n", tmp);
		} else {
			// Send it
			if(system("cat /tmp/emailmsg.txt /tmp/attachment | msmtp -t") != 0) {
				DEBUG_MSG("Error sending\n");
			}

		}
	}
}

When transaction is completed app can call this function such as:

	char msg[80];
	sprintf_s(msg, "ID Storage transaction %d", tran.tktNum);
	TestSendEmail("myaccount@gmail.com", "someone@gmail.com", msg);

We may create or find a library to generate mime encoded emails with html formatting and more features.

Attachments (2)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.