Swift Mailer : Address in mailbox given [] does not comply with RFC 2822, 3.6.2 - swiftmailer

I have a little problem here, when I try to send an email using swiftmailer, I got an error.
Here's my code:
<?php
session_start();
include("config.php");
$dir = dirname(__FILE__);
$session = $_SESSION[serialize];
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$hold = 'TIDAK';
// Kondisi - Kondisi yang ada
if($_GET[phase]=='confirm'){
$to = "test#127.0.0.1";
$kalimat = "Invoice Dengan nomor ".$_GET[nosc]." Telah di confirm, mohon di cek di sistem";
$kalimat2 = "Berikut terlampir data Invoice Customer , mohon di pastikan.";
$title = "CONFIRMATION NOTIFICATION";
}
// Convert string dlu
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject($title) // Message subject
->setTo(array($to)) // Array of people to send to
->setBcc(array($bcc)) // Bcc:
->setFrom(array($_SESSION[mailadmin] => $_SESSION[namauser].''.store('namasite'))) // From:
->setBody($kalimat, 'text/html'); // Attach that HTML message from earlier
// Send the email, and show user message
if($hold<>'YA'){
if ($mailer->send($message)){
echo json_encode(array('success'=>true));
} else {
echo json_encode(array('success'=>false));
}
}
?>
And then when I try to post the data to this file through jquery I got an error, this error I got from firebug.
<br />
<b>Fatal error</b>: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php:319
Stack trace:
#0 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(249): Swift_Mime_Headers_MailboxHeader->_assertValidAddress(NULL)
#1 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(107): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(71): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
#3 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderFactory.php(74): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array)
#4 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderSet.php(87): Swift_Mime_SimpleHeaderFactory->createMailboxHeader('Bcc', Array)
#5 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime in <b>C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php</b> on line <b>319</b><br />
So guys, anybody can help me here? I really doesn't know what happening here.

It's better if you use a try/catch around the send instruction:
try {
if ($mailer->send($message)){
...
}
} catch (Exception $e) {
$log_error->write_error($e->getMessage() . ';');
}
Or if you validate the email before sending it:
if(!Swift_Validate::email($email)){ //if email is not valid
//do something, skip them or log them
$log_error->log($email);
}

Please make sure there is no white space before or after the email address given.

Related

Enter the wrong email address, the email will still go to the correct email address

If I am sending the email to "test+10#gmail.com", it will still send to "test#gmail.com"
Here is my code, please tell me what I'm doing wrong?
$single_email = $this->single_email;
try {
Mail::send('welcome_mail',['user'=>$single_email], function($message) use($single_email) {
$name = "Name";
if($single_email->first_name){
$name = $single_email->first_name;
}
$message->to($single_email->email,$name)
->subject("Subject")
->from('from#gmail.com',"From")
->replyTo('replyto#gmail.com',"Reply to");
});
MailSend::where('id',$single_email->id)->update(['is_sent'=>3]);
} catch (\Exception $e) {
MailSend::where('id',$single_email->id)->update(['error'=>$e->getMessage()]);
}
Thanks.
That is how emails work. Gmail and most providers allow you multiple "sibling" emails by appending + to the end. For example, test+1#gmail.com and test+stackoverflow#gmail.com will both end up at test#gmail.com - One of many things people use this is to track which service leaked your email. E.g. test+facebook#gmail.com, if you start getting spam emails to test+facebook#gmail.com, you will know they revealed it.
So, there is nothing wrong with your code.

Google reCAPTCHA working on Local but not on my server

