css不使用AWS进行电子邮件发送

时间:2013-09-23 05:59:22

标签: php email amazon-web-services html-email

我正在使用php开发一个网站,并希望在用户注册时向用户发送电子邮件。

我能够成功地做到这一点。 即使我使用html格式,它也以html格式发送。

我试过这个。我知道图像应该上传到服务器,但我会在以后再做。

$bodyPart = '
<body style="background-color:#FFFFFF;">

<img id="header" style="position:absolute; left:85px; top:50px; " src="images/logo.jpg" width="140" height="90" border="0" alt="" /></a>


<img id="cloths" style="position:absolute; left:275px; top:25px;" src="images/cloths.jpg" width="350" height="300" border="0" alt="" /></a>

<font size="7" style="font-family:Times new Roman; position:absolute; left:50px; top:120px;">Thank YOU</font>

<font size="3" style="font-family:Times new Roman;position:absolute;left:50px; top:165px;">For registering with our team </font>

<img id="bag" style ="position:absolute; left:290px; top:345px;"src="images/bag1.jpg" width="250" height="240" border="0" alt="" /></a>

<img style="position:absolute; left:30px; top:440px; "id="watch" src="images/watch1.jpg" width="260" height="180" border="0" alt="" /></a>

</body>

';

  $m->setMessageFromString($plainTextBody,$bodyPart);

  print_r($ses->sendEmail($m));

我应该得到这个输出

enter image description here

但我得到了这个。

enter image description here

但问题是格式化没有正确完成,即使它在我的localhost中作为独立文件工作。我尝试使用外部CSS,内部CSS和内联CSS。但它不起作用。

任何使用html发送电子邮件的CSS示例都会非常有用。

提前致谢。

4 个答案:

答案 0 :(得分:2)

<强>已更新 正如我所假设的,您刚刚完成了复制粘贴。请尝试以下代码。学习HTML基本语法。

  

不要忘记upvote&amp;标记为已回答;)

$bodyPart = ' 

<BODY 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body style="margin-top:20px;"> 
<table width="600" cellpadding="0" cellspacing="0" align="center"> 


<tr> 
  <td> 
    <img id="header" src="images/logo.jpg" width="140" height="90" border="0" alt="" /></a> 
  </td>
  <td>
    <img id="cloths" src="images/cloths.jpg" width="350" height="300" border="0" alt="" /></a> 
  </td>
</tr>

<tr>
  <td>
    <font size="7" style="font-family:Times new Roman;">Thank YOU</font> 
  </td>
  <td>
    <font size="3" style="font-family:Times new Roman;">For registering with our team </font> 
  </td>
</tr>
<tr>
  <td>
    <img id="bag" src="images/bag1.jpg" width="250" height="240" border="0" alt="" /></a> 
  </td>
  <td>
    <img id="watch" src="images/watch1.jpg" width="260" height="180" border="0" alt="" /></a> 
  </td>
  </tr> 
</table>';

echo $bodyPart;

<强>较旧 简单一点。把一个表放在你的html body&amp;固定宽度。

您的电子邮件正文应该如下

$body = <<<BODY
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<table width="600" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>=============Your content =========</td>
</tr>
</table>
BODY;

echo $body;

答案 1 :(得分:0)

发布您的代码..

但我认为问题是你的Content-Type

检查一下:

Content-type: text/html; charset=iso-8859-1\r\n

text/html是必要的..

<强>更新

路径不正确。

images/logo.jpg

将图像上传到服务器(或imagehoster)并插入整个链接。

更新2

我想我有解决方案:

setMessageFromString()的第一个参数是纯文本。第二个参数需要html内容。如果您想发送HTML - 电子邮件,请查看:

$m->setMessageFromString($plainTextBody, $HTMLBody); 

答案 2 :(得分:0)

<?php 

$to = 'rajeevranjan@hostname.com'; 

$subject = 'Test email '; 
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: rajeev ranjan"; 
$message='<div style="background-color:#FFFFFF;">

<img id="header" style="position:absolute; left:85px; top:50px; " src="images/logo.jpg" width="140" height="90" border="0" alt="" /></a>


<img id="cloths" style="position:absolute; left:275px; top:25px;" src="images/cloths.jpg" width="350" height="300" border="0" alt="" /></a>

<font size="7" style="font-family:Times new Roman; position:absolute; left:50px; top:120px;">Thank YOU</font>

<font size="3" style="font-family:Times new Roman;position:absolute;left:50px; top:165px;">For registering with our team </font>

<img id="bag" style ="position:absolute; left:290px; top:345px;"src="images/bag1.jpg" width="250" height="240" border="0" alt="" /></a>

<img style="position:absolute; left:30px; top:440px; "id="watch" src="images/watch1.jpg" width="260" height="180" border="0" alt="" /></a>

