Sort out namespaces and imports in examples

This commit is contained in:
Marcus 2016-04-22 19:13:15 +02:00
parent baf343dab3
commit f0ed47850c
15 changed files with 94 additions and 53 deletions

View File

@ -5,7 +5,9 @@
* by matt.sturdy@gmail.com
*/
namespace PHPMailer\PHPMailer;
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
@ -42,7 +44,8 @@ $results_messages = [];
// $example_code represents the "final code" that we're using, and will
// be shown to the user at the end.
$example_code = "<?php\nnamespace PHPMailer\\PHPMailer;";
$example_code = "<?php\nuse PHPMailer\\PHPMailer\\PHPMailer;";
$example_code .= "\nuse PHPMailer\\PHPMailer\\Exception;";
$example_code .= "\nrequire_once 'vendor/autoload.php';";
$example_code .= "\n\n\$results_messages = [];";
@ -53,23 +56,18 @@ $example_code .= "\n\n\$mail = new PHPMailer(true);";
$example_code .= "\n\$mail->CharSet = 'utf-8';";
$example_code .= "\nini_set('default_charset', 'UTF-8');";
class AppException extends Exception
{
}
$example_code .= "\n\nclass AppException extends Exception {}";
$example_code .= "\n\ntry {";
try {
if (isset($_POST["submit"]) && $_POST['submit'] == "Submit") {
$to = $_POST['To_Email'];
if (!PHPMailer::validateAddress($to)) {
throw new AppException("Email address " . $to . " is invalid -- aborting!");
throw new Exception("Email address " . $to . " is invalid -- aborting!");
}
$example_code .= "\n\$to = '{$_POST['To_Email']}';";
$example_code .= "\nif(!PHPMailer::validateAddress(\$to)) {";
$example_code .= "\n throw new AppException(\"Email address \" . " .
$example_code .= "\n throw new Exception(\"Email address \" . " .
"\$to . \" is invalid -- aborting!\");";
$example_code .= "\n}";
@ -115,7 +113,7 @@ try {
$example_code .= "\n\$mail->isQmail();";
break;
default:
throw new AppException('Invalid test_type provided');
throw new Exception('Invalid test_type provided');
}
try {
@ -160,7 +158,7 @@ try {
}
}
} catch (Exception $e) { //Catch all kinds of bad addressing
throw new AppException($e->getMessage());
throw new Exception($e->getMessage());
}
$mail->Subject = $_POST['Subject'] . ' (PHPMailer test using ' . strtoupper($_POST['test_type']) . ')';
$example_code .= "\n\$mail->Subject = \"" . $_POST['Subject'] .
@ -191,22 +189,22 @@ try {
$example_code .= "\n \$results_messages[] = \"Message has been sent using " .
strtoupper($_POST['test_type']) . "\";";
$example_code .= "\n}";
$example_code .= "\ncatch (phpmailerException \$e) {";
$example_code .= "\n throw new AppException('Unable to send to: ' . \$to. ': '.\$e->getMessage());";
$example_code .= "\ncatch (Exception \$e) {";
$example_code .= "\n throw new \\Exception('Unable to send to: ' . \$to. ': '.\$e->getMessage());";
$example_code .= "\n}";
try {
$mail->send();
$results_messages[] = "Message has been sent using " . strtoupper($_POST["test_type"]);
} catch (Exception $e) {
throw new AppException("Unable to send to: " . $to . ': ' . $e->getMessage());
throw new Exception("Unable to send to: " . $to . ': ' . $e->getMessage());
}
}
} catch (AppException $e) {
} catch (Exception $e) {
$results_messages[] = $e->errorMessage();
}
$example_code .= "\n}";
$example_code .= "\ncatch (AppException \$e) {";
$example_code .= "\ncatch (Exception \$e) {";
$example_code .= "\n \$results_messages[] = \$e->errorMessage();";
$example_code .= "\n}";
$example_code .= "\n\nif (count(\$results_messages) > 0) {";

View File

