i am trying to understand why my subject method in index.php triggers an error of not being defined.i am using phpmailer 5.2.7 with php 7.2 and wampserver 3.1.7
//here is my extended class from phpmailer//
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'xxxxxx#gmail.com';
public $FromName = MM;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'xxxxxxx#gmail.com';
public $Password = 'xxxxxx';
public $SMTPSecure = 'ssl';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
and here is part of my index page where i have defined my variables
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new PHPMailer(true);
$mail->setFrom(SITEEMAIL);
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
Firstly, why are you using a version of PHPMailer that's literally years out of date? Get the latest, which has new features, fixed bugs, and fewer security holes. While you're upgrading, consider switching to using composer to manage your dependencies.
The problem you're having is quite straightforward: you have created a subclass that adds the subject() method, but the instance you've created in your script is of the original PHPMailer class, not your subclass. Do this instead:
$mail = new Mail(true);
Naming your class with a very generic "Mail" name is very likely to bring you an unexpected lesson on why namespacing is a good idea, so I'd recommend adding a namespace for your app to avoid name clashes.
While it's a good idea to subclass like this to set default values easily, it's also inviting you to check in credentials to your source repo, which is usually a bad idea. Better to use your child class to read those values from an environment file ("dot env") using a package like this.
When I'm building and testing my website on local server, I would like to emulate successful sending via phpmailer, so I avoid actually sending emails.
I normally use if ($mail->Send()) { the mail was sent, now do this }.
For local testing I think the best would be to skip the whole phpmailer inclusion, instead of adding a lot of if statements etc.
But skipping phpmailer would then cause php to complain about $mail->Send(), $mail->addAddress('emailaddress') etc.
How could I fake the function (or object/class) so that calls to $mail->Send() are always true, and the rest $mail->something() etc. are just ignored/true, so that no email is sent?
Extend the PHPMailer class and override the public function send().
class UnitTestMailer extends PHPMailer {
public function send() {
return $this;
}
}
class User {
public function __construct(PHPMailer $mailer) {
$this->mailer = $mailer;
}
public function sendActiviation() {
return $this->mailer->send();
}
}
// ... somewhere in your test
public function test_if_from_is_properly_set() {
// ...
$user = new User(new UnitTestMailer);
// ...
$mailer = $user->sendActivation();
$this->assertEquals($expectedFrom, $mailer->From);
}
Why emulate?
I use INI files to provide configuration variables for PHPMailer depending on the environment. Live obviously has the server's mail settings. Local uses my Gmail account credentials to send mail.
You can have the best of both worlds :)
Keep a config.ini file for example somewhere in your working directory (I tend to use root but that's preference) and make sure it's in your .gitignore or similar. Your local version would look something like:
[PHPMailer Settings]
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_SMTPSECURE = "tls"
EMAIL_SMTPAUTH = "true"
EMAIL_USERNAME = "my_email#gmail.com"
EMAIL_PASSWORD = "my_password"
then in your PHP:
$ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . "/config.ini", true, INI_SCANNER_TYPED);
// use settings from INI file:
$foo = $ini['PHPMailer Settings']['EMAIL_HOST'];
$bar = $ini['PHPMailer Settings']['EMAIL_PORT'];
Security Bonus
Change the syntax of your INI file to look like the below and rename it to config.ini.php:
; <?php
; die();
; /*
[PHPMailer Settings]
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_SMTPSECURE = "tls"
EMAIL_SMTPAUTH = "true"
EMAIL_USERNAME = "my_email#gmail.com"
EMAIL_PASSWORD = "my_password"
; */ ?>
(remember to use the new filename in your PHP code)
PHP can still parse the settings, but if anyone tried to access the INI file it would be parsed as PHP comments and just show ";"
I'm writing an application with codeigniter, which is clearly done but I just want 1 more plugin in it. I would like to catch all the emails from my outlook account to my codeigniter app.
It would be awesome if I could send and receive messages on my codenigter app.
My second question is how can I api my agenda from my codeigniter app to the agenda from outlook?
Email works on some protocols for incoming (IMAP) and outgoing (SMTP) or similar like POP3 etc.
So you must have configured those settings in outlook to read your mails and send mails. Similarly, You can read mails in PHP and Send mails using php.
Sending Emails:
You can use codeigniter core email library which works well for outgoing. sending emails codeigniter
Reading Mails:
This script can read your mail by providing configuration which you provided to outlook.
<?php
class Email_reader {
// imap server connection
public $conn;
// inbox storage and inbox message count
private $inbox;
private $msg_cnt;
// email login credentials
private $server = 'YOUR_MAIL_SERVER';
private $user = 'email#mailprovider.com';
private $pass = 'yourpassword';
private $port = 143; // change according to server settings
// connect to the server and get the inbox emails
function __construct() {
$this->connect();
$this->inbox();
}
// close the server connection
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
imap_close($this->conn);
}
// open the server connection
// the imap_open function parameters will need to be changed for the particular server
// these are laid out to connect to a Dreamhost IMAP server
function connect() {
$this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
// move the message to a new folder
function move($msg_index, $folder='INBOX.Processed') {
// move on server
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
// re-read the inbox
$this->inbox();
}
// get a specific message (1 = first email, 2 = second email, etc.)
function get($msg_index=NULL) {
if (count($this->inbox) <= 0) {
return array();
}
elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index]))
{
return $this->inbox[$msg_index];
}
return $this->inbox[0];
}
// read the inbox
function inbox() {
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
for($i = 1; $i <= $this->msg_cnt; $i++) {
$in[] = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'body' => imap_body($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
}
$this->inbox = $in;
}
}
?>
Its a basic script to read mails, you can enhance it according to your requirement.
I have PHP's mail() using ssmtp which doesn't have a queue/spool, and is synchronous with AWS SES.
I heard I could use SwiftMail to provide a spool, but I couldn't work out a simple recipe to use it like I do currently with mail().
I want the least amount of code to provide asynchronous mail. I don't care if the email fails to send, but it would be nice to have a log.
Any simple tips or tricks? Short of running a full blown mail server? I was thinking a sendmail wrapper might be the answer but I couldn't work out nohup.
You have a lot of ways to do this, but handling thread is not necessarily the right choice.
register_shutdown_function: the shutdown function is called after the response is sent. It's not really asynchronous, but at least it won't slow down your request. Regarding the implementation, see the example.
Swift pool: using symfony, you can easily use the spool.
Queue: register the mails to be sent in a queue system (could be done with RabbitMQ, MySQL, redis or anything), then run a cron that consume the queue. Could be done with something as simple as a MySQL table with fields like from, to, message, sent (boolean set to true when you have sent the email).
Example with register_shutdown_function
<?php
class MailSpool
{
public static $mails = [];
public static function addMail($subject, $to, $message)
{
self::$mails[] = [ 'subject' => $subject, 'to' => $to, 'message' => $message ];
}
public static function send()
{
foreach(self::$mails as $mail) {
mail($mail['to'], $mail['subject'], $mail['message']);
}
}
}
//In your script you can call anywhere
MailSpool::addMail('Hello', 'contact#example.com', 'Hello from the spool');
register_shutdown_function('MailSpool::send');
exit(); // You need to call this to send the response immediately
php-fpm
You must run php-fpm for fastcgi_finish_request to be available.
echo "I get output instantly";
fastcgi_finish_request(); // Close and flush the connection.
sleep(10); // For illustrative purposes. Delete me.
mail("test#example.org", "lol", "Hi");
It's pretty easy queuing up any arbitrary code to processed after finishing the request to the user:
$post_processing = [];
/* your code */
$email = "test#example.org";
$subject = "lol";
$message = "Hi";
$post_processing[] = function() use ($email, $subject, $message) {
mail($email, $subject, $message);
};
echo "Stuff is going to happen.";
/* end */
fastcgi_finish_request();
foreach($post_processing as $function) {
$function();
}
Hipster background worker
Instantly time-out a curl and let the new request deal with it. I was doing this on shared hosts before it was cool. (it's never cool)
if(!empty($_POST)) {
sleep(10);
mail($_POST['email'], $_POST['subject'], $_POST['message']);
exit(); // Stop so we don't self DDOS.
}
$ch = curl_init("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'email' => 'noreply#example.org',
'subject' => 'foo',
'message' => 'bar'
]);
curl_exec($ch);
curl_close($ch);
echo "Expect an email in 10 seconds.";
Use AWS SES with PHPMailer.
This way is very fast (hundreds of messages per second), and there isn't much code required.
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-west-2.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'blah'; // SMTP username
$mail->Password = 'blahblah'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 443;
Not sure if i interpreted your question correctly but i hope this helps.
Pthreads is your friend :)
This is a sample of how i made in my production application
class AsynchMail extends Thread{
private $_mail_from;
private $_mail_to;
private $_subject;
public function __construct($subject, $mail_to, ...) {
$this->_subject = $subject;
$this->_mail_to = $mail_to;
// ...
}
// ...
// you must redefine run() method, and to execute it we must call start() method
public function run() {
// here put your mail() function
mail($this->_mail_to, ...);
}
}
TEST SCRIPT EXAMPLE
$mail_to_list = array('Shigeru.Miyamoto#nintendo.com', 'Eikichi.Kawasaki#neogeo.com',...);
foreach($mail_to_list as $mail_to) {
$asynchMail = new AsynchMail($mail_to);
$asynchMail->start();
}
Let me know if you need further help for installing and using thread in PHP
For logging system, i strongly advice you to use Log4PHP : powerful and easy to use and to configure
For sending mails, i also strongly advice you to use PHPMailer
I'm using asynchronous php execution by using beanstalkd.
It is a simple message queue, really lightweight and easy to integrate.
Using the following php wrapper for php https://github.com/pda/pheanstalk
you can do something as follows to implement a email worker:
use Beanstalk\Client;
$msg="dest_email##email_subject##from_email##email_body";
$beanstalk = new Client();
$beanstalk->connect();
$beanstalk->useTube('flux'); // Begin to use tube `'flux'`.
$beanstalk->put(
23, // Give the job a priority of 23.
0, // Do not wait to put job into the ready queue.
60, // Give the job 1 minute to run.
$msg // job body
);
$beanstalk->disconnect();
Then the job would be done in a code placed into a separate php file.
Something like:
use Beanstalk\Client;
$do=true;
try {
$beanstalk = new Client();
$beanstalk->connect();
$beanstalk->watch('flux');
} catch (Exception $e ) {
echo $e->getMessage();
echo $e->getTraceAsString();
$do = false;
}
while ($do) {
$job = $beanstalk->reserve(); // Block until job is available.
$emailParts = explode("##", $job['body'] );
// Use your SendMail function here
if ($i_am_ok) {
$beanstalk->delete($job['id']);
} else {
$beanstalk->bury($job['id'], 20);
}
}
$beanstalk->disconnect();
You can run separately this php file, as an independent php process. Let's say you save it as sender.php, it would be run in Unix as:
php /path/to/sender/sender.php & && disown
This command would run the file and alsow allow you to close the console or logout current user without stopping the process.
Make sure also that your web server uses the same php.ini file as your php command line interpreter. (Might be solved using a link to you favorite php.ini)
I hope it helps.
Your best bet is with a stacking or spooling pattern. It's fairly simple and can be described in 2 steps.
Store your emails in a table with a sent flag on your current thread.
Use cron or ajax to repeatedly call a mail processing php file that will get the top 10 or 20 unsent emails from your database, flag them as sent and actually send them via your favourite mailing method.
An easy way to do it is to call the code which handles your mails asynchronously.
For example if you have a file called email.php with the following code:
// Example array with e-mailaddresses
$emailaddresses = ['example1#test.com', 'example2#example.com', 'example1#example.com'];
// Call your mail function
mailer::sendMail($emailaddresses);
You can then call this asynchronously in a normal request like
exec('nice -n 20 php email.php > /dev/null & echo $!');
And the request will finish without waiting for email.php to finish sending the e-mails. Logging could be added as well in the file that does the e-mails.
Variables can be passed into the exec between the called filename and > /dev/null like
exec('nice -n 20 php email.php '.$var1.' '.$var2.' > /dev/null & echo $!');
Make sure these variables are safe with escapeshellarg(). In the called file these variables can be used with $argv
Welcome to async PHP
https://github.com/shuchkin/react-smtp-client
$loop = \React\EventLoop\Factory::create();
$smtp = new \Shuchkin\ReactSMTP\Client( $loop, 'tls://smtp.google.com:465', 'username#gmail.com','password' );
$smtp->send('username#gmail.com', 'sergey.shuchkin#gmail.com', 'Test ReactPHP mailer', 'Hello, Sergey!')->then(
function() {
echo 'Message sent via Google SMTP'.PHP_EOL;
},
function ( \Exception $ex ) {
echo 'SMTP error '.$ex->getCode().' '.$ex->getMessage().PHP_EOL;
}
);
$loop->run();
I'm building a webmail-client-like application, and I'm having issues connecting to POP3 through our beloved corp proxy.
Due to client requirements, I'm obligated to use sockets to talk with their POP3 server (they actively specified this is mandatory and they also explained they have a proxy between the app server and the POP) so besides enabling me to work this is kind of the only option I have.
Does anyone know how to do this?
UPDATED TO ADD:
Here's my socket handler:
<?php
class Socket {
protected $handle = null;
protected static $status = null;
public function __construct($server, $port){
if(!$this->connect($server, $port)){
throw new UnexpectedValueException("Server $server in port $port unreachable.");
}
}
public static function getStatus(){
return self::$status;
}
public function write($string){
fputs($this->handle, $string);
return $this;
}
public function read($lines = 1){
$lines = abs((int)$lines);//people can be morons
for($i = 0; $i < $lines; $i++){
$response []= $this->getLine();
}
if($lines==1){
$response = $response[0];
}
return $response;
}
public function connect($server, $port){
$errNo = 0;
$handle = fsockopen($server, $port);
if(!$handle){
return false;
}
$this->handle = $handle;
return $this;
}
public function disconnect(){
if(gettype($this->handle)=='resource'){
fclose($this->handle);
}
}
public function __destruct(){
$this->disconnect();
}
protected function getLine(){
return fgets($this->handle);
}
}
I'm using it like so:
$plug = new Socket('mx.mailserver.com',110);
$plug->write("USER $username\n")->read();
$could = $plug->write("PASS $password\n")->read();
if(strpos($could,'+OK')!==false){
$mails = $plug->write("STAT\n");
}
And right now our proxy is not letting me out; situation I would like to know how to revert.
First, have you tested with telnet - i.e. if you are on the same server / workstation that the PHP script is running on, and you issue:
telnet mx.mailserver.com 110
Do you get the greeting? (you're also not reading that - so your read from the command where you send the username is going to get that as a response, so your code will work but largely by accident.
If you can't get it to work via telnet then stop and get that working first - no point proceeding otherwise.
You may need to send \r\n at the end of each command.
$plug->write("USER $username\r\n")->read();
So you need to do something like:
$plug = new Socket('mx.mailserver.com',110);
$greeting = $plug->read(); // check this said +OK
$plug->write("USER $username\r\n")->read(); // check this said +OK
$could = $plug->write("PASS $password\r\n")->read();
if(strpos($could,'+OK')!==false) {
$mails = $plug->write("STAT\r\n");
}
I'm not entirely sure about your strpos() line - but I can't test that right now.
As I say, before you do ANYTHING else verify that it all works ok on Telnet.
If it does, then monitor the server logs (if possible) and see whether you're getting in touch with the POP3 server and what it's getting.
Here's an example sequence:
telnet pop.blah.com 110
+OK Welcome to MailEnable POP3 Server
USER matthew
+OK
PASS mypassword
+OK
STAT
+OK 0 0
Hope this helps.
Use this for client
https://github.com/clue/php-socks
Use this for proxy server
https://github.com/clue/psocksd
If your server has more than one public IP and you need these IP act as proxy outgoing, use this for proxy server https://www.inet.no/dante/
Guide to configure proxy to have multi outgoing ip: http://www.inet.no/dante/doc/faq.html#multiple_external_ips
Example config to have multi outgoing ip: http://wiki.jokeru.ro/dante-socks-server
They all work for me