Swift mailer connection refused with symfony1.2 - php

Im having some problems sending mail. I have a ubuntu server running postfix as mailserver. The webapp is based on symfony 1.2 and has Swift 3 for mailing. Everyrhing is running on the same server. This is the code im trying to run:
public static function sendMailLocal($mailFrom, $mailto, $subject, $mailBody, $prevError)
{
$mailer = null;
try
{
$connection = new Swift_Connection_SMTP('localhost', '25');
$mailer = new Swift($connection);
// Create the mailer and message objects
$message = new Swift_Message($subject, $mailBody, 'text/html');
// Send
$mailer->send($message, $mailto, $mailFrom);
$mailer->disconnect();
}
catch (Exception $e)
{
if($mailer != null)
$mailer->disconnect();
throw new EmailTransferException ("There was an error while trying to send the email - locally:" . $e->getMessage() . " , first error:" . $prevError);
}
}
This is the error message im getting:
There was an error while trying to send the email - locally:The SMTP connection failed to start [localhost:25]: fsockopen returned Error Number 111 and Error String 'Connection refused' ,
It's kind of strange, because i did manage to send a mail without swift with this code:
//adresses are examples
$to = "john.doe#mail.com";
$sender_email = "someone#mail.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: <'.$to.'>' . "\r\n";
$headers .= 'From: John Doe<'.$sender_email.'>' . "\r\n";
// Mail it
$success = mail($to, "test from server", "msg", $headers);
Anybody got any ideas about this??
thanks!

It's not a problem with Symfony or PHP - your local SMTP server is unreachable. You likely have a configuration problem with your mail server that is preventing the SMTP server being available on it's native port (25).

Related

PHP Sendgrid - Sender name is not being sent in mail

I am working on PHP Sendgrid.. Everything is working fine.. but the problem is
I cannot add the Sender Name with from email somehow. I think it didn't captures the headers or what i have no idea.
P.S : I have executed this code on a separate server and it works. But when I try to send mail from the other server with same code it sends the email but doesn't set/show the Sender Name. Is it some server side issue I am using Laravel 4? please guide
$headers = "From: Coffe Email" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
sendMail("myemail","mysubject","msg", $headers,$from="admin#email.com", "admin");
Function's Definition (sendMail) :
function sendMail($to, $subject, $message, $headers, $from = "", $from_name="", $to_name="")
{
require($_SERVER['DOCUMENT_ROOT']."/../sendgrid-php/sendgrid-php.php");
$email = new \SendGrid\Mail\Mail();
$email->setFrom($from, $from_name);
$email->setSubject($subject);
$email->addTo($to, $to_name);
$email->addContent("text/plain", strip_tags($message));
$email->addContent("text/html", $message);
$sendgrid = new \SendGrid($settings->send_grid_user);
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
}

PHP mail causing phishing warning on gmail

I am using PHPMailer to send automated e-mails from my website and while testing, I noticed that when I sent mail from website by Gmail, then e-mails sent by php mailer are generating the following warning on the recipients end:
This message may not have been sent by: example#gmail.com Learn more Report phishing.
But when I use other emails (like yahoo, outlook), then I got no emails in my $contact_email. Please help me to solve this problem.
PHP Mailer code:
<?php
global $_REQUEST;
$response = array('error'=>'');
$user_name = substr($_REQUEST['user_name'], 0, 20);
$user_email = substr($_REQUEST['user_email'], 0, 40);
$user_msg = $_REQUEST['user_msg'];
$contact_email = 'contact.arefin#gmail.com';
if (trim($contact_email)!='') {
$subj = 'Message from Official Website';
$msg = "Name: $user_name
E-mail: $user_email
Message: $user_msg";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To: $user_email\n"
. "To: $contact_email\n"
. "From: $user_email\n";
if (!#mail($contact_email, $subj, $msg, $head)) {
$response['error'] = 'Error send message!';
}
} else
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
?>
When you send bulk emails and especially when you mock the sender address, you need to use best practices that may reduce how many servers block you as spammer.
Three things which I think you should do are:
1) Use appropriate mail headers
Add the following to your code - a notice that this is a bulk sender, and the OPT-OUT email address:
.= "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n"; // this will identify the real sender
.= "Precedence: bulk" . "\r\n"; // this will say it is bulk sender
.= "List-Unsubscribe:info#YOUR_SITE_DOMAIN\r\n"; // this will reveal the OPT-OUT address
Read more about it here
2) Make sure your server domain has a reverse DNS record. This will tell the recipient's server that your domain is REALLY hosted on your server.
3) Publish SPF record with your domain. You can read more about it here, and google it for other big handlers (like Yahoo).
In addition to those, make sure you are adding a footer with a "one click" OPT-OUT removal option and explanation note that this message is sent on behalf, and who is the original sender.
Cheers
You can either set up google apps for your site and get a Username#yourwebsite.com gmail account (more info: http://www.google.com/enterprise/apps/business/), or You will need to set up an e-mail address on your current server that is Username#yourwebsite.com and use that as the $mail->from address.
Your E-Mail recipients are receiving the message because you are telling google to send an e-mail from your server, and then you are telling them that the mail is coming from gmail, which it isn't, it's coming from your personal server. Since the from address and your server address don't match, they flag it as spam. This is googles way of preventing spam, to them it would be the same if you put $mail->from(YOURMOM#LOL.com). The e-mail would still send, but your domain name does not match the # address.
Try this code , In your code i found some mistake which i have solved here.
global $_REQUEST;
$response = array('error'=>'');
$user_name = substr($_REQUEST['user_name'], 0, 20);
$user_email = substr($_REQUEST['user_email'], 0, 40);
$user_msg = $_REQUEST['user_msg'];
$contact_email = 'contact.arefin#gmail.com';
if (trim($contact_email)!='') {
$subj = 'Message from Official Website';
$msg = "Name: $user_name
E-mail: $user_email
Message: $user_msg";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To: $user_email\n"
. "To: $contact_email\n"
. "From: $user_email\n";
if (!#mail($contact_email, $subj, $msg, $head)) {
$response['error'] = 'Error send message!';
}else{
$response['error'] = 'success!';
}
} else {
$response['error'] = 'Error send message!';
}
echo json_encode($response); die();