@ -2,7 +2,10 @@
/**
* This example shows how to make use of PHPMailer's exceptions for error handling.
*/
namespace PHPMailer\PHPMailer;
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
@ -28,9 +31,9 @@ try {
//send the message
//Note that we don't need check the response from this because it will throw an exception if it has trouble
$mail->send();
echo "Message sent!";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo 'Message sent!';
} catch (Exception $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (\Exception $e) { //The leading slash means the Global PHP Exception class will be caught
echo $e->getMessage(); //Boring error messages from anything else!
}

View File

@ -1,6 +1,8 @@
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
*/
namespace PHPMailer\PHPMailer;

View File

@ -3,7 +3,9 @@
* This example shows how to send via Google's Gmail servers using XOAUTH2 authentication.
*/
namespace PHPMailer\PHPMailer;
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
// Alias the League Google OAuth2 provider class
use League\OAuth2\Client\Provider\Google;

View File

@ -2,7 +2,9 @@
/**
* This example shows sending a message using PHP's mail() function.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';

View File

@ -1,5 +1,10 @@
<?php
namespace PHPMailer\PHPMailer;
/**
* This example shows how to send a message to a whole list of recipients efficiently.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
error_reporting(E_STRICT | E_ALL);

View File

@ -2,7 +2,11 @@
/**
* This example shows how to use POP-before-SMTP for authentication.
*/
namespace PHPMailer\PHPMailer;
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\POP3;
require '../vendor/autoload.php';
@ -22,7 +26,7 @@ try {
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
$mail->Host = 'mail.example.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
@ -45,9 +49,9 @@ try {
//send the message
//Note that we don't need check the response from this because it will throw an exception if it has trouble
$mail->send();
echo "Message sent!";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo 'Message sent!';
} catch (Exception $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (\Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}

View File

@ -1,8 +1,13 @@
<?php
/**
* PHPMailer simple file upload and send example
* PHPMailer simple file upload and send example.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload

View File

@ -2,12 +2,15 @@
/**
* PHPMailer multiple files upload and send example
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// Create a message
// This should be somewhere in your include_path
require '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'First Last');
$mail->addAddress('whoto@example.com', 'John Doe');

View File

@ -2,7 +2,9 @@
/**
* This example shows sending a message using a local sendmail binary.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';

View File

@ -1,8 +1,7 @@
<?php
namespace PHPMailer\PHPMailer;
/**
* This example shows signing a message and then sending it via the mail() function of PHP.
*
*
* Before you can sign the mail certificates are needed.
*
*
@ -13,25 +12,25 @@ namespace PHPMailer\PHPMailer;
* The form is directly available via https://secure.comodo.com/products/frontpage?area=SecureEmailCertificate
* Fill it out and you'll get an email with a link to download your certificate.
* Usually the certificate will be directly installed into your browser (FireFox/Chrome).
*
*
*
* STEP 2 - Exporting the certificate
* This is specific to your browser, however, most browsers will give you the option to export your recently added certificate in PKCS12 (.pfx)
* Include your private key if you are asked for it.
* Set up a password to protect your exported file.
*
*
* STEP 3 - Splitting the .pfx into a private key and the certificate.
* I use openssl for this. You only need two commands. In my case the certificate file is called 'exported-cert.pfx'
* To create the private key do the following:
*
* openssl pkcs12 -in exported-cert.pfx -nocerts -out cert.key
*
*
* Of course the way you name your file (-out) is up to you.
* You will be asked for a password for the Import password. This is the password you just set while exporting the certificate into the pfx file.
* Afterwards, you can password protect your private key (recommended)
* Also make sure to set the permissions to a minimum level and suitable for your application.
* To create the certificate file use the following command:
*
*
* openssl pkcs12 -in exported-cert.pfx -clcerts -nokeys -out cert.crt
*
* Again, the way you name your certificate is up to you. You will be also asked for the Import Password.
@ -45,10 +44,13 @@ namespace PHPMailer\PHPMailer;
* STEP 3 - Code
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail = new PHPMailer;
//Set who the message is to be sent from
//IMPORTANT: This must match the email address of your certificate.
//Although the certificate will be valid, an error will be thrown since it cannot be verified that the sender and the signer are the same person.
@ -71,7 +73,9 @@ $mail->addAttachment('images/phpmailer_mini.png');
$mail->sign(
'/path/to/cert.crt', //The location of your certificate file
'/path/to/cert.key', //The location of your private key file
'yourSecretPrivateKeyPassword', //The password you protected your private key with (not the Import Password! may be empty but parameter must not be omitted!)
//The password you protected your private key with (not the Import Password!
//May be empty but the parameter must not be omitted!
'yourSecretPrivateKeyPassword',
'/path/to/certchain.pem' //The location of your chain file
);

View File

@ -2,7 +2,9 @@
/**
* This example shows making an SMTP connection with authentication.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
@ -20,15 +22,15 @@ $mail->isSMTP();
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
$mail->Host = 'mail.example.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "yourname@example.com";
$mail->Username = 'yourname@example.com';
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
$mail->Password = 'yourpassword';
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
@ -47,7 +49,7 @@ $mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Message sent!";
echo 'Message sent!';
}

View File

@ -3,7 +3,12 @@
* This uses the SMTP class alone to check that a connection can be made to an SMTP server,
* authenticate, then disconnect
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer SMTP class into the global namespace
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that

View File

@ -2,13 +2,15 @@
/**
* This example shows making an SMTP connection without using authentication.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require_once '../PHPMailerAutoload.php';
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
@ -20,7 +22,7 @@ $mail->isSMTP();
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
$mail->Host = 'mail.example.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//We don't need to set this as it's the default value

View File

@ -2,7 +2,9 @@
/**
* This example shows settings to use when sending over SMTP with TLS and custom connection options.
*/
namespace PHPMailer\PHPMailer;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that