I have a form which requires a google reCAPTCH to be ticked. It is working perfectly on Local but does not work when I put it on the development server. I have replaced the registered keys to the ones appointed to me by Google.
It keeps outputting the error message.
The method in my form is post.
I do not understand why it doesn't work. Can someone please help me?
Here is my code:
$secretKey = "#######";
$captcha = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$throwErrorMessage = "You are a robot! ";
$throwError = 1;
$isvalid = False;
};
What version of ReCAPTCHA are you using? The docs on Google website here are pretty different than your code. In particular, you use the function file_get_contents while the documentation uses recaptcha_check_answer like in this example:
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again."."(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
Can you post the error message?

SwiftMailer using G Suite Issue

I am trying to setup SwiftMailer on my server. I am using the example configuration from the SwiftMailer docs for testing. I have a paid G Suite account but haven't finished setting up SSL on the server yet. I am running Cent OS 6.8 and Apache 2.2. I have googled everything I can think of and tried all the proposed solutions with no success.
My script is as follows, obviously my email address and credentials are correct in the real script. I appreciate any advice.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('swiftmailer-5.x/lib/swift_required.php');
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp-relay.gmail.com', 25)
->setUsername('myemail#mydomain.com')
->setPassword('*****')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Swift Mailer Test')
->setFrom(array('my from address'))
->setBody('Here is the message itself')
;
// Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('another#email.com' => 'Joe');
foreach ($to as $address => $name)
{
if (is_int($address)) {
$message->setTo($name);
} else {
$message->setTo(array($address => $name));
}
$numSent += $mailer->send($message, $failedRecipients);
}
printf("Sent %d messages\n", $numSent);
?>
When I run this script I get the following very vague error:
"Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp-relay.gmail.com [Connection timed out #110]' in /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php:269 Stack trace: #0 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php(62): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/AbstractSmtpTransport.php(113): Swift_Transport_StreamBuffer->initialize(Array) #2 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3 /var/www/html/appreciate-erp/mailtest.php(42): Swift_Mailer->send(Object(Swift_Message), Array) #4 {main} thrown in /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php on line 269"
As of https://support.google.com/a/answer/176600, it seems like SMTP Relay on G-Suite applies IP address restriction. The "could not connect" error would fit as the result of a missing configuration or the attempt to connect from another IP address as the one configured. Hope this helps!

magento newsletter keeps returning an error even after email is saved

There was a problem with the subscription.
this is the message I get after I enter an email. When I go to the admin panel the email is added.
I would like it NOT to display this message. any suggestions please
The only place in Magento that error string occurs is in the following try/catch block
#File: app/code/core/Mage/Newsletter/controllers/SubscriberController.php
try {
//...snip!...
}
catch (Mage_Core_Exception $e) {
$session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
}
catch (Exception $e) {
$session->addException($e, $this->__('There was a problem with the subscription.'));
}
Since you reported the error message as "There was a problem with the subscription.", that means the newsletter subscription code is throwing some sore of PHP exception, caught by the catch (Exception $e) { block. Magento does not output the messages from PHP exceptions. If I was in your position I'd temporarily change the exception handling code to include the error message
$session->addException($e, $this->__('There was a problem with the subscription. ' . $e->getMessage()));
This will let you track down the PHP error that's triggering your error message.
Per the comment below, the only place the "Cannot set standard header from addHeader()" exception error exists is
#File: lib/Zend/Mail.php
$prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
'reply-to', 'return-path',
'date', 'message-id',
);
if (in_array(strtolower($name), $prohibit)) {
/**
* #see Zend_Mail_Exception
*/
#require_once 'Zend/Mail/Exception.php';
throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
}
$value = $this->_filterOther($value);
$value = $this->_encodeHeader($value);
$this->_storeHeader($name, $value, $append);
return $this;
My guess is somewhere in your system someone's added some custom code that tries to set one of the standard email headers via the addHeader method.

Magento issue with email charset

I have a little issue. I'm using to send mail in my custom module this piece of code:
$content = 'Wiadomość testowa ąśźć';
$mail = Mage::getModel('core/email');
$mail->setToName($name);
$mail->setToEmail($email);
$mail->setBody($content);
$mail->setSubject($subject);
$mail->setFromEmail('test#test.com');
$mail->setFromName("Test");
$mail->setType('html');
try {
$mail->send();
Mage::getSingleton('customer/session')->setData('success',Mage::helper('adminhtml')->__('Your request has been sent'));
}
catch (Exception $e) {
Mage::getSingleton('customer/session')->setData('error',Mage::helper('adminhtml')->__('Unable to send email.'));
}
How can I set utf-8 charset before sending email in magento mail function. ?
I get email with no utf-8 charset in email body.
SOLVED
I created sendZendMail function in helper.
I used this function, instead Magento core/email:
public function sendZendMail($name,$email,$subject,$content){
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($content);
$mail->setFrom('your#frommail.com');
$mail->addTo($email, 'No reply');
$mail->setSubject($subject);
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Mail was sucessfully send.');
}
catch(Exception $ex) {
Mage::getSingleton('core/session')->addError('Unable to send email.');
}
}
kristoff, I had the same issue before with Portuguese, which also has special characters. I've sorted it out by using the function htmlentities() for accented words. The Zend Mail Object is a bit retarded. I hope it helps.

Resources