smtp snippets

How to send both HTML and text emails with PHP and PHPMailer

Tagged send, email, smtp, php, phpmailer, html  Languages php

This is an example on how to use PHPMailer to send an HTML email. It also shows how to include a text only version of the same email for clients, such as mutt, that don't support HTML emails.

$mail = new PHPMailer();

$mail->IsHTML(true);
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->IsSMTP();

$mail->WordWrap = 80;
$mail->Host = "smtp.thehost.com"; 
$mail->SMTPAuth = false;

$mail->From = $from;
$mail->FromName = $from; // First name, last name
$mail->AddAddress($to, "First name last name");
#$mail->AddReplyTo("reply@thehost.com", "Reply to address");

$mail->Subject =  $subject;
$mail->Body =  $htmlMessage; 
$mail->AltBody  =  $textMessage;    # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt.

if(!$mail->Send())
{
  throw new Exception("Mailer Error: " . $mail->ErrorInfo);
}

Sending email with Linux CLI through SMTP.

Tagged email, smtp, cli scripting  Languages bash

Great for scripting e.g motion to send the captured images + movie into your email. No public ssh keys, no passwords, no hassles. Install on Debian with apt-get install sendemail. The email address below has naturally been altered to not be picked up by harvesters.

sendEmail -f marko_haapala@aktagon_com -t marko_haapala@aktagon_com -m "$(date -I)" -u "Possible intruder" -s smtp.aktagon.com -a /tmp/motion/file.jpg

How to send emails from the command line in Linux

Tagged mail, postfix, smtp  Languages 

Use the mail command...

Specify the sender with the -r switch

mail -s "Yer subject" devil@devil.com -- -r "devil@devil.com"

Use echo to send your emails

echo -e "TEST\n.\n" | mail -s "subject" devil@devil.com -- -r "devil@devil.com"

Note that we use the -e switch to preserve new lines.

Use a file as the contents of your email

mail -s "subject" devil@devil.com -- -r "devil@devil.com" < /tmp/email