My code-
$from = "From:Company\n\r";
$mesg = include('mail.html');
function mail($u,'hey',$mesg);
basically, i want to send the mail, where the message needs to be the mail.html.
help...the mesage needs to be sent to $u.
Here's an example (composed from the examples in the manual) of how you can do this when using Swift Mailer:
<?php
require_once 'lib/swift_required.php';
//Create the message
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject('Your subject')
//Set the From address with an associative array
->setFrom(array('your#email.com' => 'Your Name'))
//Set the To addresses with an associative array
->setTo(array('recipient#domain.org' => 'Recipient\'s name'))
//Set CC
->setCc('support#example.com')
//Give it a body
->setBody(file_get_contents('mail.html'))
;
//Create the Mailer
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
//Send the message
$result = $mailer->send($message);
This won't work because include() includes a file and evaluates (interprets the contents), you are trying to make it act like a function that returns a value which it is not.
What you need to do is read the contents of the file into a variable using file_get_contents() or a simillar function and then use it as the message body.
I advice you to use the PHPMailer Class really it will give you a lot of facilites and features and also the download contains examples you will need to include the class file then use it its free. for mor information go to this link
Don't use function in front of mail, just do
mail($emailto, $subject, file_get_contents('mail.html'), $headers);
Related
I have this code which I am using to send an email via Swiftmailer.
I tried many things, I tried with Phpmailer aswell and I am not able to send a simple e-mail. I also tried with mail() function in php and nothing, I don't have the sendmail configured, so I thought to use one of these libraries.
The code I am using is:
require_once ('lib/swift_required.php');
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject('Webinar Registration')
//Set the From address with an associative array
->setFrom(array('ami#am.com' => 'FROM NAME'))
//Set the To addresses with an associative array
->setTo(array('ami#am.com'))
//Give it a body
->setBody('My Message')
//And optionally an alternative body
//->addPart('<q>Here is the message itself</q>', 'text/html')
;
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('127.0.0.1', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
When I debug it, it stops right after the require statement. I am trying to send it via localhost and i don't want to use smtp.
Please any help is appreciated.
I am teaching myself php and how to use Swiftmailer. I have followed the doc and added the code below to my php file, but it returns array( ). I can't figure out what could be wrong and how to check. I don't seem to have access to the remote server's ini file from the hosting provider. Would appreciate any help in determining what may be wrong and how to correct. thanks!
require_once 'Swift/lib/swift_required.php';
//Use simple mail transport
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo("xxx#yyy.com");
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
//$message->setFrom($email);
$message->setFrom("xxx#zzz.net");
$message->setReturnPath('xxx#zzz.net');
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
I am trying to send email using SMTP in Laravel everything is good but the html elements is coming along in the email body.
i.e.,
I am getting the below as mail
<h2>Welcome</h2><br><p>Hello user</p><br><p>Thanks</p>
Instead of
WelcomeHello userThanks
Here is my Code :
What is the thing i am missing to make it applied on the content of the email
$msg = "<h2>Welcome</h2><br><p>Hello user</p><br><p>Thanks</p>"
$message->setBody($msg);
$message->to('user#gmail.com');
$message->subject('Welcome Mail');
Try this...
$msg = "Welcome
Hello user
Thanks"
$message->setBody($msg);
$message->to('user#gmail.com');
$message->subject('Welcome Mail');
One possible solution is to wrap your HTML code in body tag.
Take a look at Laravel mail api too.
Seems like you want to send an email as HTML format:
mail($to, $subject, $message, $headers);
You will have to use the $headers parameter.
The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML.
Source
Or, if you just want to add breaking lines and you leave HTML elements:
$msg = "Welcome \n Hello user \n Thanks"
I use PHPMailer to send email via SMTP. It works, but it doesn't save the sent emails in
sent items. I want to make to sent emails in the sent items, any idea?
I know can use imap_append function to achieve it. But, how to do that?
The PHPMailer seems doesn't has the function to return the $message.
if ($return = $mail->Send()) {
$stream = imap_open("{{$host}:993/imap/ssl}Sent Items", $user_name, $user_pwd);
imap_append($stream, "{{$host}:993/imap/ssl}Sent Items" , $message , "\\Seen");
imap_close($stream);
};
How to get the message body from PHPMailer?
The instance variables from PHPMailer are all public.
Looking at the source you can just use:
$mail->Body
If you want the plain body including the all boundaries you could also use:
$mail->CreateBody();
See:
http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/trunk/phpmailer/class.phpmailer.php?revision=452&view=markup
The PHPMailer seems doesn't has the function to return the $message.
Yes, it does. At least in version 5.2.28 there is a method getSentMIMEMessage() to get the full MIME message (different from the email body). That method only works after sending the email, or calling the preSend() method.
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