How to store email template in PHP? - php

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.

Related

wp_mail() method not sending email in some cases

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

How to use the WordPress wp_mail(); function on an external PHP using Advanced Email Options settings e.g. WP-Mail-SMTP Plugin

How to use the WordPress wp_mail(); function on a custom external PHP page using Advanced Email Options saved connection settings.
Furthermore the PHP page is hosted in /wp-content/wpmailtest.php
I would like to know if this is possible and if so, I would like a brief explanation.
By the way example#example.com is not my email and has been changed for the example below:-
include '../wp-load.php';
$to = 'example#example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($to, $subject, $body, $headers);
Moreover The mail is not sent. But my saved settings work when i do a send test in the wp-admin page in "Advanced Email Options" e.g. WP-Mail-SMTP Plugin--->Settings, run email test.
include '../../../wp-load.php';
include this file in your custom php file on the top. then after you can access all wordpress functions inside that file. including wp_mail() function.

How to show only the name instead of the email address of the sender in a simple php mail script

I have a simple php mail script which will send emails.
The problem is while sending it is displaying sendername <senderemail#domain.com>
I have given in the code as $headers = "From: $name <$senderEmail>";
I want to display only name and not email address of the sender.
How do I do that?
Here is the program in its entirety.
<?php
if($_POST["submit"]) {
$recipient=$_POST["senderemail"];
$subject=$_POST["subject"];
$name=$_POST["name"];
$senderEmail=$_POST["email"];
$city=$_POST["city"];
$headers = "From: $name <$senderEmail>";
$mailBody="$city\n";
$process=explode(",",$recipient);
reset($process);
foreach ($process as $to) {
mail($to,$subject, $mailBody,$headers);
}
$thankYou="<p>Thank you! Your Message has been sent.</p>";
echo 'Thank you! Your Message has been sent.';
}
?>
A From header containing both name and address should be formatted like this:
From: User Name <user#example.com>
If you don't provide a name, it will display the email address.
PHPMailer has built-in support for creating this header correctly for you via the From and FromName properties, and the setFrom method (it doesn't matter which you use):
$mail->setFrom('user#example/com', 'User Name');
or
$mail->From = 'user#example/com';
$mail->FromName = 'User Name';
Your generated message already seems to contain the user's full name in the From: header (assuming $name is the human-readable name). How this is displayed by any individual user agent is no longer in your hands. Very often, it will display both parts regardless; or only shorten to the human-readable name if the sender's email address is in the recipient's address book. (Thunderbird takes this a step further and displays the information from the address book rather than the actual information in the message!)
One common problem is if the generated message has a different Sender: header; often, this will cause the recipient's user agent to display more than just the usual full name. Your question doesn't mention this, nor does it show one of the generated messages; but perhaps this will help you find a solution.

E-mail message/body section won't change

I have a login form when the user registers it sends out an E-mail to activate the account. The problem is that the e-mail body section is not changing, my code to send out the E-mail looks like this
$to ="$e";
$from ="jalilmotaz#gmail";
$subject = 'collegeloop activation';
$body ="hi";
$message="hi";
$headers ='From: $from';
mail($to, $message, $subject, $headers);
echo "signup_success";
exit();
when the E-mail is sent it looks like this http://imgur.com/rvxIv3g this is what the message originally was, but when I want to change it, as you see above It should say "hi" , it is not changing.
any suggestions?
Everything works as it should, really.
You changed $body variable but message is in $message. Try to change correct variable ;)
This looks like a caching problem. PHP can cache the rendered pages to speed up performance and email body is rendered as an HTML like all other pages.
If you are using APC then use the following link to clear cache and try again,
http://www.inmotionhosting.com/support/website/php-configuration/view-and-clear-php-apc-cache
If your server is on linux, then you can try the following,
find . -name "*.php.cache" | xargs rm -f

How do i create an email contact form for my website

I'm looking to create a form on my website where users can enter their email address and a message (however long). Then upon clicking on a submit button, the code, sends the message as an email to my mailbox.
I was wondering if someone knew what code i could use. I am using PHP as my server-side language.
You need to have access to an smtp server somewhere. Assuming you do, use the php mail function to send the mail like so:
$Name = "John doe"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "recipient#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for receiver"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header);
The smtp server is set in the php.ini file in these two lines:
SMTP = servername
smtp_port = 25
More information at w3schools site.
while the above two answers provide a basic email sending suggestion, there's one thing you should consider, the codes are not secure. spammers can inject Cc: codes and send spam using the form. if they do, your smtp provider may ban your account. try a dedicated mailer, like phpmailer
Try to use PHPForms. It is an ultimate web form builder that allows to create professionally looking email forms within just a few minutes. You will only need to create a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.

Categories