From charlesreid1

(Created page with "Sending email with PHP: https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server#14456761 Procedure: * Open port 465 * Put PHPMailer on your P...")
 
 
(8 intermediate revisions by the same user not shown)
Line 9: Line 9:
* Drop in your SMTP credentials
* Drop in your SMTP credentials
* Now you're able to send off emails
* Now you're able to send off emails
==Namecheap SMTP Mail Settings==


Namecheap:
Namecheap:
Line 18: Line 20:
* Incoming server (POP3): 995 port for SSL
* Incoming server (POP3): 995 port for SSL
* Outgoing server (SMTP): 465 port for SSL
* Outgoing server (SMTP): 465 port for SSL
==Installing PHPMailer==
Visit PHPMailer: https://github.com/PHPMailer/PHPMailer
Copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration


==Opening Port==
==Opening Port==


The following iptables rule will keep track of outgoing packets and will allow corresponding return packets - necessary for SMTP connections, because you want to be able to receive the "okay, data received" signal from the server you're sending mail out to.
<pre>
iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
</pre>
See [https://unix.stackexchange.com/questions/104954/how-to-allow-outgoing-smtp-on-iptables-debian-linux#104957]
==Editing php.ini==
No smtp settings needed in the php.ini file...
See [https://stackoverflow.com/questions/17726195/phpmailer-php-ini-configurations]
Do need to enable PHP OpenSSL support though: in php.ini, add
<pre>
[PHP_OPENSSL]
extension=php_openssl.dll
</pre>
See [https://stackoverflow.com/questions/11318578/sending-an-email-using-phpmailer-and-gmail-smtp]
==Editing PHPMailer Settings==
Need to edit the file class.smtp.php and put your SMTP credentials there. (I think.)
==Using PHPMailer==


See https://github.com/PHPMailer/PHPMailer


<pre>
<pre>
Line 39: Line 76:
// Set SMTP credentials
// Set SMTP credentials


$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';


$mail->Host      = "mail.example.com"; // SMTP server example
$mail = new PHPMailer(true);                             // Passing `true` enables exceptions
$mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
try {
$mail->SMTPAuth  = true;                 // enable SMTP authentication
    //Server settings
$mail->Port      = 25;                   // set the SMTP port for the GMAIL server
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
$mail->Username   = "username"; // SMTP account username example
    $mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Password   = "password";       // SMTP account password example
    $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.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}


?>
?>
</pre>
</pre>
==Don't Send to Gmail==
If you are experiencing problems with PHPMailer, and you are sending emails to a gmail acount, try sending your emails to a non-gmail account.
I was trying all kinds of different things in the script above and nothing was working, but then I got the bright idea to send to a non-gmail email address, and everything worked like a charm.
The PHPMailer emails were simply being dropped by Gmail, without any notification. They did not even show up in my spam folder.
My ultimate solution: send the emails to a non-gmail account (an email service associated with my domain but run by NameCheap). If you need to send them to a gmail account, set up email forwarding from the non-gmail NameCheap account to your gmail account.
=Flags=
[[Category:PHP]]
[[Category:Mail Server]]
[[Category:Deployment]]

Latest revision as of 05:59, 29 October 2017

Sending email with PHP:

https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server#14456761

Procedure:

  • Open port 465
  • Put PHPMailer on your PHP include path
  • From the PHP script you want to send email from, include PHPMailer
  • Drop in your SMTP credentials
  • Now you're able to send off emails

Namecheap SMTP Mail Settings

Namecheap:

  • Username: your email address
  • Password: password for this email account
  • Incoming/outgoing servers name: mail.privateemail.com
  • Incoming server type: IMAP or POP3
  • Incoming server (IMAP): 993 port for SSL
  • Incoming server (POP3): 995 port for SSL
  • Outgoing server (SMTP): 465 port for SSL

Installing PHPMailer

Visit PHPMailer: https://github.com/PHPMailer/PHPMailer

Copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration

Opening Port

The following iptables rule will keep track of outgoing packets and will allow corresponding return packets - necessary for SMTP connections, because you want to be able to receive the "okay, data received" signal from the server you're sending mail out to.

iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

See [1]

Editing php.ini

No smtp settings needed in the php.ini file...

See [2]

Do need to enable PHP OpenSSL support though: in php.ini, add

[PHP_OPENSSL]
extension=php_openssl.dll

See [3]

Editing PHPMailer Settings

Need to edit the file class.smtp.php and put your SMTP credentials there. (I think.)

Using PHPMailer

See https://github.com/PHPMailer/PHPMailer

<?php

//////////////////////////////////////////////////////////////
// Include PHP mailer stuff

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

//////////////////////////////////////////////////////////////
// Set SMTP credentials


$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.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

?>

Don't Send to Gmail

If you are experiencing problems with PHPMailer, and you are sending emails to a gmail acount, try sending your emails to a non-gmail account.

I was trying all kinds of different things in the script above and nothing was working, but then I got the bright idea to send to a non-gmail email address, and everything worked like a charm.

The PHPMailer emails were simply being dropped by Gmail, without any notification. They did not even show up in my spam folder.

My ultimate solution: send the emails to a non-gmail account (an email service associated with my domain but run by NameCheap). If you need to send them to a gmail account, set up email forwarding from the non-gmail NameCheap account to your gmail account.

Flags