Trying to understand the php mail() function and phpMailer library

Sorry I know this is basic, but I’m having a little trouble understanding how the whole email thing works.

Let’s just say I have an order-now page on my website. I have a working PHP form where a person can select the products they want, type in their contact information and hit submit. I receive the emails fine with the php mail() function. What I need to know is how do I send a copy of the email back to the person who placed the order?

The mail() function cannot work if the customer has a gmail account. And I need the customer to get a copy of the email no matter which email server they’re using…

I looked it up and all I can come up with is the php Mailer library where it’s possible to send FROM Gmail. But that’s not my problem - I already bought an email domain and I have no trouble sending emails from my web hosting server.

I don’t mind if you need some other language or library to do this, as long as I can get the email confirmation sent to the client automatically.

There aren’t tutorials or anything on the web with the keywords I’m using, so I’m a little stuck right now…

My test-website is currently live at www.unidrones.co.za/ChoiceGelatin/order.php if you need to have a look at it. I receive the emails fine, but I have absolutely no clue how to send the email back to the customer :frowning:

Another question on the same topic, how do websites send such pretty promotion emails? According to what I heard, you have to style emails with inline CSS…Is this what every website does, or did I miss something?

Thanks so much in advance guys, I know this is a much too basic question but I simply can’t find the answer on my own :sob:

The answer to your First Question (PHP Mailer)

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

Above code is the official demo code from the repo. It is well commented and easy to follow. Please try to read it and let me know if you still stuck and have questions about it.

The answer to your second Questions (Pretty Promotion Emails)

Yes, all those emails must be styled with in-line css since we cannot attach any css files to it.

Most common thing that today companies do is that they buy the services from like Mailchimp to make these campaign promotions easier.

1 Like

Thank you for the response :slight_smile:
I’m trying it out now and will let you know when I reach the next stage

Okay, this is the first time I’m trying phpMailer… And there are some errors. Firstly I downloaded several files, one was called class.phpmailer.php, but none of them had the phpMailerAutoload.php file … So I had to copy paste the contents of one of the files online and create it on my PC. Here’s my code, and as I’d expect it isn’t working… I feel like there’s something important I’m missing. What do I need to do to get it to work?

<?php

require('phpMailerAutoload.php');

 // $mail = new PHPMailer;
    $mail = new PHPMailer(true); 

    $name = $_POST['name'];
    $msg = $_POST['msg'];

  //Creating the email body to be sent
  $email_body = "";
  $email_body .= "Name: " . $name . "\n";
  $email_body .= "Email: " . $msg . "\n";

try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'unidrones.co.za;www.unidrones.co.za';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'info@unidrones.co.za';                 // SMTP username
    $mail->Password = ''; //'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465; //587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('info@unidrones.co.za', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@unidrones.co.za', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}