Here is what I need to do. I need to be able to dynamically generate custom emails. I have been using PHP's mail() function, but I have been encouraged to try phpmailer or Zendmail. But it doesn't seem to be able to handle custom emails.
What I need to do is be able to grab values from the form and insert them into the body of the message. I've been doing:
$message = '<html><body><p>First name: ' $first . '<br/><br/>';
$message .= ...(rest of message)
Then I do:
mail($recipient, $subject, $message, $headers); using the right headers for HTML.
Is there a way to do what I want with phpmailer or Zendmail? Is there a way to do this in OOP instead that might improve on what are getting to be very lengthy pages? I'd appreciate some guidance.
Using phpmailer you could try the code below.
$message = '<html><body><p>First name: '. $first . '<br/><br/>';
$mailer = new PHPMailer();
// other fields / properties
$mailer->Subject = $subject;
$mailer->AddAddress($receipient);
$mailer->IsHTML(true);
$mailer->Body = $message;
$mailer->Send();
you'd need to set the other fields for it to work properly though.
Yes, one of the main points of having a mail library is to be able to create complex emails (more easily). I would also recommend SwiftMailer.
http://swiftmailer.org
Related
That is my code for sending email via wp_mail() method
$user_email = $_POST["user_email"];
$subject = 'Account is ready.';
$message = "Hello <b>$user_name</b>,a new account created for you and you need to create a password, <a href'http:example.com/account?email=$user_email'>click here</a> to create a password and login.";
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$mail = wp_mail($user_email, $subject, $message, $headers);
if($mail){
echo 'Sent email to user.';
}
else {
echo 'Oops something went wrong to sending email.';
}
actually that didn't work I mean not sending the email to user, while modify the wp_mail() method like this
$mail = wp_mail($user_email, $subject, $message);
that's working, that the mean it never taken more than three parameters, but I need to add $headers for HTML.
What can I do now?
Any advise?
Thanks
According to the wp_mail docs the headers parameter is used to set From, CC and BCC fields. It might not set other fields.
To set your content type and character set you need to use another function, please see linked docs.
Mail is send in plain text by default, to send it as HTML you should also consult linked docs as the method to switch to HTML is also there.
Have you setup, SMTP settings in WordPress panel if not you can use Many plugins to setup you can use m fav plugin
https://wordpress.org/plugins/post-smtp/
Try to set up this plugin and after let me know if you still facing those issues
I am looking to create one email (example#domain.com) to forward to all email addresses in a database.
People can email to example#domain.com, and then that email would get blasted to a list of predefined email addresses. It would need to include support for attachments.
I realize this is not secure at all and leaves this email address open to anybody to use, but this is what our organization wishes to do.
What would be the best way to do this on a PHP server?
Thanks.
This can be achieved in PHP Server as you have to go in depth of following things:
Email Piping
Email Headers
MIME Email attachments
Mailing List Management
While emailing to a lot of people in the way you mentioned is not a good idea...
You can use PHP's "mail" function:
$to = "user1#domain.com, user2#domain.com"; //(Comma separated list of emails)
$from = "noreply#domain.com";
$subject = "Hello";
$message = "Hello there!";
$headers = "From: ". $from . "\r\n".
"Reply-To: ".$from . "\r\n";
//send it
mail($to, $subject, $message, $headers);
while($row = mysql_fetch_array($emails))
{
$addresses[] = $row['address'];
}
$to = implode(", ", $addresses);
And then send mail using mail function ... mail($to, $subject, $message, $headers);
The way you have worded your query is a bit confusing but it seems you have a hosting server that is going to receive emails to a domain email. You then want to have those emails autoforwarded to a list of recipients. It looks like you want to investigate (given you mention sendmail) linux mail services and autoforwarding. You may be looking at postfix and possibly using procmailrc to trap and redirect incoming mail on your server.
I have done this with procmailrc before but it really depends on what service is handling the incoming mail.
There is no problem calling a CLI PHP script as suggested by #George to then read your recipients from the database and do the sending.
While Arthur's answer made sense and most likely would work, I ended up finding that my host had the feature I was looking for buried deep inside it. For those who were wondering, it is called discussion lists.
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 the following code to send users a verification email, which contains some variables such as username and token for verification purpose. The variable obviously changes on an individual basis. However, I would like to store this message separately in the PHP include_path so that I can re-use it somewhere else if need be. I don't want to hardcode it like it is now:
$to = $username;
$subject = 'Account Verification';
$now = date('Y-m-d H:i:s',time());
$message = "
Date sent:$now
Thanks for signing up!
Your account has been created, however we need to verify that this is your email address. Here is your verification token which you should copy and paste on the verification page:
<strong>$token</strong>
Alternatively, click the below link to activate your account:
http://localhost/login/verification.php?username=$username&token=$token
If you did not register with us. You <a href='http://localhost/login/'>can click</a> here to report this to us, or ignore this email.
Thanks,
The LocalHost Team
";
mail($to, $subject, $message);
So three questions:
How do I store this message to be like a template in such a way the variables are still accessible?
How do I turn this message into a nicely HTML formatted message with html markups?
Can I do this:
$to = $username;
$subject = 'Account Verification';
$now = date('Y-m-d H:i:s',time());
$message = include "verification_template.php";
mail($to, $subject, $message);
I solve the inclusion like this:
ob_start();
include $filename;
$message = ob_get_clean();
Alternatively to php, you can use template engines like Smarty to generate the html/text needed for your emails.
The advantage of using a templating engine is that your template can contain simple logic (such as conditionals, loops for handling dynamic data.
i wanna write a script sending e-mail to my client automatically using php
How do i send it automatically, for example, if they enter their email. and click submit
i wanna send this e-mail automatically
And, second do i need smtp server on my host? can i just this in any free hosting?
Thanks you guys and im so sorry for my language
Nikky
I probably wouldn't go with using the mail function directly : too many things you have to care about...
Instead, I would recommend using some mail-related library, which will deal with a lot of things for you.
One of those (which seems to have some success nowadays -- it's being integrated in the Symfony framework, for instance) is Swift Mailer.
Of course, it might be a bit overkill for just a simple mail... But investing some time in learning how to use such a library is always worth it ;-)
PHP doesn't implement SMTP protocol (RFC 5321) or IMF (RFC 5322), or MIME like Python for example. Instead - all PHP has is a simple C wrapper around sendmail MTA.
However - with all it's drawbacks - one can still create mime messages(multipart/alternative, multipart/mixed etc) and send html and text messages and also attach files using default PHP's mail() function. The problem is - it's not straightforward. You'll end up handcrafting entire message by using "headers" mail() argument, while setting "message" argument to ''. Also - sending emails in a loop through PHP's mail() will be a performance waste since mail() opens new smtp connection for each new email.
/**sending email via PHP's Mail() example:**/
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Because of these limitations most folks end up using third party libraries like:
PHPmailer (download)
Swiftmailer
Zend_Mail
Using these libraries one can construct text or html messages with ease. Adding files also becomes an easy thing to do.
/*Sending email using PHPmailer example:*/
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "from#example.com";
$mail->FromName = "Your Name";
$mail->AddAddress("myfriend#example.net"); // This is the adress to witch the email has to be send.
$mail->Subject = "An HTML Message";
$mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML.
$mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !";
$mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !";
if(!$mail->Send()) // Now we send the email and check if it was send or not.
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent.';
}
ALSO:
Q: do i need smtp server on my host? can i just this in any free hosting?
A: any shared hosting has SMTP server nowadays (sendmail/postfix).
Using built-in mail() function is not a good idea in most cases. So yes, either use the SwiftMailer or:
http://phpmailer.worxware.com/ - The PhpMailer which is in many senses is a similar implementation.