I have three problems with my mails sended by Zend Framework 2.
First of all, the subject appears twice with a comma. For example, if a set "My subject", the subject in Outlook or Gmail will be "My subject, my subject". Why it does that and how can I fix that?
After that, There is always an "UTF-8" written before the text of my mails. How can I remove that?
Finally, all my accents are replaced by "C) or )c". And yet, I set UTF-8.
I tested in Outlook and Gmail. All these errors are the same for Outlook and Gmail.
Thanks.
My code:
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\Sendmail as SendmailTransport;
.
.
.
$message = new Message();
$bodyPart = new \Zend\Mime\Message();
$bodyMessage = new \Zend\Mime\Part($emailTemplate);
$bodyMessage->type = 'text/html';
$bodyPart->setParts(array($bodyMessage));
$message->setEncoding("UTF-8")
->addFrom("$from")
->addTo("$courrielDestinataire")
->setSubject($contactObject->sujet)
->setBody($bodyPart);
$transport = new SendmailTransport();
$transport->send($message);
When I removed this from my emailTemplate and the «UTF-8» didn't show in my email, but I don't know why.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
However I didn't fix my two other problems. I make a var_dump($message->getSubject()) and it dump show my subject correctly.
I changed my code for this and fixed all my problems, but it's not ZF2
$headers = "Content-Type: text/html; charset=\"UTF-8\"";
mail("$courrielDestinataire","$contactObject->sujet","$emailTemplate",$headers);
Thanks for your help though.
For your question
In ZF2 subject appears twice with a comma. For example, if a set "My
subject", the subject in Outlook or Gmail will be "My subject, my
subject".
When sending an email with the default Zend mailer, the subject headers are added two times. This is because the header is set by the PHP mail() function, and is not removed from the additional headers if the server is Windows machine. This was fixed with issue ZF2-177 but only for *nix machines.
check this isssue
http://framework.zend.com/issues/browse/ZF2-177
Related
All the information are from a form. when submit the form, I use php mail() to send all the information to my email box, but I don't know how to format all the informatioin which from the submitted form.
is there a way to format the email which displays as the following style.
if using mail() can get that,how do i do? or there is another way to get that, thank you.
Don't use mail().
Instead use the phpmailer class.
http://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.phpmailer.php
http://phpmailer.worxware.com/
You call the class in your code.
require "includes/class.phpmailer.php";
Then you format your mail strings using HTML and a bit of common sense. To format the way you want you can use p and line breaks. Images, links, most of what you would think to use in html. It has worked well for me.
Tip: You don't want to include external css so your html might have to be a bit old school with text-align and setting width in using style.
edited.
call the include
require "includes/class.phpmailer.php";
then, set a few variables such as your message ... do your formatting.
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($emailAddress, "Your Reply Name"); // could be $replayemail if set
$mail->AddAddress($mailto); // who you are emailing
$mail->SetFrom($emailAddress, "Your From Name"); // from email
$mail->Subject = $subject; // keep it simple, email header
$mail->MsgHTML($message); // format fun stuff in $message
$mail->Send();
You need to learn about PHP HTML mail.
IMO best school for PHP beginners: http://www.w3schools.com/php/func_mail_mail.asp
Set the headers like this
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
mail($to,$subject,$message,$headers);
This would allow you to send html in mail.You can apply styling too.
I have been trying for awhile now to send mail using PHP, and I can't seem to get it to work.
I looked at the tutorials, and they make it seem simple enough. Yet, it seems to actually require more work than what is mentioned. Through research (to make this short), it seems that instead of the "From: " header actually having the address I give to it, it seems that... sendmail replaces it or something with daemon#ubuntu? Ok, even if I don't include a header I still get spam from my gmail account from the daemon#ubuntu thing (I am running ubuntu on a vm, and sending mail to my gmail account). I guess this could explain why I incessantly get errors when I try to send mail saying how there is an error in the "MAIL FROM command".
So before I get myself involved with messing around with sendmail, is this the (likely)culprit of the daemon#ubuntu nonsense? The tutorials make mention of having things like sendmail, but they really don't go into any details of what to do with them. I did make sure that sendmail is being used in my php configuration file.
I have heard of alternative mail servers like PHPmail or something like that. I really don't want to get involved in installing and configuring more software unless I really have to. So unless you guys really think sendmail isn't worth it, I would prefer advice on sendmail if it is the problem.
Thank you for your time.
edit: I am using the PHP mail() function on an Ubuntu vm by VMware Player.
So someone wanted to see my code. I somewhat wanted to avoid this simply because I tried many different possibilities. I will just include what I have in my file right now:
<?php
$myEmail = "#####gmail.com";
$fromAddr = $_POST['sender-email'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromAddr\r\n";
$headers .= "'X-Mailer: PHP/'" . phpversion() . "\r\n";
$message = $_POST['message'];
$status = mail($myEmail, $_POST['subj'], $message);
if($status) {
echo "Your message has been sent, " .$fromAddr. "!";
}
else {
echo "An error occurred while trying to send the mail.";
}
?>
The MIME type thing, and Content-type thing I added later on. I have tried this without them. I have also tried sending mail without using any headers at all, and still apparently get the daemon#ubuntu thing. Also, I edited out my e-mail address to post the code on here. Ok I guess I should add this too: I am getting the data from an HTML form from another page. On submit, I come to this php page where it grabs that data and then sends the email (this isn't for anything professional, I am just doing this to learn more about web stuff).
Postfix is a very easy to use and install email server, in my experience. You need an email server installed and configured before you're able to send email on your localhost.
SwiftMail does not send my email, while mail() does work. Same as here.
I added the EchoLogger but it does not print anything.
$message = Swift_Message::newInstance();
$message->setSubject('Test');
$message->setTo( $email );
$message->setFrom(ABSENDER);
$message->setBody($nl_text);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$logger = new Swift_Plugins_Loggers_EchoLogger();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
$result = $mailer->send($message, $failures);
var_dump($failures);
The email is returned in $failures but why?
Update
In Swift_Transport_SimpleMailInvoker::mail I dumped out the parameters and got this:
$headers =>
string(208) "Message-ID: <1337173064.4fb3a4480b8dd#domain.de>
Date: Wed, 16 May 2012 14:57:44 +0200
From: news#domain.de
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
"
$extraParams =>
string(15) "-fnews#domain.de"
$to, $subject, $body is same as I used in mail(). Still no idea what the problem is.
Update #2
This worked fine for me:
mail($email, 'Test', $nl_text, "From: " . ABSENDER);
Disclaimer: This is not a solution but the workaround I used, because I did not have the time to debugger the framework and find a real solution. Feel free to use the information given above to debug yourself and post your solution here. I will gladly accept and upvote it.
I suggest you use the debugger and step to through the code behind send().
If you can not use a debugger you should have a look at the function mail in lib/classes/Swift/Transport/SimpleMailInvoker.php. There is a call to the internal mail function with an shutup operator (´#´). Maybe removing this operator can show you an error message. Also you can use var_dump() to dump the parameters passed there.
I had the same problem.
According to the documentation swiftmailer.org by default Swift_MailTransport::newInstance() uses parameters -f%s and that is why you got "-fnews#domain.de", to solve that place blank space between -f %s.
Your code now can be looks like that:
$message = Swift_Message::newInstance('-f %s');
I solved that problem simply passing null to __construct(). So my working code looks like that:
$message = Swift_Message::newInstance(null);
I hope it will help someone.
P.S. I use Postfix to send emails.
I was able to fix it. I was using noReply#mydomain.com in from header and this email account was not created. I created noReply#mydomain.com and my emails started working. I hope this helps somebody.
Looks like you're using the default transport mechanism which according to their docs uses mail() by default.
Serious drawbacks when using this Transport are :
Unpredictable message headers
Lack of feedback regarding delivery failures
Lack of support for several plugins that require real-time delivery feedback
source
If I were debugging this, I would find SwiftMailer's implementation of the mail() function and dump the arguments it's passing and compare them to your own working version of mail(). I don't have any experience with SwiftMailer but this seems like a fast option.
The problem is in Swift_Transport_MailTransport, private $_extraParams = '-f%s'.
The flag '-f' (see your code: string(15) "-fnews#domain.de") will fail in some situations. Code Igniter has the same "bug".
I found out on symfony if I just exit the function with "exit()" for example, swiftmailer won't send the mail but if I ended with
return new Response(); it sends fine. so I am not sure.
This may be really old, Had the same problem i solved it when using a shorter from email. seems like the maximum length after the # sign is 10. that is what i have tested so far.
example:
$sname = 'noreply#system.com';
->setFrom(array($sname => 'Some Header Title here'));
first of all this question is for personal knowledge, and not for any kind of attack :) hope you'll believe me and give me some hints.
I'm trying to reproduce an example of mail header injection I found (link-> http://www.phpsecure.info/v2/article/MailHeadersInject.en.php). Basically it uses a form to get 3 parameters (subject, message and sender mail), then these parameters are sent with POST method and used in the php mail() function to an admin's mail.
Everything works fine, each mail is sent without problem but when I try to inject some other parameters as Cc, Bcc etc the trick doesn't work: neither \r & \n nor %0A & %0D are interpreted as CL and RF. For example, if I put my#mail.com%0ACc:foo#bar.com in the "From" field, in "my#mail.com" inbox I'll find the mail, with the same "From" field as it was sent (my#mail.com%0ACc:foo#bar.com). Does php or does input tag encode (or unencode) properly the input? How can I make it work?
Hope you can understand my bad english, thanks in advance, best regards.
ps: the article I linked is dated 2005, recently I've found that a similar bug with http headers splitting using php function "header()" was fixed, so I thought that they fixed email headers injection problem too.. But I can't find anything on the web that confirms this.
______________________EDIT________________________________________
Example working, modifying header within php code:
$to = "admin#mail.com";
$sub = "this is the subject";
$msg = "this is the message";
$header = "From: foo#foo.com"."\r\n"."Cc: bar#bar.com";
$if(mail($to, $sub, $msg, $header."\n")){
echo "sent";
}else{
echo "error";
}
The email is correctly received both from foo#foo.com and bar#bar.com
Examples NOT working (this is the problem I'd like to solve with your help):
Once I send the mail with "send" button, only foo#foo.com will get the e-mail, and in the "from" detail (inside the mail) I'll find (1st case) foo#foo.comrnCc: bar#bar.com or (2nd case)foo#foo.com%0D%0ACc: bar#bar.com.
I always find i need to use both \r\n in order for the headers to be sent properly.
I'm currently trying to get links working in emails sent via the PHP mail function. Here is my code - I've also included some things I've tried (commented out along with notes):
$to = "test#testing.com";
$subject = "Testing email";
//$body = '<strong>This is strong text</strong>'; <-- Works
//and the text is correctly emphasised.
//$body = 'Link Test'; <-- Works
//but without http:// at the start makes the link relative to the server root
//$body = "<a href='http://www.yahoo.com'>Link Test</a>"; <-- Does not work
//$body = "Link Test"; <-- Does not work
$body = 'Link Test'; //<-- Does not work
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Steven Parler <". $to . ">\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
As can be seen html is working without links, and links also work providing I don't include "http://" in the link itself. If I do include the "http://" then no email is received / sent at all (I'm unsure which as the mail() command returns true to say it was sent).
I'm not really sure why this isn't working. Could it be some setting that needs changing on my webhost's server? I'm on windows shared hosting.
Thanks in advance for any advice anyone can give me - been pulling my hair out over this lol. :)
Don't build MIME messages by hand. It's just too painful and fragile. Use something like PHPMailer or SwiftMailer to do it for your automatically. You provide the HTML, they'll provide the appropriate headers
I completely agree with #Marc B.
Here are a couple more options: XPertMailer, Zend_Mail, Rmail, HTML MIME MAIL
I've worked with all of these but since I'm working mostly with the Zend Framework I'm lately using Zend_Mail.
Having said that, your hosting provider might be blocking your emails because they might think it's SPAM. Try generating valid html markup that passes validation and see if that helps.
That's very odd behaviour, I'd expect the last 2 to work perfectly. I tried this out on my server, and mail($email, $subject, $body, $headers); worked perfectly with a $body of
text text text: \n http://website.com.
Perhaps it's a setting somewhere? If you're using awardspace, they require a from header to be used, and the mail sent from an e-mail registered with them. Other hosts might have a similar procedure.
Thanks for all the responses. I finally got around it by using the SMTP feature of Swiftmailer which correctly created the email with links.
Out of interest I tried the code I used in my original post again... but this time I used a random web address (http://www.trustedreviews.com). The email arrived... I then tried a lot of other web addresses - google.com, hotmail, yahoo.co.uk etc and they all arrived. Changed it back to (http://www.yahoo.com) and lo and behold the message did not arrive again. So it seems out of the millions of web URL's there are I chose the one that my webhost has decided to block lol...
That said the yahoo link does arrive ok using the smtp function of swift mail; it's only with the php mail function it doesn't seem to work. I guess I'll contact them and ask why it's blocked. I'll most likely stick with using the smtp method as at least it seems like it bypasses any restrictions.
Thanks again for everyone's time and help :)