Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Sep 11, 2012

Sending e-mail via PHP and MSMTP

Normally I would have set up a fully-fledged mailing systems like Postfix or Sendmail to handle my mailing needs. This means spending hours configuring, tweaking and securing another service running on my server. But I have found a simpler way.

MSMTP.

  1. Install MSMTP. In Ubuntu you would do the following:

    sudo apt-get install msmtp ca-certificates
  2. Edit the configuration file /etc/msmtprc with the following:

    #set defaults
    defaults

    # Enable or disable TLS/SSL encryption
    tls off
    tls_starttls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt

    # account settings
    account default
    host mail.optusnet.com.au # CHANGE THIS!!!
    port 25
    auth off
    from do-not-reply@domain.com.au
    logfile /var/log/msmtp/msmtp.log
  3. Edit the configuration of PHP to use MSMTP. Edit the configuration file with the following (in Ubuntu and PHP5, the file is stored in /etc/php5/cli/php.ini

    sendmail_path = /usr/bin/msmtp -t
  4. Create the log file directory and set proper permissions (depends on how your machine is set up:

    sudo mkdir /var/log/msmtp
    sudo chown [user]:[group] /var/log/msmtp
  5. Tell our system to rotate the logs so that they do not get too large by creating the file /etc/logrotate.d/msmtp:
    /var/log/msmtp/*.log {
    rotate 12
    monthly
    compress
    missingok
    notifempty

    }
  6. You can now test it with the following PHP script:

    <?php
    $to = "your-email@domain.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    if (mail($to,$subject,$message)) {
        echo "Mail Sent.";
    } else {
        echo "NOT SENT.";
    }

References:

Aug 5, 2012

Email validation through MySQL triggers and signals

Did some research and found out that MySQL provides triggers and signals. Triggers allow you to run some code when an certain event occurs, and Signals allow you to raise exceptions in your code.

Here is a SQL script to show you how it is done:

CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

USE `test`;

CREATE TABLE IF NOT EXISTS `test`.`entity_email` (
    `emailID` INT NOT NULL,
    `email` VARCHAR(64) NOT NULL,
    PRIMARY KEY (`emailID`) )
ENGINE = InnoDB
COMMENT = "Generic Email table";

DELIMITER $$

USE `test`$$
CREATE TRIGGER `trg_entity_email_insert` BEFORE INSERT ON `test`.`entity_email` FOR EACH ROW

BEGIN
    IF NOT (SELECT NEW.email REGEXP '$[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$') THEN
        -- bad data
        SIGNAL SQLSTATE '40000';
    END IF;
END$$
CREATE TRIGGER `trg_entity_email_update` BEFORE UPDATE ON `test`.`entity_email` FOR EACH ROW

BEGIN
    IF NOT (SELECT NEW.email REGEXP '$[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$') THEN
        -- bad data
        SIGNAL SQLSTATE '40000';
    END IF;
END$$


DELIMITER ;

Now MySQL will perform e-mail validation EVERY time you insert or edit the record. If it doesn't match, it will fail (MySQL outputs an "Unhandled user-defined exception condition" as the error message).

Hope this helps!