Index: /lib/wpsg_basket.class.php
===================================================================
--- /lib/wpsg_basket.class.php	(revision 7816)
+++ /lib/wpsg_basket.class.php	(revision 7817)
@@ -557,8 +557,28 @@
 		 * @param Array $arBasket (Daten des Basket als Array)
 		 */
-		public function sendOrderSaveMails($order_id, $arBasket, $bCustomerMail = true, $bAdminMail = true, $bDebug = false)
-		{
+		public function sendOrderSaveMails($order_id, $arBasket, $bCustomerMail = true, $bAdminMail = true, $bDebug = false) {
 			
 			$oOrder = wpsg_order::getInstance($order_id);
+			
+			$arAttach_Customer = [];
+			$arAttach_Admin = [];
+			
+            // Rechnung schreiben, damit ich sie an die Mail anhÃ€ngen kann
+			if ($this->shop->hasMod('wpsg_mod_rechnungen') && $this->shop->get_option('wpsg_mod_rechnungen_customermail') === '1') {
+			    
+			    try {
+			    
+			        // Ich brauche das PDF von der Rechnung
+                    $this->shop->callMod('wpsg_mod_rechnungen', 'writeAutoRechnung', [$oOrder->getId(), false, false]);
+                 
+                    $arAttach_Customer[] = $this->shop->callMod('wpsg_mod_rechnungen', 'getFilePathWithFilename', [$oOrder->getId()]);
+                    
+                } catch (\Exception $e) {
+			        
+			        echo $e;
+			        
+                }
+                
+            }
 			
 			$this->shop->view['oOrder'] = $oOrder;
@@ -572,6 +592,5 @@
 			$this->shop->view['basket']['checkout']['k_id'] = $oOrder->k_id;
 			
-			if ($bDebug === true)
-			{
+			if ($bDebug === true) {
 				
 				$this->shop->view['basket']['checkout']['datum'] = time();
@@ -583,8 +602,8 @@
 			
 			// Adminmail
-			if ($bAdminMail === true)
-			{
-				
-				$arAttach = array();
+			if ($bAdminMail === true) {
+				
+				$arAttach = $arAttach_Admin;
+				
 				// Anhang aufbereiten
 				$this->shop->callMod('wpsg_mod_orderupload', 'getAdminAttachment', array(&$arAttach));
@@ -622,5 +641,5 @@
 				else $mail_html = false;
 				
-				$arAttachments = array();
+				$arAttachments = $arAttach_Customer;
 				
 				if ($this->shop->get_option('wpsg_widerrufsformular_kundenmail') === '1' && wpsg_isSizedString($this->shop->get_option('wpsg_revocationform')))
Index: /mods/wpsg_mod_rechnungen.class.php
===================================================================
--- /mods/wpsg_mod_rechnungen.class.php	(revision 7816)
+++ /mods/wpsg_mod_rechnungen.class.php	(revision 7817)
@@ -217,6 +217,5 @@
 
 			}
-
-
+ 
 			if (array_key_exists('wpsg_rechnungen_logo_position_left', $_REQUEST) && strpos(wpsg_sinput("text_field", $_REQUEST['wpsg_rechnungen_logo_position_left']), 'selected') !== false)
 				$wpsg_rechnungen_logo_position = "left";
@@ -227,8 +226,6 @@
 			if (array_key_exists('wpsg_rechnungen_logo_position_right', $_REQUEST) && strpos(wpsg_sinput("text_field", $_REQUEST['wpsg_rechnungen_logo_position_right']), 'selected') !== false)
 				$wpsg_rechnungen_logo_position = "right";
-
-
-			if (file_exists($_FILES['wpsg_rechnungen_logo']['tmp_name']))
-			{
+ 
+			if (file_exists($_FILES['wpsg_rechnungen_logo']['tmp_name'])) {
 
 				$ending = strtolower(preg_replace("/(.*)\./", "", $_FILES['wpsg_rechnungen_logo']['name']));
@@ -246,7 +243,5 @@
 				}
 
-			}
-			else if ((array_key_exists('wpsg_rechnungen_logo_del', $_REQUEST)) && ($_REQUEST['wpsg_rechnungen_logo_del'] == "1"))
-			{
+			} else if ((array_key_exists('wpsg_rechnungen_logo_del', $_REQUEST)) && ($_REQUEST['wpsg_rechnungen_logo_del'] == "1")) {
 
 				@unlink($this->getFilePath('').'wpsg_rechnungen_logo.jpg');
@@ -314,4 +309,5 @@
 			$this->shop->update_option('wpsg_rechnungen_foottext_standard', $_REQUEST['wpsg_rechnungen_foottext_standard'], false, false, WPSG_SANITIZE_INT, ['allowEmpty' => true]);
 			$this->shop->update_option('wpsg_mod_rechnungen_hideCountry', $_REQUEST['wpsg_mod_rechnungen_hideCountry'], false, false, WPSG_SANITIZE_CHECKBOX);
+            $this->shop->update_option('wpsg_mod_rechnungen_customermail', $_REQUEST['wpsg_mod_rechnungen_customermail'], false, false, WPSG_SANITIZE_CHECKBOX);
 
 		}
@@ -724,8 +720,7 @@
 		} // public function admin_emailconf()
 		
-		public function basket_save_done(&$order_id, &$kunde_id, &$oBasket)
-		{
-
-			if ($this->shop->get_option('wpsg_mod_rechnungen_auto') != '1') return;
+		public function basket_save_done(&$order_id, &$kunde_id, &$oBasket) {
+
+			if ($this->shop->get_option('wpsg_mod_rechnungen_auto') != '1' || $this->shop->get_option('wpsg_mod_rechnungen_customermail') === '1') return;
 			
 			$this->writeAutoRechnung($order_id);
@@ -921,15 +916,12 @@
 		} // private function writeGutschrift($order_id)
 		 
-		public function writeRechnung($order_id, $preview, $bOutput = true)
-		{
+		public function writeRechnung($order_id, $preview, $bOutput = true) {
 
 			// PrÃŒfen ob schon eine Rechnung zu dieser Bestellung existiert und eventuell Fehler werfen
-			if (!$preview)
-			{
+			if (!$preview) {
 			
 				$lastEntry = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_RECHNUNGEN."` WHERE `o_id` = '".wpsg_q($order_id)."' AND `rnr` != '' ORDER BY `id` DESC LIMIT 1");
 				 
-				if (strtotime($lastEntry['datum']) > 0 && strtotime($lastEntry['storno']) === false)
-				{
+				if (strtotime($lastEntry['datum']) > 0 && strtotime($lastEntry['storno']) === false) {
 					
 					throw new \Exception(wpsg_translate(__('Rechnung konnte nicht geschrieben werden, da fÃŒr diese Bestellung (ID:#1#) bereits eine Rechnung (NR:#2#) geschrieben wurde.'), $order_id, $lastEntry['rnr']));
@@ -968,5 +960,5 @@
             			
 			$this->shop->view['kunde']['land'] = $this->db->fetchRow("SELECT L.* FROM `".WPSG_TBL_LAND."` AS L WHERE L.`id` = '".wpsg_q($this->shop->view['kunde']['land'])."'");
-			$this->shop->view['data']['shipping_land'] = $this->db->fetchRow("SELECT L.* FROM `".WPSG_TBL_LAND."` AS L WHERE L.`id` = '".wpsg_q($this->shop->view['data']['shipping_land'])."'");
+			$this->shop->view['data']['shipping_land'] = $this->db->fetchRow("SELECT L.* FROM `".WPSG_TBL_LAND."` AS L WHERE L.`id` = '".wpsg_q(wpsg_getStr($this->shop->view['data']['shipping_land']))."'");
 			$this->shop->view['rDatum'] = wpsg_sinput("key", @$_REQUEST['wpsg_rechnungen_datum']);
 			            
@@ -998,12 +990,12 @@
 			$this->shop->view['fussText'] = $this->shop->replaceUniversalPlatzhalter(wpsg_sinput("text_field", @$_REQUEST['wpsg_rechnungen_fusstext']), $order_id);
 			
-			if ($_REQUEST['wpsg_rechnungen_faelligkeit'] == '1')
-			{
+			if ($_REQUEST['wpsg_rechnungen_faelligkeit'] == '1') {
+			    
 				$this->shop->view['faelligkeitdatum'] = wpsg_sinput("key", $_REQUEST['wpsg_rechnungen_faelligkeitsdatum']);
+				
 			}
             				
 			// Beschreibung dazuladen
-			foreach ($this->shop->view['basket']['produkte'] as $k => $v)
-			{
+			foreach ($this->shop->view['basket']['produkte'] as $k => $v) {
 				
 				$produkt_id = $this->shop->getProduktID($v['id']);
@@ -1388,5 +1380,5 @@
 			
 		} // private function copyTemp($file, $r_id)
-
+         
         public function getDownloadFile($r_id) {
 
@@ -1702,16 +1694,24 @@
 		} // function foottext_list()
 		
+        public function getFilePathWithFilename($order_id, $url = false) {
+		    
+		    $invoice_db = $this->getOrderInvoice($order_id);
+		    
+		    $file = $this->getFilePath($order_id, $url).$invoice_db['id'].'.pdf';
+		    
+		    return $file;
+		    
+        }
+        
 		/*
 		 * Gibt den Absoluten Pfad zurÃŒck wo die Rechnungen gespeichert sind
  		 * Ist der Parameter $url auf true so wird der relative Pfad fÃŒr die Ausgabe in URLs zurÃŒckgegeben
  		 */
- 		public function getFilePath($order_id, $url = false)
- 		{ 
+ 		public function getFilePath($order_id, $url = false) { 
  		  	
  			if ($order_id == '') {
  			    
  			    // Briefpapier/Logo
- 				if ($this->shop->isMultiBlog())
- 				{
+ 				if ($this->shop->isMultiBlog()) {
  					
  					$path = WP_CONTENT_DIR.'/'.WPSG_MB_UPLOADS.'/wpsg/wpsg_rechnungen/';
@@ -1722,7 +1722,5 @@
  					else $strReturn = $path.'/'.$order_id.'/';
  					
- 				}
- 				else
- 				{
+ 				} else {
  					
  					$path = WPSG_PATH_CONTENT.'uploads/wpsg/wpsg_rechnungen/';
@@ -1741,6 +1739,5 @@
  				$ym = date('Y/m/', strtotime($datum));
  				
- 				if ($this->shop->isMultiBlog())
- 				{
+ 				if ($this->shop->isMultiBlog()) {
  					
  					$path = WP_CONTENT_DIR.'/'.WPSG_MB_UPLOADS.'/wpsg/wpsg_rechnungen/'.$ym;
@@ -1751,7 +1748,5 @@
  					else $strReturn = $path.'/'.$order_id.'/';
  					
- 				}
- 				else
- 				{
+ 				} else {
  					
  					$path = WPSG_PATH_CONTENT.'uploads/wpsg/wpsg_rechnungen/'.$ym;
@@ -1761,5 +1756,6 @@
  					
  				}
- 			}
+ 				
+ 			} 			
 						
 			$this->shop->protectDirectory($path);
@@ -1772,6 +1768,5 @@
  		 * Wird beim schreiben der Rechnung nach der Bestellung bzw. Bezahlung aufgerufen
  		 */
- 		private function writeAutoRechnung($order_id)
- 		{
+ 		public function writeAutoRechnung($order_id) {
  			
  			$order_data = $this->shop->cache->loadOrder($order_id);
@@ -1797,6 +1792,5 @@
  				
  			// StandardfuÃtext setzen, sollte er definiert sein
- 			if ($this->shop->get_option('wpsg_rechnungen_foottext_standard') !== false)
- 			{
+ 			if ($this->shop->get_option('wpsg_rechnungen_foottext_standard') !== false) {
  			
  				$foot_text = $this->shop->get_option("wpsg_rechnungen_footer");
@@ -1813,12 +1807,9 @@
  			}
  			
-		    try
-		    {
+		    try {
  			
 			    $this->writeRechnung($order_id, false, false);
-			    
-		    } 
-		    catch (Exception $e)
-		    {
+			    			    
+		    } catch (Exception $e) {
 			    
 			    die($e->getMessage());
Index: /views/mods/mod_rechnungen/settings_edit.phtml
===================================================================
--- /views/mods/mod_rechnungen/settings_edit.phtml	(revision 7816)
+++ /views/mods/mod_rechnungen/settings_edit.phtml	(revision 7817)
@@ -201,4 +201,5 @@
 			<?php echo wpsg_drawForm_Checkbox('wpsg_mod_rechnungen_showov', __('Bestellvariablen auf Rechnung anzeigen', 'wpsg'), $this->get_option('wpsg_mod_rechnungen_showov'), array('help' => 'wpsg_mod_rechnungen_showov')); ?>
 			<?php echo wpsg_drawForm_Checkbox('wpsg_mod_rechnungen_hideCountry', __('Land auf Rechnung verbergen', 'wpsg'), $this->get_option('wpsg_mod_rechnungen_hideCountry'), array('help' => 'wpsg_mod_rechnungen_hideCountry')); ?>
+			<?php echo wpsg_drawForm_Checkbox('wpsg_mod_rechnungen_customermail', __('Mail an AuftragsbestÃ€tigung anhÃ€ngen', 'wpsg'), $this->get_option('wpsg_mod_rechnungen_customermail'), ['help' => 'wpsg_mod_rechnungen_customermail']); ?>
 			<?php echo wpsg_drawForm_Input('wpsg_rechnungen_start', __('Startnummer (Rechnungen)', 'wpsg'), $this->get_option('wpsg_rechnungen_start'), array('help' => 'wpsg_rechnungen_start')); ?>
 			<?php echo wpsg_drawForm_Input('wpsg_gutschrift_start', __('Startnummer (Rechnungskorrektur)', 'wpsg'), $this->get_option('wpsg_gutschrift_start'), array('help' => 'wpsg_gutschrift_start')); ?>
@@ -232,7 +233,27 @@
 
 
-<script type="text/javascript">/* <![CDATA[ */
+<script>
 
 	jQuery(document).ready(function() {
+	    
+	    jQuery('#wpsg_mod_rechnungen_customermail').on('change', function() {
+	       
+	        if (jQuery(this).prop('checked') === true) {
+	            
+	            jQuery('#wpsg_mod_rechnungen_auto').val('1');
+	            
+			} 
+	        
+		});
+
+        jQuery('#wpsg_mod_rechnungen_auto').on('change', function() {
+            
+            if (jQuery(this).val() !== '1') {
+
+                jQuery('#wpsg_mod_rechnungen_customermail').prop('checked', false);
+                
+			}
+            
+		})
 
 		jQuery('#wpsg_tab').wpsg_tab( {
@@ -347,3 +368,3 @@
 
 
-	/* ]]> */</script>
+</script>