</div>

';
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

答案 3 :(得分:0)

我的示例在Boto3 library中使用Python 3,但我认为它仍然可以转化为PHP这个问题,主要是因为问题的核心不是特定于PHP而是HTMLHTML_BODY字段内的SES,并且与语言无关,因此这里的示例应该有所帮助。

此代码已成功发送到我的电子邮件地址,其格式与the example on W3 Schools here

中的显示格式完全相同

HTML示例:

<!DOCTYPE html>
<html>
<head>
<style>
table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}
</style>
</head>
<body>

<h2>Responsive Table</h2>
<p>A responsive table will display a horizontal scroll bar if the screen is too 
small to display the full content. Resize the browser window to see the effect:</p>
<p>To create a responsive table, add a container element (like div) with <strong>overflow-x:auto</strong> around the table element:</p>

<div style="overflow-x:auto;">
  <table>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
      <th>Points</th>
    </tr>
    <tr>
      <td>Jill</td>
      <td>Smith</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
      <td>50</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
      <td>94</td>
    </tr>
    <tr>
      <td>Adam</td>
      <td>Johnson</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
      <td>67</td>
    </tr>
  </table>
</div>

</body>
</html>

使用上述HTML的AWS SES示例:

#Your Imports go here
import boto3

# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "the_email_address_of_who_is_sending_this_email_out@gmail.com"

# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = "the_email_address_of_who_will_be_receiving_this_email@gmail.com"

# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the
# ConfigurationSetName=CONFIGURATION_SET argument below.
# CONFIGURATION_SET = "ConfigSet"

# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-east-1"

# The subject line for the email.
SUBJECT = "ARL Data Lake"

# The email body for recipients with non-HTML email clients.
# BODY_TEXT = ("Amazon SES Test (Python)\r\n"
#             "This email was sent with Amazon SES using the "
#             "AWS SDK for Python (Boto)."
#             )
BODY_TEXT = str(records)

# The HTML body of the email.

BODY_HTML = """<!DOCTYPE html>
    <html>
    <head>
    <style>
    table {
        border-collapse: collapse;
        width: 100%;
    }

    th, td {
        text-align: left;
        padding: 8px;
    }

    tr:nth-child(even){background-color: #f2f2f2}
    </style>
    </head>
    <body>

    <h2>Responsive Table</h2>
    <p>A responsive table will display a horizontal scroll bar if the screen is too 
    small to display the full content. Resize the browser window to see the effect:</p>
    <p>To create a responsive table, add a container element (like div) with <strong>overflow-x:auto</strong> around the table element:</p>

    <div style="overflow-x:auto;">
      <table>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
          <th>Points</th>
        </tr>
        <tr>
          <td>Jill</td>
          <td>Smith</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
          <td>50</td>
        </tr>
        <tr>
          <td>Eve</td>
          <td>Jackson</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
          <td>94</td>
        </tr>
        <tr>
          <td>Adam</td>
          <td>Johnson</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
          <td>67</td>
        </tr>
      </table>
    </div>

    </body>
    </html>"""

# The character encoding for the email.
CHARSET = "UTF-8"

# Create a new SES resource and specify a region.
client = boto3.client('ses', region_name=AWS_REGION)

# Try to send the email.
try:
    # Provide the contents of the email.
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            },
        },
        Source=SENDER,
        # If you are not using a configuration set, comment or delete the
        # following line
        # ConfigurationSetName=CONFIGURATION_SET,
    )
# Display an error if something goes wrong.
except Exception as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])

电子邮件结果:

enter image description here

来源和进一步的帮助/示例:

注意:

要轻松运行此示例,我建议设置一个快速的Cloud9 IDE in AWS,在其上安装Python 3,将我的代码复制并粘贴到其中,然后单击运行。如果您的Cloud9具有使用SES的适当角色和权限,则它应该可以工作。还要注意,Cloud9只是在EC2实例上运行,因此您可以SSH进入它(直接从浏览器的管理控制台上进行,这很好),并运行类似“ sudo yum install python-3.6”和使用“ sudo touch code_example.py”,“ sudo chmod 755 code_example.py”,“ sudo vi code_example.py”手动运行代码,然后按“ i”键代表插入,然后右键单击并使用我的SSH客户端将我复制到剪贴板的代码粘贴到SSH的VI文本编辑器中,然后单击“ ESC”退出插入模式,然后单击“:wq”并单击enter代表“写”,然后单击“退出” ,然后运行“ python3 code_example.py”。如果您成功安装了Python,它应该可以工作。但是就像我说过的,使用Cloud9,打开Cloud9的IDE,创建一个新的Python文件,粘贴代码,然后以Python 3的形式运行它会更容易,有一个按钮可以像任何普通的IDE一样。

enter image description here