php mail function error on windows hosting

Mail function now not working my server. i cannot find out the solution for that. i am getting bellow this error when i submitting the contact form.
Warning: mail() [function.mail]: SMTP server response: 550 Sender is not allowed. in E:\HostingSpace\abayamtranslations.com\httpdocs\contact-form\classes\contact.php on line 137
ERROR! Please ensure PHP Mail() is correctly configured on this server.
$address = "info#abayamtranslations.com";
ini_set("SMTP","mail.abayamtranslations.com");
ini_set("smtp_port","25");
ini_set('sendmail_from','info#abayamtranslations.com');
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
Try with the following
$emailFrom = "info#abayamtranslations.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Subject of email";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Message of email";
mail($email, $subject, $message, $headers);
"550 Sender is not allowed" means that the SMTP Blocked Senders list rejected the sender. Search your SMTP log for 550 Sender is not allowed and figure out what address is getting blocked and then alter your list to let that through.
That is the meaning of the error you are getting. This is from the hMailServer Documentation.
Can you try if the following will work?
<?php
mail('info#abayamtranslations.com','Test Email','This is a test email.',"From: info#abayamtranslations.com");
?>
If it doesn't work, then it's probably due to a misconfiguration in your hMailServer and you would need to check your hMailServer Logs.
You are on the windows server, hence forth you have to change SMTP of windows server.
Better way is to use PHPMailer Library. https://github.com/PHPMailer/PHPMailer
Some important information is missing under your SMTP configuration....
username and password is needed to authenticate with the SMTP mail server.....
it easy ti use smtp mailing in php with:
PHPMailer (https://github.com/Synchro/PHPMailerhttps://github.com/Synchro/PHPMailer)
have a look into it....
Thankyou....

SMTP ERROR : Could not authenticate

i am trying to use phpMailer to send confirmation messages to users via email. my code is this:
$SMTP_USERNAME = SMTP_USERNAME;
$SMTP_PASSWORD = SMTP_PASSWORD;
$SMTP_HOST = SMTP_HOST;
$SMTP_STATUS = SMTP_STATUS;
if($SMTP_STATUS==1)
{
include($_SERVER['DOCUMENT_ROOT']."/modules/SMTP/smtp.php"); //mail send thru smtp
}
else
{
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$from.'' . "\r\n";
$headers .= 'Bcc: '.$to.'' . "\r\n";
mail($from,$subject,$message,$headers);
}
but every time I load it, it displays this
Error msg: " SMTP Error: Could not authenticate.
Please help me out from these issue.
Thanks for your time guys, hope someone can get back to me soon!
The mail() function does not allow any kind of authentication. You need to switch to a third-party mail package that implements the required SMTP authentication (or write you own, which I wouldn't recommend). Typical choices include:
PEAR Mail
PHPMailer
Swift Mailer

Sending email from php

I am using following php code but i ain't getting any mail
function Mail($to, $subject, $message)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: MYAPP! <myYahooMail>' . "\r\n";
// Mail it
if(!mail($to, $subject, $message, $headers)) {
throw new Exception('There was a problem trying to send an email.');
}
}
The error you're getting is Fatal error: Cannot redeclare mail(). This is because
PHP has a built in mail function. Name your function something else.
You probably need to edit your php.ini (xampp\php\php.ini). Search for "mail function" and change these parameters according your server specs. If you are not the host, you can do this via htaccess.
SMTP = "Your server smtp address here"
smtp_port = 587

Categories